我是 VBA 新手,我一直在尝试弄清楚如何打开输入文件并将这些内容复制到工作簿上的隐藏工作表中,我看到了一个有关如何执行此操作的示例,但行数和列数是静态的,我必须使用 3 个不同的输入文件,其中一个会不断变化,因此,尽管我一直在尝试使用我的直觉来了解如何做到这一点,但我似乎无法找到答案。
这是它:
Sub s()
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
Dim lastRow As Long
lastRow = sourceSheet.Range("A" & Rows.Count).End(xlUp).Row
Dim lastColumn As Long
lastColumn = sourceSheet.Cells(1, Columns.Count).End(xlToLeft).Column
targetSheet.Range("A1:A" & lastRow).Value = sourceSheet.Range("A1:A" & lastRow).Value
' Close customer workbook
customerWorkbook.Close
End Sub
我正在使用 EXCEL 2007
如果这是一个愚蠢的菜鸟问题,我提前道歉,但老实说,我放弃了,不知道还能做些什么来让它发挥作用。
问题是我不知道如何让它选择第一个到最后一行和第一个到最后一个单元格(非空白:单元格和行)
试过这个:
targetSheet.Range("A1:A" & lastRow).End(xlUp).Value = sourceSheet.Range("A1:A" & lastRow).End(xlUp).Value
and this targetSheet.Range("A1:A" & lastRow).End(xlRight).Value = sourceSheet.Range("A1:A" & lastRow).End(xlRight).Value