我是VBA的新手,我一直在试图弄清楚如何打开输入文件并将这些内容复制到我工作簿上的隐藏工作表中,我看到了一个如何执行此操作但行数的示例和列是静态的,我必须使用3个不同的输入文件,其中一个不断变化,所以虽然我一直试图用我的直觉来解决这个问题,但我似乎找不到答案。 / p>
这是:
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
如果这是一个愚蠢的noob问题,我提前道歉,但老实说,我放弃了,不知道还有什么可以让它发挥作用。
问题是我不知道如何让它选择第一行到最后一行和第一个到最后一个单元格(非空白:单元格和行)
试过这个:
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
答案 0 :(得分:3)
这样的事情:
SourceSheet.UsedRange.Copy TargetSheet.Range("A1")