VBA to copy multiple columns if cells contain data

timlh42

Board Regular
Joined
Sep 27, 2017
Messages
76
I have a macro that opens another workbook(Stats) and pastes 4 columns of data into columns N, O, P, Q on Sheet1 which works great.
I am trying to write a worksheet change event in sheet1 that will copy only the rows that contain data in those 4 columns to the next blank row in sheet5
This code works fine if I copy and paste the data into columns N,O,P,Q on sheet1 but if I let the macro open the workbook and paste the data then it shows an error in this line

If wshS.Range("N" & s).Value <> "" Then


Here is the code that I have so far. Any ideas?

Dim wshS As Worksheet
Dim wshT As Worksheet
Dim s As Long
Dim m As Long
Dim t As Long
Set wshS = Worksheets("Sheet1")
m = wshS.Range("N" & wshS.Rows.Count).End(xlUp).Row
Set wshT = Worksheets("Sheet5")
t = wshT.Range("A" & wshT.Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For s = 1 To m
If wshS.Range("N" & s).Value <> "" Then
wshS.Range("N" & s).Resize(ColumnSize:=13).Copy
t = t + 1
wshT.Range("A" & t).PasteSpecial Paste:=xlPasteValues
End If
Next s
Application.CutCopyMode = False
Application.ScreenUpdating = True
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
.
The macro code you posted works fine here. Not certain what is causing the error on your end.
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,652
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top