【问题标题】:Copy data from one workbook to another workboook [duplicate]将数据从一个工作簿复制到另一个工作簿[重复]
【发布时间】:2015-06-15 15:33:06
【问题描述】:

我正在尝试将数据从一个工作簿复制到另一个工作簿。我一直遇到的问题是,我从中复制的工作簿每次都会更改名称。

我将数据粘贴到的工作簿将保持不变。我已经知道如何获取数据了,我只是遇到了切换回要从中复制数据的工作簿的问题。

这是我目前所拥有的。

Sub grabdata()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim w As Workbook

w = ActiveWorkbook

lrow = ThisWorkbook.Sheets("Month").Cells(Rows.Count, 3).End(xlUp).Row
LoopEnd = lrow - 16

LRowTxtTab = 2

For x = 32 To LoopEnd

' Grabs Metrics from Month tab (This is the grab data from workbook and will       change everytime

ThisWorkbook.Sheets("Month").Select
GridEnd = x + 16
Range("D" & x, "V" & GridEnd).Copy


(this is the workbook that will stay the same all the time)
Workbooks("NRBSrub").Activate
Workbooks("NRBSrub").Worksheets("Sheet5").Select

Range("E" & LRowTxtTab).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True

' Grabs assocaited brands per metrics copied over above
BrandName = ThisWorkbook.Sheets("Month").Range("A" & x)
Sheets("Sheet5").Range("B" & LRowTxtTab, "B" & LRowTxtTab + 18) = BrandName

LRowTxtTab = LRowTxtTab + 19
x = x + 17

Next x

' Grabs FY time frames for FISC_MO column
LRowTxtTab = ThisWorkbook.Sheets("Sheet5").Cells(Rows.Count, 
5).End(xlUp).Row
ThisWorkbook.Sheets("Month").Range("D2:V2").Copy
Sheets("Sheet5").Range("C2").PasteSpecial Paste:=xlPasteValues,   
Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True

Application.ScreenUpdating = True`enter code here`
Application.DisplayAlerts = True

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我只是遇到了切换回要从中复制数据的工作簿的问题

    使用Set

    Dim w As Workbook: Set w = ActiveWorkbook
    

    然后你可以像这样在工作簿之间切换:

    w.Activate
    

    或者使用with方法

    With w
        lrow = .Sheets("Month").Cells(Rows.Count, 3).End(xlUp).Row
        'do whatever you need with w
    End with
    With Workbooks("NRBSrub")
        'do whatever you need with Workbooks("NRBSrub")
    End With
    

    【讨论】:

    • 没关系解决这个问题,谢谢你的帮助。
    • 那么,在运行宏之前你不知道工作表的名称?
    猜你喜欢
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2019-07-29
    相关资源
    最近更新 更多