loop with lastrow

BradMS

New Member
Joined
Jan 27, 2017
Messages
32
How would I change a5000 to last row?

Sub PosX()




Dim r As Range
Dim i As Long
Dim LastRow As Long


Set r = Range("A1:A5000")
For i = r.Rows.Count To 1 Step -1
With r.Cells(i, 1)
If .Value = "POSNX" Then

Range("A" & i + 0).Range("$h$1").FormulaR1C1 = _
"=RC[-4]"



'With ActiveWorkbook.ActiveSheet
' LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
' .Range("A11:AD" & LastRow).Select
'End With


End If
End With
Next i
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
Code:
Sub PosX()
   Dim r As Range
   
   For Each r In Range("A1", Range("A" & Rows.Count).End(xlUp))
      If r.Value = "POSNX" Then r.Offset(, 7).FormulaR1C1 = "=rc[-4]"
   Next r
End Sub
 
Upvote 0
Try:
Code:
Sub PosX()
    Application.ScreenUpdating = False
    Dim i As Long, LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 1 To LastRow
        If Cells(i, 1).Value = "POSNX" Then
            Range("A" & i + 0).Range("$h$1").FormulaR1C1 = "=RC[-4]"
        End If
    Next i
    Application.ScreenUpdating = True
End Sub


I'm not sure what you want to do with this line of code:
Code:
Range("A" & i + 0).Range("$h$1").FormulaR1C1 = "=RC[-4]"
 
Last edited:
Upvote 0
Try:
Code:
Sub PosX()
    Application.ScreenUpdating = False
    Dim i As Long, LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 1 To LastRow
        If Cells(i, 1).Value = "POSNX" Then
            Range("A" & i + 0).Range("$h$1").FormulaR1C1 = "=RC[-4]"
        End If
    Next i
    Application.ScreenUpdating = True
End Sub


I'm not sure what you want to do with this line of code:
Code:
Range("A" & i + 0).Range("$h$1").FormulaR1C1 = "=RC[-4]"


Here is another part, you can see the i +0 changes

Sub PosY()




Dim r As Range
Dim i As Long


Set r = Range("A1:A5000")
For i = r.Rows.Count To 1 Step -1
With r.Cells(i, 1)
If .Value = "POSNY" Then

Range("A" & i - 1).Range("$i$1").FormulaR1C1 = _
"=IF(R[1]C[-5]="""","""",R[1]C[-5])"

End If
End With
Next i
End Sub
 
Upvote 0
Have you tried Fluff's code in Post #2 ?
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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