我当前正在做一个报告,其中我将基于在特定单元格中指示的链接/路径和文件名来获取报告,并且应在我也指示的特定工作表中传输数据。这是必需的,因为路径总是根据用户保存文件的位置而变化。
第一件事是,我具有“列表”选项卡,在其中输入了每个文件的文件名和完整路径以及应粘贴到的位置。例如对于文件A应该粘贴到“ MasterData”工作表。文件B,应该在下一个标签中,文件C应该在其他标签中。
然后,当我使用vba时,它将获取我的文件。文件已复制到“ MasterData”工作表,但是第二个和第三个文件已复制到文件A的数据下,这是我的问题。
第二个文件和第三个文件也是文本格式,但是我希望首先弹出“文本向导”,以便用户在将数据复制到指定的图纸之前可以选择是定界还是定宽。
我无法在此处附加文件:(
这是我当前的VBA代码:
Public strFileName As String
Public currentWB As Workbook
Public dataWB As Workbook
Public strCopyRange As String
Sub GetData()
Dim strWhereToCopy As String, strStartCellColName As String
Dim strListSheet As String
strListSheet = "List"
On Error GoTo ErrH
Sheets(strListSheet).Select
Range("B2").Select
'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
Set currentWB = ActiveWorkbook
Do While ActiveCell.Value <> ""
strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value
strCopyRange = ActiveCell.Offset(0, 2) & ":" & ActiveCell.Offset(0, 3)
strWhereToCopy = ActiveCell.Offset(0, 4).Value
strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1)
Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=False
Set dataWB = ActiveWorkbook
Range(strCopyRange).Select
Selection.Copy
currentWB.Activate
Sheets("MasterData").Select
lastRow = LastRowInOneColumn(strStartCellColName)
Cells(lastRow + 1, 1).Select
Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
Application.CutCopyMode = False
dataWB.Close False
Sheets("List").Select
ActiveCell.Offset(1, 0).Select
Loop
Exit Sub
ErrH:
MsgBox "It seems some file was missing. The data copy operation is not complete."
Exit Sub
End Sub
Public Function LastRowInOneColumn(col)
'Find the last used row in a Column: column A in this example
'http://www.rondebruin.nl/last.htm
Dim lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
LastRowInOneColumn = lastRow
End Function
答案 0 :(得分:0)
我怀疑要解决粘贴问题,该行应为:
Sheets("Master Data").select
应阅读
Sheets(strWhereToCopy).select