我有一个包含数据的大型工作簿,我需要将特定列中的数据从预定数量的行复制到另一个工作簿中。数据将始终来自相同的列,但相关的行每天都会不同。
例如,我的工作簿有1000行和15列,今天我需要复制A,K和A列中单元格的内容。 J从第100行进入单元格B3,B2,& B1,然后分别来自A,K和A列的数据。 J从第101行进入细胞C3,C2和& C1,在新工作簿中。这是我到目前为止所做的:
Dim WB1 As Workbook
Dim WS1 As Worksheet
Dim SelectWB As Variant
Dim FilePath As Variant
Dim WB2 As Workbook
Dim Quantity As Integer
Dim x As Integer
Dim ColumnRef As Integer
Application.ReferenceStyle = xlR1C1
Set WB1 = ActiveWorkbook
Set WS1 = ActiveSheet
Quantity = InputBox("Input the number of rows to be copied")
Set SelectWB = Application.FileDialog(msoFileDialogFilePicker)
With SelectWB
.ButtonName = "Select"
.AllowMultiSelect = False
.Title = "Choose File"
.InitialView = msoFileDialogViewDetails
If .Show Then FilePath = .SelectedItems(1)
End With
Set WB2 = Workbooks.Open(FilePath)
ColumnRef = 2
For x = 1 To TagQuantity
If x <= TagQuantity Then
Selection.Copy
WB2.Sheets(3).Cells(3, ColumnRef).Select
Sheets(3).Paste
WB1.WS1.ActiveCell.Offset(0, 10).Select
'Selection.Copy
'WB2.ActiveCell.Offset(-1, 0).Paste
'WB1.WS1.ActiveCell.Offset(0, -1).Copy
'WB2.ActiveCell.Offset(-1, 0).Paste
'WB1.WS1.ActiveCell.Offset(1, -9).Select
'ColumnRef = ColumnRef + 1
End If
Next
Application.ReferenceStyle = xlA1
End Sub
我的代码错误出现在“WB1.WS1.ActiveCell.Offset(0,10).Select”行中(运行时错误'438':对象不支持此属性或方法)。如果我注释掉错误行并仅在if语句中运行前3行,则它不会崩溃,但也不会将值复制到新工作表中。我做错了什么?
我是编程新手,所以感谢任何帮助!
答案 0 :(得分:0)
尝试将其添加到:
Dim rng As Range
Set rng = ActiveCell
Application.GoTo rng
此处它已添加到您提供的代码中:
Dim WB1 As Workbook
Dim WS1 As Worksheet
Dim rng as Range
Dim SelectWB As Variant
Dim FilePath As Variant
Dim WB2 As Workbook
Dim Quantity As Integer
Dim x As Integer
Dim ColumnRef As Integer
Application.ReferenceStyle = xlR1C1
Set WB1 = ActiveWorkbook
Set WS1 = ActiveSheet
Set rng = ActiveCell
Quantity = InputBox("Input the number of rows to be copied")
Set SelectWB = Application.FileDialog(msoFileDialogFilePicker)
With SelectWB
.ButtonName = "Select"
.AllowMultiSelect = False
.Title = "Choose File"
.InitialView = msoFileDialogViewDetails
If .Show Then FilePath = .SelectedItems(1)
End With
Set WB2 = Workbooks.Open(FilePath)
ColumnRef = 2
For x = 1 To TagQuantity
If x <= TagQuantity Then
Selection.Copy
WB2.Sheets(3).Cells(3, ColumnRef).Select
Sheets(3).Paste
Application.Goto rng.Offset(0, 10)
'WB1.WS1.ActiveCell.Offset(0, 10).Select
'Selection.Copy
'WB2.ActiveCell.Offset(-1, 0).Paste
'WB1.WS1.ActiveCell.Offset(0, -1).Copy
'WB2.ActiveCell.Offset(-1, 0).Paste
'WB1.WS1.ActiveCell.Offset(1, -9).Select
'ColumnRef = ColumnRef + 1
End If
Next
Application.ReferenceStyle = xlA1
End Sub