Snabelhund
New Member
- Joined
- Nov 11, 2021
- Messages
- 20
- Office Version
- 2016
- Platform
- Windows
I have a macrorelated problem in Excel VBA, where I'm trying to copy a range from one workbook (source) and paste it in a sheet (sh1) in column C in another workbook (target) (there Is already some information in column A in the target workbook.)
However my code seems to be working as far as to the paste step. It seems as the macro quits rather than pastes the content and I'm not getting any error messages and when debugging I really cant identify the problem.
The code is placed in the open workbook named target and the data is text based. Also it is possible for me to manually paste the copied range. so it seems that the copied content i stored to the clipboard.
Excuse me for any spelling errors... english is not my first language. Also i´m somewhat new to VBA, so the code a a regeneration off bits found online. So apoligies if it is related to a simple beginners error..
see code below:
However my code seems to be working as far as to the paste step. It seems as the macro quits rather than pastes the content and I'm not getting any error messages and when debugging I really cant identify the problem.
The code is placed in the open workbook named target and the data is text based. Also it is possible for me to manually paste the copied range. so it seems that the copied content i stored to the clipboard.
Excuse me for any spelling errors... english is not my first language. Also i´m somewhat new to VBA, so the code a a regeneration off bits found online. So apoligies if it is related to a simple beginners error..
see code below:
VBA Code:
Sub CopyFromWorkbook()
' Define variables'
Dim wb1 As Workbook, wb2 As Workbook
' Disable screen updating to reduce screen flicker'
Application.ScreenUpdating = False
' Define which workbook is which
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("H:\source.xlsx")
' Copy range A1:D1000 from the Data sheet of wb2
wb2.Sheets("sourceData").Range("A1:D7000").Copy
'to avoid clipboard prompt'
Application.DisplayAlerts = False
' Paste the copied data to the column C of the target sheet in wb1'
wb1.Sheets("target").Activate
'the code runs fine until here'
ActiveSheet.Paste Destination:=wb1.Sheets("target").Range("C1")
' Close wb2
wb2.Close
Application.DisplayAlerts = True
' Re-enable screen updating
Application.ScreenUpdating = True
End Sub