Solved

Excel add date to text

Posted on 2016-10-07
3
79 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 22

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

680 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