我正在尝试使用命令按钮将工作簿1中的三个范围(工作簿1中的A,B和C列)复制到工作簿2中的B,C和G列,而不必打开目标工作簿。这里,WB1的A列进入B WB2列,WB1的B列进入C WB2列,WB1的C列进入G WB2列。
我已经能够使用以下代码将A和B复制并粘贴到B和C中,但是如果不使用不同的命令按钮就无法弄清楚如何将C转换为G. 单击它时,我需要按钮来完全更新目标工作表中的列。 这就是我对前两列的看法:
Private Sub CommandButton1_Click()
ActiveSheet.Range("A2:B250").Copy
Workbooks.Open Filename:="C:\Users\og677\Desktop\N
\ Matlab的\ VehicleList1.xlsx” ActiveSheet.Cells(2,2)。选择 Selection.PasteSpecial Paste:= xlPasteAll,Operation:= xlNone,skipblanks:= False,Transpose:= False ActiveWorkbook.Save ActiveWorkbook.Close Application.CutCopyMode = False
End Sub
我以为我可能需要设置范围,所以我尝试了这样的事情,但我不知道如何并且在我尝试设置wb2时出错。
Private Sub CommandButton1_Click()
Dim O As Workbook
Dim wb2 As Workbook
Dim ESN As Worksheet
Dim List As Worksheet
Dim I As Integer
Dim n As Integer
Set O = ThisWorkbook
Set wb2 = Workbooks("C:\Users.xlsx")
Set ESN = O.Sheets("ESN Regression")
Set List = VehicleList.Sheets("Sheet1")
n = 2
For I = 2 To WorksheetFunction.CountA(O.Columns.EntireColumn(1))
If Cells(I, "I").Value = "Yes" Then
List.Cells(n, "B").Value = ESN.Cells(I, "A")
List.Cells(n, "C").Value = ESN.Cells(I, "B")
List.Cells(n, "G").Value = ESN.Cells(I, "C")
n = n + 1
End If
Next
End Sub
我希望能够保留我的第一次尝试,但是我可以接受任何改变。
提前致谢!
答案 0 :(得分:1)
以下是我提出的建议:
Private Sub CommandButton1_Click()
ActiveSheet.Range("A2:B250").Copy
Workbooks.Open Filename:="C:\Users\og677\Desktop\N\Matlab\VehicleList.xlsx"
ActiveSheet.Cells(2, 2).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, skipblanks:=False, Transpose:=False
Application.CutCopyMode = False
ThisWorkbook.Activate
ActiveSheet.Range("C2:C250").Copy
Windows("VehicleList.xlsx").Activate
ActiveSheet.Cells(2, 7).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, skipblanks:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Save
End Sub
答案 1 :(得分:0)
您可以指定要粘贴的范围。 这就是我在宏中从wbTarget列A复制到wbThis列B
的方法wbTarget.Sheets("sheet1").Range("A14:A100").Copy
wbThis.Activate
wbThis.Sheets("Sheet1").Range("B14:B100").PasteSpecial Paste:=xlPasteValues
在同一命令调用的更多步骤中拆分复制/粘贴,以便将A和B粘贴到A和B,同时将C粘贴到G
答案 2 :(得分:-1)
试试这段代码。
Private Sub CommandButton1_Click()
Workbooks.Open Filename:="enter file name.xlsx"
'Column A
Windows("Workbook1.xlsm").Activate
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Workbook2.xlsx").Activate
Range("B1").Select
ActiveSheet.Paste
'Column B
Windows("Workbook1.xlsm").Activate
Range("B1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Workbook2.xlsx").Activate
Range("C1").Select
ActiveSheet.Paste
'Column C
Windows("Workbook1.xlsm").Activate
Range("C1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Workbook2.xlsx").Activate
Range("G1").Select
ActiveSheet.Paste
Range("A1").Select
End Sub