Solved

Excel add date to text

Posted on 2016-10-07
3
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Outlook Free & Paid Tools
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

739 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