Solved

Excel add date to text

Posted on 2016-10-07
3
63 Views
Last Modified: 2016-10-11
I'd like to add the prior month to the user's name. I'd like it to return this:
John Doe - September

Open in new window


In the VBA debugger, when I Add Watch, this works:
Rng.Cells(1).Value + " - " + Format(DateAdd("m", -1, Now), "mmmm")

Open in new window


But, in the macro, it doesn't. It says "Run time error 438. Object doesn't support this property or method":

With rng.Cells(1).Offset(-1, 3).Font
   .Bold = True
   .Size = 14
   .Name = "Calibri"
   .Value = Rng.Cells(1).Value + " - " + Format(DateAdd("m", -1, Now), "mmmm")
End With

Open in new window


Here's the the whole code, for context...
Sub AddGroupAndTotals()
'
' Run after SeparateGroups
' Put BM's name on bolded line, add bolded totals for each group, and under and overline totals row.
'
'
Dim rng As Range
Application.ScreenUpdating = False
Range("A1").Select
For Each rng In Range("A:A").SpecialCells(xlCellTypeConstants, 2).Areas
    If rng.Cells(1).Row <> 1 Then
        rng.Cells(1).Copy rng.Cells(1).Offset(-1, 3)
        With rng.Cells(1).Offset(-1, 3).Font
            .Bold = True
            .Size = 14
            .Name = "Calibri"
            .Value = rng.Cells(1).Value + " - " + Format(DateAdd("m", -1, Now), "mmmm")
        End With
        rng.Cells(rng.Rows.Count + 1).Offset(0, 6).Resize(, 8).FormulaR1C1 = "=SUM(R[-" & rng.Rows.Count & "]C:R[-1]C)"
        rng.Cells(rng.Rows.Count + 1).Offset(0, 6).Resize(, 8).Font.Bold = True
        ' Add page break at bottom of Totals line
        ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=rng.Cells(rng.Rows.Count + 2)
        With rng.Cells(rng.Rows.Count + 1).Offset(0, 6).Resize(, 8).Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With rng.Cells(rng.Rows.Count + 1).Offset(0, 6).Resize(, 8).Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
    End If
Next rng
Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:NVIT
3 Comments
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41834706
This will return the Value in A1 with a hyphen then the previous month

MsgBox Cells(1, 1).Value & " - " & Format(Application.WorksheetFunction.EoMonth(Date, -1), "mmmm")

Open in new window

0
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
ID: 41835041
Line 17 assign the cell value to the cell font, and that is not possible.
Move it outside the with block for font settings, like this
rng.Cells(1).Offset(-1, 3).Value = rng.Cells(1).Value + " - " + Format(DateAdd("m", -1, Now), "mmmm")

Open in new window

0
 
LVL 24

Author Closing Comment

by:NVIT
ID: 41838939
Thank, Ejgil! Have a nice day...
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question