复制已关闭工作簿列中的内容

时间:2018-07-18 15:17:32

标签: excel vba excel-vba

正如标题中所述,我正尝试将列中的数据复制到新工作簿中,因为它是每周报告,其中我在此列中添加的数据仍然有效。

Sub copyColData00()

Dim lastRow As Long

Dim myApp As Excel.Application

Dim wkBk As Workbook

Dim wkSht As Object

Dim mnt As String



Set myApp = CreateObject("Excel.Application")

mnt = InputBox("Enter Filename")

Set wkBk = Workbooks.Open("\\n\Documents\" & mnt & ".xlsx")

lastRow = wkBk.Sheets(1).Range("R" & Rows.Count).End(xlUp).Row

wkBk.Sheets(1).Range("R1:R" & lastRow).Copy

myApp.DisplayAlerts = False

wkBk.Close

myApp.Quit

Set wkBk = Nothing

Set myApp = Nothing

Set wkBk = ActiveWorkbook

Set wkSht = wkBk.Sheets("Sheet1")

wkSht.Activate

Range("R1").Select

wkSht.Paste

Exit Sub



End Sub

我的问题是我希望它直接粘贴到那里,然后出现提示询问我是否要复制剪贴板中的所有数据,而我的第二个问题是  设置wkSht = wkBk.Sheets(“ Sheet1”) 它使我的错误下标超出范围,如果有人可以帮助我,我将无法理解那里发生的情况,这将是很好的!

1 个答案:

答案 0 :(得分:0)

这是更简单的方法:

Sub copyColData00()
    Dim lastRow As Long
    Dim wkBk1 As Workbook, wkBk2 As Workbook
    Dim wkSht As Object
    Dim mnt As String

    mnt = InputBox("Enter Filename")
    Set wkBk1 = ActiveWorkbook
    Set wkBk2 = Workbooks.Open("\\n\Documents\" & mnt & ".xlsx")
    lastRow = wkBk2.Sheets(1).Range("R" & Rows.Count).End(xlUp).Row

    wkBk1.Sheets(1).Range("R1:R" & lastRow).Value = wkBk2.Sheets(1).Range("R1:R" & lastRow).Value 'change which sheet you want for wkBk1

    wkBk2.Close
End Sub