0
我是VBA新手,我一直在努力弄清楚如何打开输入文件并将这些内容复制到工作簿上的隐藏表格中,我看到一个示例关于如何做到这一点,但行和列的数量是静态的,我必须使用3个不同的输入文件,其中一个不断变化,尽管我一直试图用我的直觉了解如何做到这一点,但我可以似乎没有找到答案。使用VBA选择所有非空白行和单元格
这里,它是:
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
这样一个简单的答案,我觉得有点像个白痴。非常感谢你:) – Splendonia
不要流汗 - 一旦你知道它存在,一切都很容易。 :)只是很高兴它的工作! –