Trouble shoot vba code fine last cell

jpsanicky

New Member
Joined
Sep 22, 2018
Messages
12
Greetings all;
I have a find last column to begin pasting data in Col A:X (returned row)

The code is used earlier in the same procedure with no issue
When I use the code a second time it errors out at Cell.Select

Data is already in All Orgs Prod.xlsx A2:A;500

Here is the code for the first use;
Call P_Org_Update 'opens prod and non prod
Workbooks("P_Org.xlsx").Activate

LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:W" & LastRow).Select
Selection.Copy

Workbooks("All Orgs Prod Update.xlsx").Activate

' Find last row


Dim ws As Worksheet
Set ws = ActiveSheet


For Each cell In ws.Columns(1).Cells
If Len(cell) = 0 Then cell.Select: Exit For
Next cell

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

Workbook All Orgs Prod.xlsx is properly populated


Second use of code;

Workbooks("P NON Prod Update.xlsx").Activate

LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:W" & LastRow).Select
Selection.Copy

Workbooks("All Orgs Prod Update.xlsx).Activate

' Find last row


' Dim ws As Worksheet remarked out since dim already in routine
' Set ws = ActiveSheet remarked out since dim already in routine


For Each cell In ws.Columns(1).Cells
If Len(cell) = 0 Then cell.Select: Exit For This where the routine stops??
Next cell

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

What am i doing wrong??

Thanks JPS
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Uncomment the line beginning with Set.
 
Upvote 0
Happy it helped (but not the way I would find the next blank cell/ Last Row in a column though)
 
Last edited:
Upvote 0
Hi Mark858, Thanks again. If you have a more efficient way I'm eager to learn and maybe update my project.
 
Upvote 0
Try replacing all of...

Code:
Workbooks("P NON Prod Update.xlsx").Activate

LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:W" & LastRow).Select
Selection.Copy

Workbooks("All Orgs Prod Update.xlsx).Activate

' Find last row


' Dim ws As Worksheet remarked out since dim already in routine
' Set ws = ActiveSheet remarked out since dim already in routine


For Each cell In ws.Columns(1).Cells
If Len(cell) = 0 Then cell.Select: Exit For This where the routine stops??
Next cell

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

with the (untested) code below

Code:
    With Workbooks("P NON Prod Update.xlsx").Sheets("Sheet1")
        .Range("A2:W" & .Range("A" & Rows.Count).End(xlUp).Row).Copy
        Workbooks("All Orgs Prod Update.xlsx").Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)(2).PasteSpecial xlValues
    End With
    Application.CutCopyMode = False

Test on copies of your workbooks
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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