VBA-导入宏-导入时如何添加到当前数据?

时间:2019-05-14 00:33:43

标签: excel vba

我创建了一个宏来从另一个工作簿导入数据,但是我必须设置要开始的行。我想知道如何更改将导入数据粘贴到最后一个空行的代码(以便将其添加到表中)。

当前宏会提示您单击要导入的文件,然后将数据从特定行从源WB导入到目标WB。但是正如您所看到的,已将其分配给要粘贴的行。我的问题是如何将其粘贴到最后一个空行,因此它是在收集数据,而不是覆盖数据。

我是否需要更改“ targetSheet.Range(” R2“,” R4000“)。Value = sourceSheet.Range(” Q2“,” Q4000“)。Value”

Public Sub Extract_Excel_file()

''//--------------------------------------------
' 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)

' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = ThisWorkbook.Sheets("Raw Data")
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
sourceSheet.Range("A2", "V400").NumberFormat = "@" ''//Set all cells to 
text format.

' sourceSheet.Range("A2", "A4000").NumberFormat = "@" ''//Set all cells 
to text format.
targetSheet.Range("B2", "B4000").Value = sourceSheet.Range("A2", 
"A4000").Value
targetSheet.Range("C2", "C4000").Value = sourceSheet.Range("B2", 
"B4000").Value
targetSheet.Range("D2", "D4000").Value = sourceSheet.Range("C2", 
"C4000").Value
targetSheet.Range("E2", "E4000").Value = sourceSheet.Range("D2", 
"D4000").Value
targetSheet.Range("F2", "F4000").Value = sourceSheet.Range("E2", 
"E4000").Value
targetSheet.Range("G2", "G4000").Value = sourceSheet.Range("F2", 
"F4000").Value
targetSheet.Range("H2", "H4000").Value = sourceSheet.Range("G2", 
"G4000").Value
targetSheet.Range("I2", "I4000").Value = sourceSheet.Range("H2", 
"H4000").Value
targetSheet.Range("J2", "J4000").Value = sourceSheet.Range("I2", 
"I4000").Value
targetSheet.Range("K2", "K4000").Value = sourceSheet.Range("J2", 
"J4000").Value
targetSheet.Range("L2", "L4000").Value = sourceSheet.Range("K2", 
"K4000").Value
targetSheet.Range("M2", "M4000").Value = sourceSheet.Range("L2", 
"L4000").Value
targetSheet.Range("N2", "N4000").Value = sourceSheet.Range("M2", 
"M4000").Value
targetSheet.Range("O2", "O4000").Value = sourceSheet.Range("N2", 
"N4000").Value
targetSheet.Range("P2", "P4000").Value = sourceSheet.Range("O2", 
"O4000").Value
targetSheet.Range("L2", "L4000").Value = sourceSheet.Range("P2", 
"P4000").Value
targetSheet.Range("Q2", "Q4000").Value = sourceSheet.Range("L2", 
"L4000").Value
targetSheet.Range("R2", "R4000").Value = sourceSheet.Range("Q2", 
"Q4000").Value



' Close customer workbook
Application.DisplayAlerts = False ''//Don't promt to Save
customerWorkbook.Close
Application.DisplayAlerts = True   '' undo Don't promt to Save

End Sub

我只希望它收集数据,而不是每个月覆盖一次。

0 个答案:

没有答案