I have dropdown box with months populated. When a month is selected I would like to then convert it to the month number is there a function that can do this?
Eg. September = 9
Another way
Excel Formula
=MONTH(1&A1)
VBA
Sub Sample()
Dim MonthNm As String
MonthNm = "September"
Debug.Print Month(DateValue("01 " & MonthNm & " 2012"))
End Sub
or
Sub Sample()
Dim MonthNm As String
MonthNm = "September"
Debug.Print Application.Evaluate("=MONTH(1&" & Chr(34) & MonthNm & Chr(34) & ")")
End Sub
Replace
Try this...
=MONTH(DATEVALUE(A1&"1"))
Where A1 cell contains month name.
Sub month()
Dim monthh As Integer
monthh = month(Date)
MsgBox monthh
End Sub
try this.
another excel formula where A1 is the cell id with month name:
=TEXT(DATEVALUE(A1&" 1"), "m")
This solution didn't work for me (Excel 2010), I had to shorten the month name to 3 characters and add the day number in front of the shortened string.
=MONTH(1&LEFT(A1;3))
Another VBA solution
For the sake of the art in addition to Siddharth's valid answer :-)
Sub SampleTM()
Dim MonthNm$: MonthNm = "September"
Debug.Print MonthNm2Num(MonthNm)
End Sub
Function MonthNm2Num(ByVal MonthNm) As Long
MonthNm2Num = Format(CDate(MonthNm & "/1 0"), "m") * 1&
End Function
Related
This question already has answers here:
Prevent Excel to format string as date
(2 answers)
Closed last year.
Sub test1()
MonthYear = MonthName(Right(2009, 2)) & " " & Left(2009, 2) 'locals window show "September 20"
Range("A1").Value = MonthYear '20-Sep is shown in cell A1 where the formula box is 20-09-2022
Range("A1").Value = "'" & MonthYear '---> my solution for the time being
End Sub
What I want is in cell A1 show September 20 (just like in the Locals Window), as a text not a date.
What I've tried so far :
Sub test2()
MonthYear = "01-" & MonthName(Right(2009, 2)) & "-20" & Left(2009, 2) 'locals window show "01-September-2020"
Range("A1").Value = MonthYear '01-Sep-20 is shown in cell A1 where the formula box is 01-09-2020
Range("A2").Value = Format(MonthYear, "mmmm yy") 'AGAIN, 20-Sep is shown in cell A1 where the formula box is 20-09-2022
Range("A1").NumberFormat = "mmmm yy" 'another solution for the time being
End Sub
My question:
How do I have to write the code, so it put the result in the cell directly as a text September 20 ?
Any kind of help would be greatly appreciated.
Thank you in advanced 🙏.
Apply text formatting first:
Range("A1").NumberFormat = "#"
Range("A1").Value = MonthYear
I want to convert a date in a cell to the date function so it is a formula. How do I get the date (using VBA), any date, say, 13 Jun 2020 to =DATE(2020, 6, 13) using variables for the year, month, and day. My code I have tried but won't work. The activecell shows 13-Jun-2020 as a date but appears in the function box as 13/06/2020
Sub ConvertDateToDateFunction()
Dim mvDay, mvMth, mvYr As Integer
mvDay = Left(ActiveCell, 2)
mvMth = Mid(ActiveCell, 4, 2)
mvYr = Right(ActiveCell, 4)
ActiveCell.Value = "=DATE(mvYr, mvMth, mvDay)"
End Sub
You have two problems. Here is the solution to the smaller one. The code below would do what you intend. It would convert a text string in the ActiveCell to a function of similar value and insert it in the cell below the ActiveCell.
Sub ConvertDateToDateFunction()
' if you don't say what it's supposed to be it'll be a Variant
Dim mvDay As String, mvMth As String, mvYr As String
mvDay = Left(ActiveCell.Value, 2)
mvMth = Mid(ActiveCell.Value, 4, 2)
mvYr = Right(ActiveCell.Value, 4)
ActiveCell.Offset(1).Formula = "=DATE(" & mvYr & "," & mvMth & "," & mvDay & ")"
End Sub
It's not entirely easy to insert a date as a text string in Excel because Excel will try to recognize a date for a date. Observe that any part of a string is a string, not an integer.
Now about your much bigger problem which is that you don't understand how Excel handles dates. It is such a big problem because you are trying to create a date in Excel in various ways and you run into all sorts of trouble. Read up on the subject here.
To give you a taste of what you will learn: what you see displayed in a cell isn't what the cell contains. There might be a formula in it and you see a number. And there might be a date and you see a string. What you see is determined by the cell's format. I think Chip Pearson's article will cover that topic. If you need to know more, look for "Cell formatting" on the web.
Your macro won't work because the date is a "real date" and not a string.
Try the following to convert the contents of cells containing a real date to a formula which will return the same date:
Option Explicit
Sub dtToFormula()
Dim R As Range, C As Range
Dim vDateParts(2)
Set R = [a1:a10]
'Set R = ActiveCell 'or Selection whatever range you want to convert
For Each C In R
If IsDate(C) And Not C.HasFormula Then
vDateParts(0) = Year(C.Value2)
vDateParts(1) = Month(C.Value2)
vDateParts(2) = Day(C.Value2)
C.Formula = "=DATE(" & Join(vDateParts, ",") & ")"
End If
Next C
End Sub
I have to find the number of working days between two dates which should exclude weekends and National Holidays.
I am using function NETWORKDAYS in vba, this excludes weekends but I want to exclude Some National Holidays as well.
How to use this function NETWORKDAYS(startDate, endDate, [holidays]) for National holidays. It says it accepts [holidays] as a list. I have all the national holidays in an array. how can I use it with this function via VBA ??
Please find the code snippet.
Public Function dataFromInputSheetHolidayDates() As Variant
Dim holidayDates As Integer
Dim holidaydatesArray(20) As Variant
holidayDates = Sheets("InputSheet").Cells(Rows.Count, "D").End(xlUp).Row
For countDate = 0 To holidayDates - 1
holidaydatesArray(countDate) = Format(Sheets("InputSheet").Cells(countDate + 2, "D").Value, "DD-MMM-YY")
Next countDate
dataFromInputSheetHolidayDates = holidaydatesArray
End Function
holidayList = dataFromInputSheetHolidayDates()
Sheets("Estimation").Range("Z" & taskcounter).Formula = "=NETWORKDAYS(X" &
taskcounter & ",Y" & taskcounter &","& holidayList & ")"
Sheets("Estimation").Range("AB" & taskcounter).Formula = "=NETWORKDAYS(X" &
taskcounter & ",AA" & taskcounter &"," & holidayList & ")"
Change these 2 lines in your code:
Dim holidayDates As Long
holidaydatesArray(countDate) = Sheets("InputSheet").Cells(countDate + 2, "D")
In VBA Integer is up to 32767, which is not quite enough for dates. Furthermore, holidaydatesArray should have a numeric value and not some text format.
Pretty much similar problem as this one - workday holiday argument doesn't accept array
If you are trying to create a flexible formula through VBA, where the holidays are on different worksheet, try this solution:
Public Sub TestMe()
Dim holidayLists As Range
Set holidayLists = Worksheets(2).Range("D1:D10")
With Worksheets(1)
.Range("A1").Formula = "=NETWORKDAYS(B1, C1," & holidayLists.Parent.Name & "!" _
& holidayLists.Address & ")"
End With
End Sub
There the holidayLists.Parent.Name & "!" & holidayLists.Address would refer correctly to the worksheet's name of the holidays.
i need some help with excel.
I have a cell that get value by macro with code:
Range("A1").Value = "This Year we are having our guest " &Guest_name &" who is currently " &Guest_age &"years old, spending holidays with us"
So i'm just wondering how can I get the Guest_name and Guest_age to be bold.
Thanks
Here is one version of the answer. Using Characters to instruct vba where to start and end.
Sub test()
Dim guest_name, guest_name_len As Integer
Dim guest_age, guest_age_len As Integer
guest_name = "tester"
guest_name_len = Len(guest_name)
guest_age = "999"
guest_age_len = Len(guest_age)
Debug.Print guest_name_len & " / " & guest_age_len
Range("A1").Value = "This Year we are having our guest " & guest_name & " who is currently " & guest_age & "years old, spending holidays with us"
With Range("A1").Characters(Start:=35, Length:=guest_name_len).Font '35 characters counted (this year....)
.FontStyle = "bold"
End With
With Range("A1").Characters(Start:=35 + guest_name_len + 18, Length:=guest_age_len).Font '18 characters counted (who is... years old)
.FontStyle = "bold"
End With
End Sub
Use InStr to locate each within the range's .value2 property and apply .font.bold = true to the substring's .Characters property.
(Excel 2010 VBA)
I have a cell (A1) containing a date in the format of mmm-yy ("Custom" category).
Foe example, if I enter 1/6/13 the cell shows June-13. That's fine.
In my VB macro I need to check this date whether the month is the current month and whether the year is the current year. I don't care about the day.
Does this help you:
Public Sub test()
d = Sheet1.Range("A1")
n = Now()
If Year(d) = Year(n) And Month(d) = Month(n) Then
MsgBox "It works"
End If
End Sub
Thanks to Dave and MiVoth I did :
Dim xdate As Date
xdate = Worksheets("sheet1").Range("A1")
If Month(Date) = Month(xdate) And Year(Date) = Year(xdate) Then
MsgBox "OK"
Else
MsgBox "not OK"
End If
That did the job!
Thank a lot to everyone,
Gadi
How about this:
Function MonthYear() As Boolean
MonthYear = False
If Not IsDate(Cells(1, 1)) Then Exit Function
If Month(Date) = Month(Cells(1, 1)) And Year(Date) = Year(Cells(1, 1)) Then
MonthYear = True
End If
End Function
The function returns true if month and year are the same as current date. If not it returns false.