Subtracting Dates to Last Row

loract

New Member
Joined
Apr 21, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
One of the things I am trying to do with a file is Subtract 10 days from a column A called "OUTDATE" and put the resulting date in Column J. This is how I am working it just now:

At the beginning of the sub I use this:

VBA Code:
lR = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row

'and then in the section where I am subtracting the dates, I use this:
 Dim i As Integer
   For i = 2 To lR
 
        Range("J" & i) = DateAdd("d", -10, Range("A" & i))
    Next i


This gives me an "Application-defined or object-defined error" and highlights the "Range" line. HOWEVER, the dates actually do work all the way to the last row. So it's almost like it does what I want it to do but then gives up. This is the last action of many that the macro does, so the next line is End Sub.

If I change the "For i" line to For i = 2 to 60 or some other number, the macro works fine as long as there are sixty columns or more (but it only gives me dates for rows 2 to 60). But if there are less than sixty it throws an error. I really need it to find the last row.

I'd appreciate any help!
Thanks!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What is your value of lR?
What if you change this line:
VBA Code:
Dim i as Integer
to this:
VBA Code:
Dim i as Long
 
Upvote 0
What is your value of lR?
What if you change this line:
VBA Code:
Dim i as Integer
to this:
VBA Code:
Dim i as Long
I gave that a try with the same result. It works but then throws up the same error.
 
Upvote 0
I gave that a try with the same result. It works but then throws up the same error.
This is how I define the last row at the beginning of the Sub:

lR = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
 
Upvote 0
This is how I define the last row at the beginning of the Sub:

lR = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
I know, but what exactly is the value it is being set?
You can add a simple MsgBox after that line to show the value, i.e.
VBA Code:
lR = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
MsgBox lR
 
Upvote 0
I know, but what exactly is the value it is being set?
You can add a simple MsgBox after that line to show the value, i.e.
VBA Code:
lR = Cells.Find("*", Cells(1, 1), xlFormulas, xlPart, xlByRows, xlPrevious, False).Row
MsgBox lR
Ah. It came up with 67. Which is the correct number of rows.
 
Upvote 0
Ah. It came up with 67. Which is the correct number of rows.
Does every cell in the range A2:A67 have a valid date in it?
Any blanks, text entries, or errors anywhere in that range?
 
Upvote 0
Does every cell in the range A2:A67 have a valid date in it?
Any blanks, text entries, or errors anywhere in that range?
Well, the last three rows ARE blank. This is bc the very first thing I do in the macro is delete the first three rows, since the worksheet is generated by a report from our software. Would it help if I deleted the rows first and THEN defined the last row? I use lr in other parts of the macro and it doesn't seem to cause trouble.
 
Upvote 0
Maybe add a line that checks for a value in column before doing the math, i.e.
VBA Code:
   For i = 2 To lR
        If Range("A" & i).Value > 0 Then Range("J" & i) = DateAdd("d", -10, Range("A" & i))
    Next i
 
Upvote 0
Solution
Maybe add a line that checks for a value in column before doing the math, i.e.
VBA Code:
   For i = 2 To lR
        If Range("A" & i).Value > 0 Then Range("J" & i) = DateAdd("d", -10, Range("A" & i))
    Next i
That did it!!
Thank you!! I appreciate your help very much!!
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,580
Members
449,039
Latest member
Arbind kumar

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