Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从外部文件打开集合,然后关闭文件,并显示集合项_Excel_Vba_Loops_Collections - Fatal编程技术网

Excel 从外部文件打开集合,然后关闭文件,并显示集合项

Excel 从外部文件打开集合,然后关闭文件,并显示集合项,excel,vba,loops,collections,Excel,Vba,Loops,Collections,是否可以从一个外部文件中弹出一个公共集合?!?!? 我无法显示收藏项目, 为了测试这段代码,我尝试循环集合的项 如果循环在关闭wb源之前,结果显示正确 但是如果我关闭che wb source,我只能计算items集合。item=31(右结果)如果我尝试调试。print collection(x)'x是1到31之间的整数 我只检索到错误424。 我的代码是错误的,还是不可能从外部文件中编译一个集合,在这种情况下,我必须使用什么 下面是我的代码: option explicit public Be

是否可以从一个外部文件中弹出一个公共集合?!?!? 我无法显示收藏项目, 为了测试这段代码,我尝试循环集合的项 如果循环在关闭wb源之前,结果显示正确 但是如果我关闭che wb source,我只能计算items集合。item=31(右结果)如果我尝试调试。print collection(x)'x是1到31之间的整数

我只检索到错误424。 我的代码是错误的,还是不可能从外部文件中编译一个集合,在这种情况下,我必须使用什么

下面是我的代码:

option explicit
public Belts as collection
Public Sub mCaricaBelts()
On Error GoTo RigaErrore
    Dim wb As Workbook, wbn As String
    Dim sh As Worksheet
    Dim rng As Range
    Dim c As Range, v As Variant
    Dim lrw As Long
    
    With Application
        .ScreenUpdating = False
        .Calculation = xlManual
        .StatusBar = _
            "Sto caricato la tabella Fasce"
    End With
    wbn = "Listino.xlsx"
    If Not Belts Is Nothing Then
        Set Belts = Nothing
    End If
    
    Set Belts = New Collection
    If AlreadyOpen(wbn) Then
        Set wb = Workbooks(wbn)
    Else
        Set wb = Workbooks.Open("\\itcpifs01\license$\Listino.xlsx")
    End If
    Set rng = wb.Worksheets("dbRatesSTD").Range("C1")
    Set rng = Range(rng, rng.End(xlToRight))
    For Each c In rng
        Belts.Add c
    Next

' if I put the loop here, I can show results
    For Each v In Belts
       Debug.Print v
    Next


Application.DisplayAlerts = False
    wb.Close
Application.DisplayAlerts = True

' if I put the loop here, I can't show results, Error 424 Object required
    For Each v In Belts
       Debug.Print v
    Next

RigaChiusura:
    Set c = Nothing
    Set rng = Nothing
    Set sh = Nothing
    With Application
        .ScreenUpdating = True
        .Calculation = xlAutomatic
        .StatusBar = ""
    End With
    Exit Sub

RigaErrore:
    MsgBox Err.Number & vbNewLine & Err.Description
    Resume RigaChiusura

End Sub

请尝试
Belts.Add c.Value
,这会将单元格中的值添加到集合中,而不是将对范围对象的引用添加到集合中。工作簿关闭后,该值仍然可用,而引用将不再可用。

Array version 我对集合了解不多,但我认为数组是一个不错的选择

Option Explicit
Public Belts As Variant 'Public Belts As Collection
Public Sub mCaricaBelts()
On Error GoTo RigaErrore
    Dim wb As Workbook, wbn As String
    Dim sh As Worksheet
    Dim rng As Range
    Dim c As Range, v As Variant
    Dim lrw As Long

    With Application
        .ScreenUpdating = False
        .Calculation = xlManual
        .StatusBar = _
            "Sto caricato la tabella Fasce"
    End With
    wbn = "Listino.xlsx"
'    If Not Belts Is Nothing Then
'        Set Belts = Nothing
'    End If

'    Set Belts = New Collection
    If AlreadyOpen(wbn) Then
        Set wb = Workbooks(wbn)
    Else
        Set wb = Workbooks.Open("\\itcpifs01\license$\Listino.xlsx")
    End If
    Set rng = wb.Worksheets("dbRatesSTD").Range("C1")
    Set rng = Range(rng, rng.End(xlToRight))
    'Be careful, this is a horizontal array.
    Belts = rng
'    For Each c In rng
'        Belts.Add c
'    Next

' if I put the loop here, I can show results
    For Each v In Belts
       Debug.Print v
    Next


Application.DisplayAlerts = False
    wb.Close
Application.DisplayAlerts = True

' if I put the loop here, I can't show results, Error 424 Object required
    For Each v In Belts
       Debug.Print v
    Next

RigaChiusura:
    Set c = Nothing
    Set rng = Nothing
    Set sh = Nothing
    With Application
        .ScreenUpdating = True
        .Calculation = xlAutomatic
        .StatusBar = ""
    End With
    Exit Sub

RigaErrore:
    MsgBox Err.Number & vbNewLine & Err.Description
    Resume RigaChiusura

End Sub
我有较旧的Excel,因此我只能想象
AlreadyOpen(wbn)
的意思,但下面是代码完整If语句的老方法:

'Check if Excel file is opened:
On Error Resume Next
  Set wb = Workbooks(wbn) 'Workbook is opened.
  If Err then 'Workbook is closed.
    Set wb = Workbooks.Open("\\itcpifs01\license$\Listino.xlsx")
    Err.Clear
  End If
On Error GoTo RigaErrore 'Reactivate your 'first line error'.
代码中没有常量。我会将此添加到代码的开头,并进行适当的更改:

Const cStrTabella as String = "Sto caricato la tabella Fasce"
Const cStrWbn as String = "Listino.xlsx"
Const cStrWbp as String = "\\itcpifs01\license$\Listino.xlsx"
Const cStrWs as String = "dbRatesSTD"
Const cStrAddress as String = "C1"

现在,更改内容和其他人修改内容以进行测试变得容易多了,您可以更快地获得问题的答案。

正确,要存储正确的值,请使用c.value,否则sys仅存储地址(),tnk