If then statement not working

mbkinzer

New Member
Joined
Jan 12, 2021
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
Hello,

My current macro works but I really need it to be an IF THEN statement. It currently looks at Sheet "RTW Report" range U2 to last row minus 2 and copies any cells with values into another sheet called "Charges" into C18. It then does the same thing for Column V on the "RTW Report" sheet and pastes those values under the last row in column C on the "Charges" sheet.

However, sometimes there are no values in Columns U or V and if so it is currently bombing out. I think an IF then statement will work but I am having trouble with it. Thanks!

Current macro:

Dim lr As Long

Sheets("RTW Report").Activate
lr = Cells(Rows.Count, "U").End(xlUp).Row
Sheets("RTW Report").Range("U2:U" & lr - 2).SpecialCells(xlCellTypeConstants, xlNumbers).Copy Sheets("Charges").Range("C18")

Dim lr2 As Long

Sheets("RTW Report").Activate
lr2 = Cells(Rows.Count, "V").End(xlUp).Row
Sheets("Charges").Activate
Sheets("RTW Report").Range("V2:V" & lr2 - 2).SpecialCells(xlCellTypeConstants, xlNumbers).Copy Sheets("Charges").Range(Cells((CHARGE1 + 18), 3), Cells((CHARGE1 + 18), 3))
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
However, sometimes there are no values in Columns U or V and if so it is currently bombing out. I think an IF then statement will work but I am having trouble with it. Thanks!
What is the value in lr in those cases?

Maybe you need something like:
VBA Code:
If lr >= 4 Then
    Sheets("RTW Report").Range("U2:U" & lr - 2).SpecialCells(xlCellTypeConstants, xlNumbers).Copy Sheets("Charges").Range("C18")
End If
 
Upvote 0
What is the value in lr in those cases?

Maybe you need something like:
VBA Code:
If lr >= 4 Then
    Sheets("RTW Report").Range("U2:U" & lr - 2).SpecialCells(xlCellTypeConstants, xlNumbers).Copy Sheets("Charges").Range("C18")
End If[/CO
[/QUOTE]
 
Upvote 0
You are welcome.

I knew it was a number, I was just wondering if it actually correctly identified when there is no data.
 
Upvote 0
You are welcome.

I knew it was a number, I was just wondering if it actually correctly identified when there is no data.
Sorry, actually I ran it with data in those cells. I just reran and it is still bombing out when there are no values.
 
Upvote 0
Sorry, actually I ran it with data in those cells. I just reran and it is still bombing out when there are no values.
OK, tell us exactly what "lr" is returning in that situation.

You can add a line of code like this, if you aren't sure how to get it from your code:
Rich (BB code):
lr = Cells(Rows.Count, "U").End(xlUp).Row
MsgBox lr

Also, is it erroring out on that line, or a line below lt (hit "Debug" and see which line of code it is highlighting)?
You may need to apply the same logic for "lr2".
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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