0

I have following code First it will insert two columns one by one Then count rows in C column Then Copy ActiveCell.FormulaR1C1 = "=R[-5]C[18]" to B7 to rows count Similarly, Copy ActiveCell.FormulaR1C1 = "=R[-5]C[13]" to A7 to rows count But I am getting error when the Active Row is only 01 (One), if it is more than one then it works ok. I am struggling with this. If anyone can please help.

Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove

Dim LastRow As Long
LastRow = Range("C" & Rows.Count).End(xlUp).Row

Range("B7").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-5]C[18]"
Range("B7").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False

Selection.AutoFill Destination:=Range("B7:B" & LastRow), Type:=xlFillCopy
Range("B7:B" & LastRow).Select

Range("A7").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-5]C[13]"
Range("A7").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False

Selection.AutoFill Destination:=Range("A7:A" & LastRow), Type:=xlFillCopy
Range("A7:A" & LastRow).Select
Say Hi
  • 1

2 Answers2

1

To close the question out:

Instead of using AutoFill, maybe do Range("B7:B" & LastRow).Value = Range("B7").Value, and similarly for column A.

Or just in one line for both columns: Range("A7:B" & LastRow).Value = Range("A7:B7").Value.

BigBen
  • 46,229
  • 7
  • 24
  • 40
0

First off, the Selection object is not necessarily the best option. You can insert the columns with Range("A:B").Insert xlShiftToRight.

Next, you can set the formula for an entire range at once rather than copying and pasting. Since you're putting the formulas only starting at row 7, you'll need to offset and resize the range that is defined by what is in Column C (after the insert).

The Resize method will need to reduce the row count by 6 since the formulas don't start until row 7:.Resize(LastRow - 6). Since the original range is only one column wide, the column count in the Resize method can be omitted (otherwise, it'd be .Resize(LastRow - 6, 1)).

The Offset method will need to shift down 6 rows and left 1 (or 2) columns: .Offset(6,-1) and .Offset(6,-2)).

Your code would then be greatly simplified to:

Dim LastRow As Long
ActiveSheet.Range("A:B").Insert xlShiftToRight

LastRow = Range("C" & Rows.Count).End(xlUp).Row

With Range("C:C").Resize(LastRow-6) 
    .Offset(6,-1).FormulaR1C1="=R[-5]C[18]"
    .Offset(6,-2).FormulaR1C1="=R[-5]C[13]"
End With

Of course, even easier would be to convert your sheet to a Table ("ListObject" in VBA) and let Excel do the heavy lifting. But that's way outside what you asked.

Dharman
  • 30,962
  • 25
  • 85
  • 135
pdtcaskey
  • 242
  • 1
  • 9