Edit VBA to another cell

phil133

Active Member
Joined
May 5, 2015
Messages
257
Office Version
  1. 365
Platform
  1. Windows
Hi. I'm trying to edit this macro
Code:
Sub copy_last_row100()
Dim ws1 As Worksheet:   Set ws1 = Sheets("P_L events up to 2 months")
Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet1")
Dim LR As Long
Dim WkRg  As Range
    With ws1
        'lastrow of sheet
        LR = ws1.Range("A4:A" & ws1.Range("A" & Rows.Count).End(xlUp).Row).Find("*", , xlValues, xlWhole, , xlPrevious).Row
        Set WkRg = Union(.Cells(LR, "B"), .Cells(LR, "C"), .Cells(LR, "G"), .Cells(LR, "P"))
    End With
    'Copy lastrow columns B,C,G,P  to sheet1
    WkRg.Copy
    ws2.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
    
    Application.CutCopyMode = False
End Sub

This works but when I add that I want to copy another cell Q it stops working. This is the VBA that I want to fix:
Code:
Sub copy_last_row100()
Dim ws1 As Worksheet:   Set ws1 = Sheets("P_L events up to 2 months")
Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet1")
Dim LR As Long
Dim WkRg  As Range
    With ws1
        'lastrow of sheet
        LR = ws1.Range("A4:A" & ws1.Range("A" & Rows.Count).End(xlUp).Row).Find("*", , xlValues, xlWhole, , xlPrevious).Row
        Set WkRg = Union(.Cells(LR, "B"), .Cells(LR, "C"), .Cells(LR, "G"), .Cells(LR, "P")), .Cells(LR, "Q")
    End With
    'Copy lastrow columns B,C,G,P,Q  to sheet1
    WkRg.Copy
    ws2.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
    
    Application.CutCopyMode = False
End Sub

Thanks for any help!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Phil,

Edit the Union line...

Code:
Set WkRg = Union(.Cells(LR, "B"), .Cells(LR, "C"), .Cells(LR, "G"), .Cells(LR, "P"), .Cells(LR, "Q"))

Hope that helps.
 
Upvote 0
Nevermind. I messed up with the parentheses. Sorry. Now it works.
 
Upvote 0

Forum statistics

Threads
1,203,521
Messages
6,055,885
Members
444,830
Latest member
Excelsmallbusinessmom

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