Converting all dates in a year into multiple strings using Excel VBA - string

I have to write a vba code that convert all dates in a year into days in a week eg. Monday, Tuesday, Wednesday....Sunday and the value of all days in a week eg.Monday must be in a string format. (See image inserted)
I tried using the .NumberFormat function but it does not work since the format is still under the date format although the values displayed are correct (please look at the formula bar of F10). Below is the code I have written:
Sub convertdate()
Range("A7:A371").Copy
'copy all the dates throughout the year'
Range("F7:F371").PasteSpecial xlPasteFormulasAndNumberFormats
'paste it into F column'
Worksheets("Sheet1").Range("F7:F371").NumberFormat = "dddd"
'convert the dates into days'
Sum1 = Application.SumIf(Range("F7:F371"), "Monday", Range("B7:B371"))
'example of calculating the total rainfall of all Mondays throughout the year'
Worksheets("Sheet1").Range("G22").FormulaArray = Sum1
End Sub
The formula bar from cell F7:F371 should display the string value "Monday","Tuesday" etc depending on the dates rather than the date itself.The reason of converting it into a string is so that it could be use in a SUMIF function later on to calculate the total rainfall of all Mondays,Tuesday etc.
Appreciate if anyone could help. Thank you.

A formula can do this.
In cell F7 enter =TEXT(A7,"dddd") and drag down.
This will format the date to show the full day name as a string.
https://support.office.com/en-us/article/TEXT-function-20d5ac4d-7b94-49fd-bb38-93d29371225c

Try something like this. It will loop through all the dates in Column A (starting in row 7) and put the associated Weekday name in Column F:
Sub test()
Dim i As Long
Dim lRow As Long
With ActiveSheet
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 7 To lRow
.Cells(i, 6).Value = WeekdayName(Weekday(.Cells(i, 1).Value, 1), False, 1)
Next i
End With
End Sub

Related

Subtract the date from each cell in a column, skip cells that have string values

I have an Excel sheet of expiration dates. Some rows lack an expiration date so I chose to fill those cells with the string “NO DATA”.
I want to loop through all the cells and find how many days the user has before the expiration date passes.
Then I want to send an email with the list of cells whose expiration dates will expire within two weeks.
I am stuck at the looping part so I just want to get at least that done.
Column A has the truck Id and Column D has the expiration dates for one document. (I have versions but I want to focus on just one first.)
Column B to C is full of values. They just aren’t worth mentioning at this moment—at least I think.
The code I created results in a type mismatch which makes sense since the cells in Column D aren't the same data type.
I fixed small mistakes but it still doesn't work.
Sub Run_Button1()
Dim mydate1 As Range
Dim datetoday1 As Date
Dim dateDif As Long
datetoday1 = Now
For Each mydate1 In Sheets("Sheet1").Range("A1:A25").Cells
If IsDate(mydate1.Value) Then
dateDif = DateDiff("d", mydate1.Value, Date)
If dateDif > 1 And dateDif < 14 Then
'This code will be added later.this code will send emails
End If
End If
Next mydate1
End Sub
From comments above:
Sub Run_Button1()
Dim c As Range, v
Dim dateDif As Long
For Each c In Sheets("Sheet1").Range("A1:A25").Cells
v = c.EntireRow.Columns("D").Value 'read from date column
If IsDate(v) Then
dateDif = DateDiff("d", cdate(v), Date)
If dateDif > 1 And dateDif < 14 Then
End If
End If
Next c
End Sub

Issue sorting dates with DDMMYY end of month

I've created a sub-routine which orders the ActivityDate column smallest to largest. The dates are formatted like DDMMYY so for example, 010621 would be the first this month, 300621 would be the last.
My issue is that I conduct reports weekly which means that on occasion, I will get the last day / days of the previous month. When this happens, those dates will be placed at the bottom, as 30 comes after 01 numerically e.g. 310521 will appear at the bottom of the spreadsheet, 010621 will appear first.
I know that I can add a column for month, use =MID(date, 3, 2) and sort the month number first but is there any simpler way of doing this?
This is my code for sorting the dates:
Sub SortDates()
Dim ActDate As Long
Dim ActDateLetter As String
sheet = "Overall Activity"
With ThisWorkbook.Worksheets(sheet).Rows(1)
Set b = .Find("ActivityDate", LookIn:=xlValues)
ActDate = b.Column
'Convert To Column Letter
ActDateLetter = Split(Cells(1, ActDate).Address, "$")(1)
End With
ThisWorkbook.Worksheets(sheet).Activate
ThisWorkbook.Worksheets(sheet).Columns(ActDate).Sort Key1:=Range(ActDateLetter & "1") _
, Order1:=xlAscending, Header:=xlYes
End Sub

Converting Dates Format into the Time Format

I have two dates in cell "A1" (Start Date) and "B1" (End Date). I change the date to get the real figures between the dates using SUMIFS formula.
There is Date Column in the Sheet with Time.
So what i want is that whenever the "A1" (Start Date) changes its time should always be initial like 00:00:00 and when i change the date for "B1" (End Date) it should always be like 23:59:59 is there any way to achieve this using VBA.
Your help will be appreciated.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
With Range("B1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
End If
Numeric time 23:59:59 is not equal to 0.9999999, it is 0.999988425925926
But use native date/time handling for this and avoid smart numbers:
Range("A1").Value = DateValue(Range("A1").Value)
Range("B1").Value = DateValue(Range("B1").Value) + TimeSerial(23, 59, 59)
I think this could work, if i understood your question correct.
It will make sure A1 is integer and make B1 integer then add 0.99999.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
Range("A1").value = cint(range("A1").value)
Range("B1").value = cint(range("B1").value) + 0.99999999
End if
Just make sure the cells is set up to display as date and time.
If I understand you correctly, you don't need to bother with the time portion of the dates or go throught the mechanics of trying to change it.
You just need to properly construct your SUMIFS formula.
Range to sum: sumRange
Dates in your table: myDates
When you enter a date in A1 or B1, it will be entered as if it were at the beginning of the day.
If your dates in myDates are just dates, you can use the formula:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<=" & endDate)
However, if your dates in myDates also include a time, then you can modify the formula slightly:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<" & endDate+1)
And you can use the 2nd formula in either instance
Edit: I just noticed you posting your formula as a comment
I assume you have entered JUST the date (no time) in I1 and J1
Your formula as posted will work UNLESS C:C includes times with the dates. If it does include times with the dates, then try:
=SUMIFS(D:D,E:E,"<>Pending for Payment",B:B,H2,C:C,">="&$I$1,C:C,"<"&$J$1+1)
By adding 1 to the date, we move it to the start of the day after; so we change the <= to just < and encompass the full range of dates you desire.

Automatically set next year in timeline

I am building a timeline and would like to have years automatically be increased according to months.
Here is my current timeline:
I have a button, that is adding +1 column = (+1 month). So if I click button, following macro is triggered:
With Sheet1
.Range("K:K").Copy
.Columns("L:L").Insert Shift:=xlToRight
End With
In screenshot attached after month is again January, year should already be 2021.
I have tried to do so:
B33
=YEAR(OtherData!$P$17)
C33 (all other cells to the right)
=YEAR(OtherData!$P$17)+IF(B34="January";1;0)
this works, but then I get 2021 only in M33. How to make formula copy previous cell until January is reached in row 34?
EDIT:
This seems to work C33 (all other cells to the right):
=IF(C34="January";YEAR(OtherData!$P$17)+IF(C34="January";1;0);B33)
but after VBA is triggered, everything gets messed up again...
EDIT 2:
I think formula is now correct, but macro should be edited somehow to copy paste from the end all the time and not operate with defined cells like now K and L.
OR
Just fill range C33 -> to the right with formula =IF(C34="January";YEAR(OtherData!$P$17)+IF(C34="January";1;0);B33) after triggering the macro. Have to find UsedRange probably somehow.
EDIT 3:
This seems to be like working solution:
Sub Add_One_Year()
Dim lColumn As Long
With Sheet1
.Range("L:L").Copy
.Columns("K:K").Insert Shift:=xlToRight
End With
lColumn = CashflowSheet.Cells(33, Columns.Count).End(xlToLeft).Column
Sheet1.Range(Cells(33, 3), Cells(33, lColumn)).Formula = "=IF(C34=""January"",YEAR(OtherData!$P$17)+IF(C34=""January"",1,0),B33)"
'MsgBox ("The last used column is: " & lColumn)
End Sub
I wouldn't try and figure the year out - add an actual date to the cell and format it to show the month or year as required. When you add a month the year will automatically increase in January.
Add your first date into cell B34 and give a custom number format of mmmm. In cell B33 use the formula =B34 and give a custom number format of yyyy.
Execute this code to add the next month:
Sub Test()
Dim LastCol As Long
With ThisWorkbook.Worksheets("Sheet1")
LastCol = .Cells(34, .Columns.Count).End(xlToLeft).Column
With .Cells(34, LastCol + 1)
.FormulaR1C1 = "=EOMONTH(RC[-1],0)+1"
.NumberFormat = "MMMM"
End With
With .Cells(33, LastCol + 1)
.FormulaR1C1 = "=R[1]C"
.NumberFormat = "YYYY"
End With
End With
End Sub

How to display only year in vba?

I have the following data set in general formatting.
Bunch of years with no day nor months provided.
What I want to achieve, is to display the date in yyyy format.
I made a standard DateSerial function to convert the data to date properly.
Private Function toDate(ByVal text As String) As Date
toDate = DateSerial(Int(text), 1, 1)
End Function
For Each cell in ws.Range("A21:A" & lr)
cell = toDate(cell)
cell.NumberFormat = "yyyy"
Next cell
Now, technically this works, but if you click inside the cell, it
obviously shows the date only as 01-01-year - that is to be expected
with usage of dateserial
But I was wondering, would it be possible given my data to somehow
omit the Days and Months and only display the result as a year?
Basically, I need want to convert the data to a Date data-type without specifying days and months
Entering the years and formatting the date as "YYYY" could be done like this:
Sub TestMe()
With ThisWorkbook.Worksheets(1)
.Range("A1") = 2011
.Range("A2") = 2013
.Range("A3") = 2011
Dim myCell As Range
For Each myCell In .Range("A1:A3")
myCell = DateSerial(myCell, 1, 1)
myCell.NumberFormat = "yyyy"
Next myCell
End With
End Sub
The "trick", is that DateSerial(myCell, 1, 1) takes the year from the cell and adds 1 for day and January for month. Thus, every year is represented as the date 1.January from that year.
More about the Date in VBA and Excel - VBA doesn't return the correct Date with Now()

Resources