inserting a formula in front of a range

powerdink

New Member
Joined
Feb 16, 2011
Messages
21
I have a spreadsheet that has data that starts in Column H. I'd like to have my macro insert a formula in front of this column and then copy it all the way down to the last row of data. Here is what I have so far, but I keep getting "application-defined or object-defined error."

Code:
Dim rng As Range
 
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("H2").CurrentRegion
 
        With rng.Offset(1, -4).Resize(rng.Rows.Count, 1)
            .Formula = "=some formula in here"
            .Value = .Value     'copy the value and paste it as a value to remove the formula
        End With

I've tried declaring a second range, say starting at cell C2 and going to the row count of rng, but I couldn't get that to work either.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try

Code:
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("OFFSET($H$2,0,-4,COUNT(OFFSET($H$2,0,0,9999)),1)")
        With rng
            .Formula = "=H2*5%"
            .Value = .Value     'copy the value and paste it as a value to remove the formula
        End With
 
Upvote 0
Thanks,
I actually went back to the declaring two ranges and got this to work.

Code:
Dim rng As Range, f As Range 
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("H2").CurrentRegion
Set f = ThisWorkbook.Worksheets("Sheet1").Range("D2").Resize(rng.Rows.Count, 1)
 
With f
            .Formula = "=H2*5%"
            .Value = .Value
End With

Works
 
Upvote 0
The debug line could not count text,
So new macro ~

Code:
Sub ABSOLUTEXCOLUMN()
'
' ABSOLUTEXCOLUMN Macro
' Macro recorded 3/10/2011 by Andrew Quirl
'
' Keyboard Shortcut: Ctrl+Shift+H
'
Range("AM4").Select
ActiveCell.FormulaR1C1 = "marked"
With ActiveCell.Characters(Start:=1, Length:=6).Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 8
End With
Range("A4:AM4").Select
Selection.AutoFilter
Selection.AutoFilter Field:=7, Criteria1:=">=5 to 6 weeks"
Range("OFFSET($AM$5,0,0,COUNT(OFFSET($A$5,0,0,9999)),1)").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.FormulaR1C1 = "x"
Selection.AutoFilter Field:=7
Selection.AutoFilter Field:=25, Criteria1:=">(35) Active"
Range("OFFSET($AM$5,0,0,COUNT(OFFSET($A$5,0,0,9999)),1)").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.FormulaR1C1 = "x"
Selection.AutoFilter Field:=25
Selection.AutoFilter Field:=38, Criteria1:="Yes"
Range("OFFSET($AM$5,0,0,COUNT(OFFSET($A$5,0,0,9999)),1)").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.FormulaR1C1 = "x"
Selection.AutoFilter Field:=38
Selection.AutoFilter Field:=39, Criteria1:="x"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,334
Members
452,907
Latest member
Roland Deschain

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