?
Solved

Excel add date to text

Posted on 2016-10-07
3
Medium Priority
?
93 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 21

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 23

Accepted Solution

by:
Ejgil Hedegaard earned 2000 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 25

Author Closing Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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