在启动单元使用ActiveCell.Offset时如何设置动态的结束单元?

时间:2019-01-29 17:37:08

标签: excel vba

我正在使用一个表从单独的工作簿中提取信息。当前该表具有一个设置的结束单元格,但是我提取的信息过多。我想将结束单元格设置为D列中数据的最后一行。我需要帮助修改代码以将结束单元格设置为动态范围。

我已经尝试使用lastRow = .Cells(.Rows.Count, col).End(xlUp).Row,但我不断获得

  

编译错误

前面.Offset中的

参考无效或不合格

Sub GetData()
    Dim strWhereToCopy As String, strStartCellColName As String
    Dim strListSheet As String
    Dim strCopySheet As String

    strListSheet = "List"

    On Error GoTo ErrH
    Sheets(strListSheet).Select
    Range("B2").Select

'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
    Set currentWB = ActiveWorkbook
    Do While ActiveCell.Value <> ""

        strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value
        strCopyRange = ActiveCell.Offset(0, 2) & ":" & lastRow = 
.Cells(.Rows.Count, col).End(xlUp).Row
        strWhereToCopy = ActiveCell.Offset(0, 4).Value
        strCopySheet = ActiveCell.Offset(0, 6).Value
        strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1)

        Application.Workbooks.Open strFileName, UpdateLinks:=False, 
ReadOnly:=True
        Set dataWB = ActiveWorkbook

        Sheets(strCopySheet).Select
        Range(strCopyRange).Select
        Selection.Copy

        currentWB.Activate
        Sheets(strWhereToCopy).Select
        lastRow = LastRowInOneColumn(strStartCellColName)
        Cells(lastRow + 1, 1).Select

        Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
        Application.CutCopyMode = False
        dataWB.Close False
        Sheets(strListSheet).Select
        ActiveCell.Offset(1, 0).Select
    Loop
    Exit Sub

ErrH:
    MsgBox "It seems some file was missing. The data copy operation is not 
complete."
    Exit Sub

End Sub

0 个答案:

没有答案