Macro - fill formula to bottom

trentq

New Member
Joined
Jun 19, 2012
Messages
6
Hi all

Could someone please help get a macro to work?

We extract a report from a system and the A column has a series of transaction dates, I want to change these to be the week-ending Sunday date, so I insert a column B put this formula in =IF(A2="","",A2+7-MOD(A2-1,7)) and fill down.

Then copy-paste B with values and delete A.

But the macro doesn't seem to work, can you see what I am doing wrong? or is there a better way to do this?

Thanks in advance.

VBA Code:
Sub Macro2()
'
' Macro2 Macro
'

'
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Date"
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-1]+7-MOD(RC[-1]-1,7))"
    Range("B2").Select
    Selection.AutoFill Destination:=Range("B2:B1011")
    Range("B2:B").Select
    Columns("B:B").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Columns("A:A").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("B7").Select
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,670
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
What happens with the code below?
VBA Code:
Sub Macro2()
    With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
        .FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-1]+7-MOD(RC[-1]-1,7))"
        .Value = .Value
        .NumberFormat = "ddd MM/DD/YYYY"
    End With
    Columns("A:A").Delete Shift:=xlToLeft
    Cells.EntireColumn.AutoFit
End Sub
 
Solution

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,504
Office Version
  1. 2013
Platform
  1. Windows
MAybe this way UNTESTED
VBA Code:
Sub Macro2()
    Columns("B:B").Insert
    Range("B1").Value = "Date"
    Range("B2:B1011").Formula = "=IF(A2="""","""",A2+7-MOD(A2-1,7))"
     With Columns("B:B")
        .Copy
        .PasteSpecial Paste:=xlPasteAllUsingSourceTheme
        .Value = .Value
    End With
     With Range("A:A")
     .Delete
    .AutoFit
    End With
    Range("B7").Select
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,119,015
Messages
5,575,560
Members
412,676
Latest member
Davejf81
Top