Insert formula with code dynamically

D

Desert Piranha

Hi all,
Ok don't laugh (to hard).
The goal is to insert a dynamic formula in Column 'I' Matching the last
used cell in Column 'C'.
By dynamic, i mean the formula should advance each cell downward.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
Dim lLastrow As Long
With lLastrow = .Range("C65536").End(xlUp).Row
For Each rCell In .Range("I1:I" & lLastrow)
If rCell.Offset(6, 0).Value <> "" Then
rCell.FormulaR1C1 = "=IF(D2="","",(H2/G2))"
End If
Next rCell
End With
End Sub
 
C

Crowbar via OfficeKB.com

Dim LastRow As Long
Dim RowNdx As Long
Dim OldVal As String

LastRow = Cells(Rows.Count, "I").End(xlUp).Row
OldVal = Range("C1")
For RowNdx = 5 To LastRow
If Cells(RowNdx, "I").Value = "" Then
Cells(RowNdx, "C").Value = OldVal
Else
OldVal = Cells(RowNdx, "C").Value
End If
Next RowNdx
 
D

Desert Piranha

Crowbar said:
Dim LastRow As Long
Dim RowNdx As Long
Dim OldVal As String

LastRow = Cells(Rows.Count, "I").End(xlUp).Row
OldVal = Range("C1")
For RowNdx = 5 To LastRow
If Cells(RowNdx, "I").Value = "" Then
Cells(RowNdx, "C").Value = OldVal
Else
OldVal = Cells(RowNdx, "C").Value
End If
Next RowNdx

I'v played with this for an hour or so. Haven't had any sucess yet,
will keep trying and post back.
 
T

Tom Ogilvy

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
Dim lLastrow As Long
With lLastrow = .Range("C65536").End(xlUp).Row
Application.EnableEvents = False
For Each rCell In .Range("I1:I" & lLastrow)
If rCell.Offset(6, 0).Value <> "" Then
rCell.FormulaR1C1 = "=IF(D" & rCell.row & _
"="""","""",(H" & rCell.row & "/G" & rCell.row _
& "))"
End If
Next rCell
Application.EnableEvents = True
End With
End Sub

--
Regards,
Tom Ogilvy



"Desert Piranha"
 
D

Desert Piranha

Hi Tom,
Thx for replying.

With lLastrow = .Range("C65536").End(xlUp).Row
Error in this line "Range" gets highlighted in blue and says "Invalid
or unqualified reference"

FYI - Column 'C' has Text, Column 'G' and 'H' have numbers

Dave
Tom said:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
Dim lLastrow As Long
With lLastrow = .Range("C65536").End(xlUp).Row
Application.EnableEvents = False
For Each rCell In .Range("I1:I" & lLastrow)
If rCell.Offset(6, 0).Value <> "" Then
rCell.FormulaR1C1 = "=IF(D" & rCell.row & _
"="""","""",(H" & rCell.row & "/G" & rCell.row _
& "))"
End If
Next rCell
Application.EnableEvents = True
End With
End Sub

--
Regards,
Tom Ogilvy



"Desert Piranha"
message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top