我目前有一个用于创建外部pdf表单的主工作簿。我当前的代码允许我浏览到一个文件夹并选择另一个工作簿来从中提取特定数据并将其粘贴到主文件中。
将数据从外部工作簿sheet1复制并粘贴到活动工作簿sheet1。我正在使用的代码是" For Each Sheet。"像魅力一样。
问题是我已经在工作簿中添加了另一张工作表并且它抛出了运行时错误1004.我需要对其进行重新编码,使其仅从外部工作簿中的sheet1复制并仅粘贴到sheet1主人(主动)。
有什么想法吗?感谢您提供的任何帮助。
Sub ImportData()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range
Dim sh As Worksheet
Dim strXLSFile As String, strPDFFile As String, strFolder As String
strFolder = "H:\Company Data\Firm Files\Client Data Workbooks\Excel Data-Client Info"
Set wb1 = ActiveWorkbook
Set PasteStart = [Client_Data]
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a Report to Parse", _
FileFilter:="Report Files *.xlsm (*.xlsm),")
If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)
For Each Sheet In wb2.Sheets
With Sheet.Cells.Range("B9:B27")
.Copy PasteStart
Set PasteStart = PasteStart.Offset(.Rows.Count)
End With
Next Sheet
End If
wb2.Close False
End Sub
答案 0 :(得分:0)
如果你只想要Sheet1然后摆脱循环。
而不是:
For Each Sheet In wb2.Sheets
With Sheet.Cells.Range("B9:B27")
.Copy PasteStart
Set PasteStart = PasteStart.Offset(.Rows.Count)
End With
Next Sheet
尝试:
wb2.sheets(1).range("B9:B27").copy PasteStart
似乎这是一次性使用所以不需要再次Set PasteStart =
。希望这有帮助