从打开的工作簿的指定 sheet 复制数据并将其粘贴到关闭的工作簿的另一个指定的 sheet
Copy data from specified sheet of opened workbook and paste it to another specified sheet of closed workbook
我想将数据从打开的工作簿中指定的 sheet 复制到关闭的工作簿中的另一个指定的 sheet。
我有这样的代码:
Private Sub CommandButton1_Click()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("B22:AF25").Copy
wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
wsCopy.Range("B26:AF30").Copy
wsDest.Range("G" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Selection.ClearContents
wsDest.Parent.Close True
End Sub
其实是复制了数据,只是要先打开目标文件,然后自动关闭。
同样只复制了第一个数据(这个:wsCopy.Range("B22:AF25").Copy
),第二个数据没有复制(那个:wsCopy.Range("B26:AF30").Copy
)。
这里:
Option Explicit
Private Sub CommandButton1_Click()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRowB As Long
Dim lDestLastRowG As Long
'It is not possible to get data from a workbook without openning it, but this is the closest
With Application
.ScreenUpdating = False 'won't show any changes on your screen
.Visible = Flase 'will hide Excel. CAUTION: if the macro fails make sure to change this back to True
End With
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
With wsDest 'with allows you to refer to the variable without writting it
lDestLastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row + 1 'You can use also +1 since it's a number
'you are not copying the same amount of rows, so maybe the data won't be on the same row on both columns
lDestLastRowG = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
End With
'3. Copy & Paste Data
wsCopy.Range("B22:AF25").Copy
wsDest.Range("B" & lDestLastRowB).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
wsCopy.Range("B26:AF30").Copy
wsDest.Range("G" & lDestLastRowG).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'when you manually copy and paste you will see that the paste range gets selected. This line erases selected content so this is deleting it
'Selection.ClearContents
wsDest.Parent.Close True
With Application
.ScreenUpdating = True
.Visible = True
End With
End Sub
我想将数据从打开的工作簿中指定的 sheet 复制到关闭的工作簿中的另一个指定的 sheet。
我有这样的代码:
Private Sub CommandButton1_Click()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("B22:AF25").Copy
wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
wsCopy.Range("B26:AF30").Copy
wsDest.Range("G" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Selection.ClearContents
wsDest.Parent.Close True
End Sub
其实是复制了数据,只是要先打开目标文件,然后自动关闭。
同样只复制了第一个数据(这个:wsCopy.Range("B22:AF25").Copy
),第二个数据没有复制(那个:wsCopy.Range("B26:AF30").Copy
)。
这里:
Option Explicit
Private Sub CommandButton1_Click()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRowB As Long
Dim lDestLastRowG As Long
'It is not possible to get data from a workbook without openning it, but this is the closest
With Application
.ScreenUpdating = False 'won't show any changes on your screen
.Visible = Flase 'will hide Excel. CAUTION: if the macro fails make sure to change this back to True
End With
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
With wsDest 'with allows you to refer to the variable without writting it
lDestLastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row + 1 'You can use also +1 since it's a number
'you are not copying the same amount of rows, so maybe the data won't be on the same row on both columns
lDestLastRowG = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
End With
'3. Copy & Paste Data
wsCopy.Range("B22:AF25").Copy
wsDest.Range("B" & lDestLastRowB).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
wsCopy.Range("B26:AF30").Copy
wsDest.Range("G" & lDestLastRowG).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'when you manually copy and paste you will see that the paste range gets selected. This line erases selected content so this is deleting it
'Selection.ClearContents
wsDest.Parent.Close True
With Application
.ScreenUpdating = True
.Visible = True
End With
End Sub