Excel macro to create date coloms - excel-formula

I just want to create list of months automatically between start and end dates in which if the starting date or the end date have only dates then it comes as date from to in excel.

If A1 contains the starting date, and A2 is the enddate, then
=TEXT(IF(EDATE($A$1,ROW() - 5) <= $A$2, EDATE($A$1,ROW() - 5), ""), "mmmyy")
does what you ask for. Just apply it for sufficiently many cells.

Related

VBA keeps changing my date format to american

So I have this simple issue, my date format is inputted on sheet as dd/mm/yyyy, but when I use this code below, it keeps printing mm/dd/yyyy for cases when day is lesser than 12 (if date is inputted as 31/10/2022 for example, excel understands that first number is day, I'm assuming it's because there is no month 31). I want to stop this from happening, since I need my date to be on local configuration. For some reason, using .FormulaLocal is returning stacked overflow error.
Input: 01/07/2022 on range A2
20:08:00 on range K2
Code:
Cells(2, 24) = Application.WorksheetFunction.Text(Cells(2, 1), "dd/mm/yyyy") & " " & Application.WorksheetFunction.Text(Cells(2, 11), "hh:mm:ss")
Output: 01/07/2022 20:08:00

Excel MS Office- months beetween dates

I have a databes of dates with start date and end date.
I want to count only month beetween year 01/2005 and 01/2006.
e.g. from DB: Start 09/2004 end 05/2005, so I need only months from 01/2005 to 05/2005.
I tried some if and lookup Excel funkctions but didn't work.
Can somebody help?
https://ibb.co/jXJHYQ
Assuming a start date is on cell F10 and the end date is on cell F11, you can get the number of months by typing this into any cell:
"=(YEAR(F11)-YEAR(F10))*12+MONTH(F11)-MONTH(F10)"

converting 'mm/dd to dd/mm/yyyy

I've got a whole column of 1000+ entries in excel that has the date keyed in as '01/02, '01/03, '01/04... representing Jan 02, Jan 03, Jan 04 and so on as the person was trying to maintain a mm/dd format while saving on the column width.
This has become somewhat troublesome since the entries are all strings instead of dates and I need to input years in now to get dd/mm/yyyy.
Does anyone know how to go about doing this (other than of course manually changing each of the 1000+ entries?)
Also, I would appreciate it if you can share how I can possibly show dd/mm while retaining dd/mm/yyyy on the entry in excel?
Thank you so much for looking at this.
While 393 rows is not an excessive amount of data, you should find that bulk processing is much faster than looping through rows and processing each cell.
Sub Date_Conversion()
With ActiveSheet
With .Columns(1)
.TextToColumns Destination:=.Offset(0, 4), DataType:=xlFixedWidth, FieldInfo:=Array(0, 3)
.Offset(0, 4).NumberFormat = "mm/dd;;;[red]#"
End With
End With
End Sub
As no year was provided for any of the cells, the default is the current year. Any text value that was not processed will be formatted in a red font and left-aligned.
If you wanted to simply replace the existing data in column A, replace .Offset(0, 4) with .Cells(1).
In column 1 let's say you have the dd/mm. In the next column, label that DD and in each cell put
=DAY(A2)
Where a2 is the target cell. Do the same in the next column labelled MM. Create another column labelled YYYY and put the year you want. Then one last column labelled mm/dd/yyyy and put this formula
=CONCATENATE(D2&"/"&C2&"/"&B2)
Long work around, but will get you what you want.
Since the date is represented by a string in the format mm/dd you need to first manipulate the string to get it in a day format:
You can use the below:
=DATEVALUE(RIGHT(CELL,2)&"/"&LEFT(CELL,2))
(replace cell with the address of the cell that you want to change(
Once you have done this to show dd/mm:
Select the cells that you want to format and:
Right Click / Format Cells / Custom
and type in the inputbox dd/mm
Hope this helps.
So I finally fixed it and this charm of a code did the trick! Wanted to share with you guys!
Sub Date_Conversion()
For i = 1 To 393
m = Mid(Cells(i, 1), 1, 2)
D = Right(Cells(i, 1), 2)
Cells(i, 5).NumberFormat = "mm/dd;#"
Cells(i, 5) = DateSerial(2015, m, D)
Next
End Sub

Subtraction between to excel column using VBScript

Suppose I have below excel sheet,And I need to find the difference between them and result need to put back to another column:
ColA ColB ColC
9/3/2012 8:31:59 AM 09/17/2012 6:45:56 PM Result
9/4/2012 8:31:59 AM 10/17/2012 6:45:56 PM Result
I did it using Loop and Row-By-Row technique. Looking for a way if it can be done directly by column level subtraction. Say ColB-ColA - > ColC. The whole operation should be performed at a time.Result should come "hh:mm:ss".
CODE
IntRow4=2
Do While objSheet4.Cells(IntRow4,1).Value <> ""
If objSheet4.Cells(IntRow4,9).Value <> "Open" Then
Date1=objSheet4.Cells(IntRow4,7).Value
Date2=objSheet4.Cells(IntRow4,8).Value
objSheet4.Cells(IntRow4,11)=TimeSpan(Date1,Date2)
End If
IntRow4=IntRow4+1
Loop
Update
ColA1 ColB1 ColC1 ColA2 ColB2 ColC2 ..... ColAN ColBN ColCN TotaltimeDurtion
Date Date 11:25:20 Date Date 10:25:00 Date Date 11:25:20 ?
here i have shown only one row,But there can be multiple or N number of rows.What I need to do is,I want to add the time durations and put them to the last colum "TotaltimeDurtion".But the last column can not be fixed.And all the columns for each row shouldn't required values,but all never will be empty.Can we also do this also in column level.here the duration is hh:mm:ss format or as per your instruction [h]:mm:ss. TotaltimeDurtion <- ColC1 + ColC2 + ...+ ColCN.
using the range object, I can set the formula on all the cells within a range at once
range("C1:C10").Formula="=B1-A1"
It will also adjust the formula based on the normal copying riles for absolute addressing.
e.g. with the above example, C10 will be =B10-A10. If I had put the formula as "=B1-$A$1" then C10 would have been =B10-$A$1
You can subtract one date from the other, and then set the formatting of the cell:
'Within the Do..While loop
Dim cell
Set cell = objSheet4.Cells(intRow4,11)
cell.Value = Date2 - Date1
cell.NumberFormat = "hh:mm:ss"

Add one day to date in cells using 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?
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.
Here's an example of how to use DateAdd:
Range("A1").value = DateAdd("d", 1, CDate(Range("A1")))
This, of course, assumes a valid date is in A1. It will increment that date by a single day.
"d" means we are adding a day. Here are the other intervals for adding years, months, etc.
yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week
h - Hour
n - Minute
s - Second
Notice I use CDate. That simply converts the value of range("a1") into a date. It will throw an error if A1 cannot be parsed as a date.
Of course, you can also use this method to subtract days:
Range("A1").value = DateAdd("d", -3, CDate(Range("A1")))
This subtracts three days for the date in A1.
If you have date in cell a1 and want increment one day, it should be as simple as
range("a1").value = range("a1").value + 1

Resources