我在尝试将其他电子表格中的数据导入主电子表格时遇到了一些麻烦。 我打算打开16个电子表格,从每个电子表格中获取一个数据块并将其粘贴到主电子表格中。然后我想把我的细胞参考移动12行然后重复。 这是我遇到麻烦的最后一部分 - 我正在尝试使用偏移但它似乎没有移动单元格。所有发生的事情都是数据覆盖了第一个。 PasteStart实际上在第二个循环中显示为空白值
Sub ImportData()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range
Dim count As Integer
Dim name As String
Set wb1 = ActiveWorkbook
Set PasteStart = [Sheet1!A1]
PasteStart = Application.InputBox(prompt:="ENTER a Single Cell Address to paste data or leave it blank to exit", Type:=2)
If PasteStart = "" Then
Exit Sub
Else
For count = 1 To 16
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a Report to Parse", _
FileFilter:="Report Files *.xlsx (*.xlsx),")
If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "ERROR"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)
Sheets("JourneyPlanner").Select
PasteStart.Value = Application.InputBox(prompt:="Who's data are you inputting?", Type:=2)
ActiveSheet.Range("B132:B139").Copy
PasteStart.Offset(1, 0).PasteSpecial xlPasteValues
ActiveSheet.Range("C132:C139").Copy
PasteStart.Offset(1, 1).PasteSpecial xlPasteValues
ActiveSheet.Range("D132:D133").Copy
PasteStart.Offset(1, 2).PasteSpecial xlPasteValues
ActiveSheet.Range("E132:E133").Copy
PasteStart.Offset(1, 3).PasteSpecial xlPasteValues
End If
Row = Row + 12
PasteStart = PasteStart.Offset(12, 0)
wb2.Close
Next count
End If
End Sub
有什么想法吗? 提前致谢
答案 0 :(得分:0)
PasteStart = PasteStart.Offset(12,0)
会将PasteStart
的值设置为等于PasteStart.Offset(12,0)
的值。您需要使用Set
关键字来表示您要将变量分配给新范围。
试试这个:Set PasteStart = PasteStart.Offset(12,0)