Macro - fill formula to bottom

trentq

New Member
Joined
Jun 19, 2012
Messages
7
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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
 
Upvote 0
Solution
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
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,267
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top