+ Reply to Thread
Results 1 to 5 of 5

Insert formula with code dynamically

  1. #1
    Forum Contributor
    Join Date
    11-20-2005
    Posts
    256

    Insert formula with code dynamically

    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
    Thx
    Dave
    "The game is afoot Watson"

  2. #2
    Crowbar via OfficeKB.com
    Guest

    Re: Insert formula with code dynamically

    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

    --
    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200601/1

  3. #3
    Forum Contributor
    Join Date
    11-20-2005
    Posts
    256
    Quote Originally Posted by 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

    --
    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200601/1
    Hi Crowbar,

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

  4. #4
    Tom Ogilvy
    Guest

    Re: Insert formula with code dynamically

    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"
    <[email protected]> wrote in
    message news:[email protected]...
    >
    > Crowbar via OfficeKB.com Wrote:
    > > 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
    > >
    > > --
    > > Message posted via OfficeKB.com
    > > http://www.officekb.com/Uwe/Forums.a...ing/200601/1Hi

    Crowbar,
    >
    > I'v played with this for an hour or so. Haven't had any sucess yet,
    > will keep trying and post back.
    >
    >
    > --
    > Desert Piranha
    >
    >
    > ------------------------------------------------------------------------
    > Desert Piranha's Profile:

    http://www.excelforum.com/member.php...o&userid=28934
    > View this thread: http://www.excelforum.com/showthread...hreadid=504764
    >




  5. #5
    Forum Contributor
    Join Date
    11-20-2005
    Posts
    256
    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
    Quote Originally Posted by 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"
    <[email protected]> wrote in
    message news:[email protected]...
    >
    > Crowbar via OfficeKB.com Wrote:
    > > 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
    > >
    > > --
    > > Message posted via OfficeKB.com
    > > http://www.officekb.com/Uwe/Forums.a...ing/200601/1Hi

    Crowbar,
    >
    > I'v played with this for an hour or so. Haven't had any sucess yet,
    > will keep trying and post back.
    >
    >
    > --
    > Desert Piranha
    >
    >
    > ------------------------------------------------------------------------
    > Desert Piranha's Profile:

    http://www.excelforum.com/member.php...o&userid=28934
    > View this thread: http://www.excelforum.com/showthread...hreadid=504764
    >

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1