How to use EMONTH function in vba?

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
Hi everyone out there. I have a report output from an accounting system that I need to clean up and date it. Any help is appreciated.

Column 6 contains transaction dates but not neccessary the last day of the month. I am hoping to use the EMONTH function to turn this date to the last day of the month and output to a range. glDate = 5/27/2011 but what I get is 1/31/1900.

Here are the lines of codes that I have trouble with:

Sub glSort()

FinalRow = Cells(Rows.count, 1).End(XlUp).Row

glDate = Cells(FinalRow, 6).End(XlUp).Value

Cells(2, 13).Resize(FinalRow-2, 1).Formula = Evaluate ("=EMONTH(" & glDate & ",0)")

End Sub
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Did you mean EOMONTH ?

Cells(2, 13).Resize(FinalRow-2, 1).Formula = Evaluate ("EOMONTH(" & glDate & ",0)")

Also, are you getting the correct date from column F? You find the FinalRow from column A. Then from that FinalRow you do a 2nd xlUp in column F. Is that what you really want? The Date from column F is not from the same row as FinalRow.

Is this what you want for the transaction date
glDate = Cells(FinalRow, 6).Value
 
Last edited:
Upvote 0
In VBA, go to tools --> references and check atpvbaen.xls (the reference for the analysis toolpak in VBA).

Then you can use EOMONTH(glDate,0) directly in your VBA instead of the EVALUATE command that's giving you issues.
 
Upvote 0
Yes, EOMONTH is what I intended to use.

The FinalRow from column A is to measure the pasting range.

glDate = Cells(FinalRow, 6).value ' date value is 5/27/2011

I want to take this date 5/27/2011 and turn it into 5/31/2011
by using the EOMONTH function.
 
Upvote 0
Tried checking atpvbaen.xls

The return date is still 1/31/1900
I think the problem is in the last line of code

This is code:
Sub GLsort()

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

Cells(2, 14).Resize(Rows.Count - 1, 3).Clear
Cells(2, 15).Resize(FinalRow - 2, 1).Formula = "=TRIM(B2&C2)"
Cells(2, 14).Resize(FinalRow - 2, 1).Formula = "=IF(ISNUMBER(IF(AND(LEFT(O1,5)<>""Total"",LEFT(O2,5)=""Total""),VALUE(TRIM(MID(O2,7,4))),""""))=TRUE,IF(AND(LEFT(O1,5)<>""Total"",LEFT(O2,5)=""Total""),VALUE(TRIM(MID(O2,7,4))),""""),"""")"
Cells(2, 16).Resize(FinalRow - 2, 1).Formula = "=IF(N2="""","""",L2)"
Cells(FinalRow, 16).FormulaR1C1 = "=SUM(R[-" & FinalRow - 2 & "]C:R[-1]C)"
Cells(2, 16).Resize(FinalRow, 1).NumberFormat = "#,##0.00"
Cells(2, 14).Resize(FinalRow - 2, 3).Copy
Cells(2, 14).Resize(FinalRow - 2, 3).PasteSpecial Paste:=xlPasteValues

glDate = Cells(FinalRow, 6).End(xlUp).Value
Cells(2, 13).Resize(FinalRow - 2, 1).Formula = "=EOMONTH(" & glDate & ",0)"


End Sub
 
Upvote 0
Do you want the EOMONTH function in your output? Or just the date the EOMONTH function returns?

If you want the function, do you want it to link to the glDate cell? Or, do you want the hardcoded value in the function?
 
Upvote 0
What date does the MsgBox return?

Code:
Sub GLsort()

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

Cells(2, 14).Resize(Rows.Count - 1, 3).Clear
Cells(2, 15).Resize(FinalRow - 2, 1).Formula = "=TRIM(B2&C2)"
Cells(2, 14).Resize(FinalRow - 2, 1).Formula = "=IF(ISNUMBER(IF(AND(LEFT(O1,5)<>""Total"",LEFT(O2,5)=""Total""),VALUE(TRIM(MID(O2,7,4))),""""))=TRUE,IF(AND(LEFT(O1,5)<>""Total"",LEFT(O2,5)=""Total""),VALUE(TRIM(MID(O2,7,4))),""""),"""")"
Cells(2, 16).Resize(FinalRow - 2, 1).Formula = "=IF(N2="""","""",L2)"
Cells(FinalRow, 16).FormulaR1C1 = "=SUM(R[-" & FinalRow - 2 & "]C:R[-1]C)"
Cells(2, 16).Resize(FinalRow, 1).NumberFormat = "#,##0.00"
Cells(2, 14).Resize(FinalRow - 2, 3).Copy
Cells(2, 14).Resize(FinalRow - 2, 3).PasteSpecial Paste:=xlPasteValues

glDate = Cells(FinalRow, 6).End(xlUp).Value
[COLOR="Red"]MsgBox Format(glDate, "mmm d, yyyy")[/COLOR]
Cells(2, 13).Resize(FinalRow - 2, 1).Formula = "=EOMONTH(" & glDate & ",0)"


End Sub
 
Upvote 0
MsgBox returns May 27, 2011

It's really strange. In Excel formula bar, it reads =EOMONTH(5/27/2011,0) but the value is 1/31/1900. However if I manually change it to =EOMONTH("5/27/2011",0) then it works (5/31/2011).
 
Upvote 0
Try this...
Cells(2, 13).Resize(FinalRow - 2, 1).Formula = "=EOMONTH(" & DateSerial(glDate) & ",0)"

Or just this...
Cells(2, 13).Resize(FinalRow - 2, 1).Formula = EOMONTH(DateSerial(glDate), 0 )


Or just dim glDate as a Date type
Dim glDate As Date
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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