首页 文章

使用单元格引用(VBA)复制/粘贴数据

提问于
浏览
1

我正在尝试将数据从一个文件复制到另一个文件 . 首先,我想找到源文件中的最后一行/列,并使用该信息来复制数据 . 但是,我似乎无法使用对最后一行和列的引用来复制/粘贴代码 . 这是我现在拥有的:

Dim G7_Tab_Name As String
   G7_Tab_Name = "G7"
Dim wkbDest As Workbook
   Set wkbDest = ActiveWorkbook
Dim SheetToCopy As Worksheet

LastInputRow = wkbSource.Sheets(1).Cells(Cells.Rows.Count, "A").End(xlUp).Row
LastInputColumn = wkbSource.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column
Set SheetToCopy = wkbSource.Sheets(1)
CopyRange = Range(SheetToCopy.Cells(LastInputRow, 1), SheetToCopy.Cells(1, LastInputColumn))

'Check if any existing data
If Len(wkbDest.Sheets(G7_Tab_Name).Range("A1").Value) > 0 Then
    wkbDest.Sheets(G7_Tab_Name).UsedRange.ClearContents
    CopyRange.Copy _
    Destination:=Range(wkbDest.Sheets(G7_Tab_Name).Cells(LastInputRow, 1), Cells(1, LastInputColumn))
    Else
       CopyRange.Copy _
       Destination:=Range(wkbDest.Sheets(G7_Tab_Name).Cells(LastInputRow, 1), Cells(1, LastInputColumn))
End If

我'm pretty sure that I'我没有在我的CopyRange定义中捕获正确的范围,但我不知道为什么 . 在这种情况下, lastRow=27lastColumn=13 . 所以,我认为范围定义为 "A1:M27" .

我正在定义这些范围,因为我需要遍历多个文件并从每个文件的第一张表中复制数据 . 此代码将包含在一个更大的For循环中以命中每个文件(一旦我可以将其复制/粘贴!)

1 回答

  • 1

    每当你使用 Cells()Range()Columns()Rows() 等时,请确保你让VBA知道你希望使用哪个工作表,否则,'ll use the ActiveSheet, which can cause errors. You did this in some places, but not others. Below I'已经完成了使用 With 语句的混合,使这些工作表更长名 . 阅读下面的内容,看看你是否能看到我做了什么 . 如果您有任何疑问,请告诉我!

    Sub t()
    
    Dim G7_Tab_Name As String
    G7_Tab_Name = "G7"
    Dim wkbDest As Workbook
    Set wkbDest = ActiveWorkbook
    Dim SheetToCopy As Worksheet
    Set SheetToCopy = wkbSource.Sheets(1)
    
    With sheetToCopy
        LastInputRow = .Cells(.Cells.Rows.Count, "A").End(xlUp).Row
        LastInputColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    End With
    
    CopyRange = SheetToCopy.Range(SheetToCopy.Cells(LastInputRow, 1), SheetToCopy.Cells(1, LastInputColumn))
    
    'Check if any existing data
    Dim destWS  As Worksheet
    If Len(wkbDest.Sheets(G7_Tab_Name).Range("A1").Value) > 0 Then
        wkbDest.Sheets(G7_Tab_Name).UsedRange.ClearContents
        Set destWS = wkbDest.Sheets(G7_Tab_Name)
        CopyRange.Copy Destination:=destWS.Range(destWS.Cells(LastInputRow, 1), destWS.Cells(1, LastInputColumn))
    Else
        CopyRange.Copy Destination:=destWS.Range(destWS.Cells(LastInputRow, 1), destWS.Cells(1, LastInputColumn))
    End If
    
    End Sub
    

    你这样做的地方,但不是到处都是 . 即:
    LastInputRow = wkbSource.Sheets(1).Cells(Cells.Rows.Count, "A").End(xlUp).Row

    你正确告诉第一个 Cells() 要使用哪个工作表,但VBA没有"remember"任何东西,所以下一个 Cells.Rows.Count 将在活动工作表上,这不一定是你的 wkbSource.Sheets(1) 工作表 . 将该行更改为

    LastInputRow = wkbSource.Sheets(1).Cells(wkbSource.Sheets(1).Cells.Rows.Count, "A").End(xlUp).Row

    (注意:这可能无法解决您的问题,但它绝对有助于收紧代码并为更少的错误留出空间) .

    编辑:尝试用此替换你的 CopyRange

    With sheetToCopy
    copyRange = .Range(.Cells(1, 1), .Cells(LastInputRow, LastInputColumn))
    End With
    

    这将产生从A1到最后一列中最后一行的范围 .

    编辑2:你发布的代码 CopyRange 的代码并不清楚,但是你应该这样做:

    Dim CopyRange as Range
    Set CopyRange = .Range(.Cells(1, 1), .Cells(LastInputRow, LastInputColumn))
    

相关问题