在未打开工作簿的情况下,使用封闭的工作簿数组时,请在“ for”循环中使用对象值



我检查了类似的问题,但它们似乎都没有提供解决方案。

我需要工作簿MIDI,工作表DL.Cells从A3到Lastrow到Lastrow到工作表HDaER.Cells中的CC_6 A2到最后一行。

我使用此For循环来参考当前工作簿中的数组,其中包含表HDaER

当我尝试使用相同的方法来参考封闭的工作簿(MIDI(,工作表.Values0时,我在HDaER中什么也没得到.Values

    Dim MIDI As Workbook
    Dim DL As Worksheet
    Dim HDaER As Worksheet
    Dim strFile As String
    Dim n As Long
    Dim o As Long
    Set HDaER = Sheets("HistoricalDataandExcessReturns")
    
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    'Insert dates
    
    strFile = "C:UsersBettyAppDataRoamingMetaQuotesTerminalB4D9BCD10BE9B5248AFCB2BE2411BA10MQL4FilesMultiInstrDataInput.xlsm"
    Set MIDI = Workbooks.Open(strFile, True, True)
    Set DL = Workbooks("MultiInstrDataInput.xlsm").Sheets("DataList")
                               
    For n = 3 To DL.Range("$B" & DL.Rows.Count).End(xlUp).Row
        If DL.Range("$A" & n).Value <> "" Then
            o = HDaER.Cells(HDaER.Rows.Count, "$A").End(xlUp).Row + 1
            HDaER.Range("$A" & o).Value = DL.Cells(n, 1).Value
        End If
    Next n
                
    MIDI.Close False
            
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
        
End Sub

没有显示错误,但没有移动.Values
我更喜欢使用For循环,而不是.Copy

我只能建议一些小调整,但是我没有看到您现有代码的任何问题。如果存在值但没有复制,则缺少一些信息。

Sub Tester()
    'use contrants for fixed values
    Const STR_FILE As String = "C:UsersBettyAppDataRoamingMetaQuotesTerminal" & _
                   "B4D9BCD10BE9B5248AFCB2BE2411BA10MQL4FilesMultiInstrDataInput.xlsm"
    Dim MIDI As Workbook, DL As Worksheet, HDaER As Worksheet
    Dim n As Long, o As Long, v
    Set HDaER = ThisWorkbook.Sheets("HistoricalDataandExcessReturns") ' if in same file as the code
    o = HDaER.Cells(HDaER.Rows.Count, "A").End(xlUp).Row + 1 'start row for copied values
    Set MIDI = Workbooks.Open(STR_FILE, True, True)
    Set DL = MIDI.Sheets("DataList")
    For n = 3 To DL.Cells(DL.Rows.Count, "B").End(xlUp).Row
        v = DL.Cells(n, "A").Value
        If Len(v) > 0 Then
            Debug.Print "copying value '" & v & "' to A" & o
            HDaER.Cells(o, "A").Value = v
            o = o + 1
        End If
    Next n
    MIDI.Close False
End Sub

相关内容

最新更新