Problem in looping with rows

lorenzo79

New Member
Joined
Feb 18, 2022
Messages
11
Office Version
  1. 2019
Dear all ,

i have a problem in looping rows.
i have below macro that should looping column B from row13 ,instead is also looping row12 (that correspond to my table heather) ...
i have tried the code below but not working ...
can someone help please .
basicaly i need to find last row of column B and loop back untill row13 .
Many tks to anyone for help .

here below the code :

VBA Code:
Dim lRow As Long
Dim Ring As Range

lRow = Cells(Rows.Count, 2).End(xlUp).Row
 
For Each Ring In Range("B13:B" & lRow)
  ActiveSheet.Unprotect
  Dim Rng As Range
  Dim foundrange As Range

  Dim wb As Workbook
  Dim fFilename As String

  test1 = Ring.Value
  Ring.EntireRow.Copy
  Ring.Offset(0, 18).Value = "OK"
  fFilename = "X:\CargoluxItalia\COMMON\RISK ANALYISIS\riskregister.xlsm"
  SetAttr fFilename, vbNormal
  Set wb = Workbooks.Open(fFilename)
  Set foundrange = Workbooks("riskregister").Sheets("sheet1").Range("b:b").Find(test1, , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
    If Not foundrange Is Nothing Then
        Workbooks("riskregister").Sheets("Sheet1").Select
        foundrange.Offset(0, -1).Select
        selection.PasteSpecial
        Application.CutCopyMode = False
    Else
        LastRow = Workbooks("riskregister").Sheets("sheet1").Cells(Rows.Count, 2).End(xlUp).Row + 1
        Workbooks("riskregister").Sheets("Sheet1").Rows(LastRow).PasteSpecial
        Application.CutCopyMode = False
        Ring.Offset(0, 12).Value = OK
    End If
    
Next
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Do you have any data in column B past row 12?
If not, then your "lRow" calculation may be returning 12 (or lower).

If you do not want to run your code if the last row is 12 or lower, after the "lRow" calculation, you can add this line of code:
VBA Code:
If lRow < 13 Then Exit Sub
 
Upvote 0
Did you even attempt my suggestion?
 
Upvote 0
HI, sure i have just now tried but will exit the SUB without performing it for the other rows...
 
Upvote 0
i have also try this but still not working
VBA Code:
lRow = Cells(Rows.Count, 2).End(xlUp).Row -12
 
Upvote 0
i have also try this but still not working
VBA Code:
lRow = Cells(Rows.Count, 2).End(xlUp).Row -12
I don't see what that would accomplish.
What exactly are you trying to do there?

What exactly is the value of "lRow"?
If you are not sure, add a Message Box after the calculation in your code:
Rich (BB code):
lRow = Cells(Rows.Count, 2).End(xlUp).Row
MsgBox lRow
 
Upvote 0
How was it solved?
What was the issue?
 
Upvote 0
insted of using for each to go trought the various rows i user below code to have the loop starting from row 13 :

For r = 13 To lRow
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,435
Members
448,898
Latest member
dukenia71

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