Need HELP in subtracting the number of days from the date in column A

AGU

New Member
Joined
Apr 24, 2021
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
Hi guys, I need help. I want to update my final report with the help of macro from the attached data to save my time. Actually I have to update this sheet consistently and manually it costs a lot of my time. Anybody who's there to help will be highly appreciated. Below is my attached VBA code...

Sub SubtractDate()

Dim LastRow As Long, i As Long
Dim myValue As Variant
myValue = InputBox("Subtraction of Days")
With Worksheets("Pre Data")
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 2 To LastRow
.Range("A" & i).Value = -myValue
Next i
End With

End Sub

my date format is HH:mm:ss

When user presses a button it'll ask how many days you wanna subtract. In the output that particular number subtract from date, which is present in column A2. Please help me out to correct above code.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
try this, I put a check to test that the enterd value is numerci too:
VBA Code:
Sub SubtractDate()

Dim LastRow As Long, i As Long
Dim myValue As Variant
myValue = InputBox("Subtraction of Days")
If IsNumeric(myValue) Then
With Worksheets("Pre Data")
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 2 To LastRow
.Range("A" & i).Value = .Range("A" & i).Value - myValue
Next i
End With
End If
End Sub
 
  • Like
Reactions: AGU
Upvote 0
Solution
try this, I put a check to test that the enterd value is numerci too:
VBA Code:
Sub SubtractDate()

Dim LastRow As Long, i As Long
Dim myValue As Variant
myValue = InputBox("Subtraction of Days")
If IsNumeric(myValue) Then
With Worksheets("Pre Data")
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For i = 2 To LastRow
.Range("A" & i).Value = .Range("A" & i).Value - myValue
Next i
End With
End If
End Sub
Thanks alot. It works awesome. God bless!!
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,838
Members
449,343
Latest member
DEWS2031

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