VBA: just one loop instead of one for any check

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I have a macro that produces the right result.
The aspect to improuve is in making it quicker: as you can see in this small part, I have a lot of checks to perform in any single cell.
I suppose that checking everything in a single loop could reduce in a significant way the execution time.

Any suggestion?

Code:
'35
For Each cell35 In Sheets("record").Range("BL2:BN" & lrt)
    If Left(cell35.Offset(0, -6).Value, 2) = "35" Then cell35.Offset.Value = "35" & " " & cell35.Offset(0, 3).Value
Next

'39
For Each cell39 In Sheets("record").Range("BL2:BN" & lrt)
    If Left(cell39.Offset(0, -6).Value, 2) = "39" Then cell39.Offset.Value = "39" & " " & cell39.Offset(0, 3).Value
Next cell39

'321
For Each cell321 In Sheets("record").Range("BL2:BN" & lrt)
    If Left(cell321.Offset(0, -6).Value, 3) = "321" Then cell321.Offset.Value = "321" & " " & cell321.Offset(0, 3).Value
Next cell321

'322
For Each cell322 In Sheets("record").Range("BL2:BN" & lrt)
    If Left(cell322.Offset(0, -6).Value, 3) = "322" Then cell322.Offset.Value = "322" & " " & cell322.Offset(0, 3).Value
Next cell322

'323
For Each cell323 In Sheets("record").Range("BL2:BN" & lrt)
    If Left(cell323.Offset(0, -6).Value, 3) = "323" Then cell323.Offset.Value = "323" & " " & cell323.Offset(0, 3).Value
Next cell323

'324
For Each cell324 In Sheets("record").Range("BL2:BN" & lrt)
    If Left(cell324.Offset(0, -6).Value, 3) = "324" Then cell324.Offset.Value = "324" & " " & cell324.Offset(0, 3).Value
Next cell324
 

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
You can do that like
Code:
For Each Cell In Sheets("record").Range("BL2:BN" & lrt)
    If Left(Cell.Offset(0, -6).Value, 2) = "35" Then
      Cell.Value = "35" & " " & Cell.Offset(0, 3).Value
   ElseIf Left(Cell.Offset(0, -6).Value, 2) = "39" Then
      Cell.Value = "39" & " " & Cell.Offset(0, 3).Value
   ElseIf Left(Cell.Offset(0, -6).Value, 2) = "321" Then
      Cell.Value = "321" & " " & Cell.Offset(0, 3).Value
   End If
Next Cell
Just add the other requirements
 
Upvote 0
You can do that like
Code:
For Each Cell In Sheets("record").Range("BL2:BN" & lrt)
    If Left(Cell.Offset(0, -6).Value, 2) = "35" Then
      Cell.Value = "35" & " " & Cell.Offset(0, 3).Value
   ElseIf Left(Cell.Offset(0, -6).Value, 2) = "39" Then
      Cell.Value = "39" & " " & Cell.Offset(0, 3).Value
   ElseIf Left(Cell.Offset(0, -6).Value, 2) = "321" Then
      Cell.Value = "321" & " " & Cell.Offset(0, 3).Value
   End If
Next Cell
Just add the other requirements

Fine, thank's you.
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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