Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add one day to date in cells using VBA

Tags:

excel

vba

I have a macro set up that will clear content on a spreadsheet. At the end of this Macro, I want to select specific cells that already have dates in them and then replace the current dates with current date +1. After searching the web I found the DateAdd function, but I am pretty new to VBA and I am having difficulty writing the function correctly. After selecting the necessary cells, how would I go about changing the dates to the next day?

like image 707
Thad Avatar asked Nov 16 '11 17:11

Thad


People also ask

How do you add days to a date in VBA Excel?

Use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now. To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").

How do I use a day function in Excel VBA?

This example uses the Day function to obtain the day of the month from a specified date. In the development environment, the date literal is displayed in short format using the locale settings of your code. Dim MyDate, MyDay MyDate = #February 12, 1969# ' Assign a date. MyDay = Day(MyDate) ' MyDay contains 12.

How do I increment a date in Excel?

Auto fill a date series in Excel Filling a column or row with dates that increment by one day is very easy: Type the initial date in the first cell. Select the cell with the initial date and drag the fill handle (a small green square at the bottom-right corner) down or to the right.

What is Datevalue in Excel VBA?

The VBA DATEVALUE function is listed under the date category of VBA functions. When you use it in a VBA code, it can return date from a text representing a date. In simple words, it can convert a date into an actual date which is stored as a text. If there a time with that date it will ignore it.


1 Answers

Taking your question literally, you could do this:

' Here goes the code where you select the date cells you want to increment
' ...
' Now increment them by 1 day:
Dim cell As Range
For Each cell In Selection
    cell.Value = cell.Value + 1 ' adds 1 day
Next cell

The unit of the Date data type is a day. So adding 1 adds one day.

like image 145
Jean-François Corbett Avatar answered Sep 24 '22 01:09

Jean-François Corbett