Solved

Excel add date to text

Posted on 2016-10-07
3
43 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 17

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 23

Author Closing Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now