How to execute code for every line that satisfies the condition

PlenkY94

New Member
Joined
Jul 13, 2022
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
Hey!

New to the forum. Just getting in the VBA. Maybe you could help me.

This code is calculating one parameter with changing another for one specific row. I would like to make a condition for example: "IF B19 = "10" then calculate the rest of the code, but I would want that for every row where B is 10.

Any ideas?

VBA Code:
Sub GoalSeek()

Application.ScreenUpdating = False

Static isWorking As Boolean


If Range("'Hid. - Proracun'!B19").Value = "Cijev" Then


If Round(Range("'Hid. - Proracun'!Q19").Value, 4) <> 0 And Not isWorking Then
    isWorking = True
    
    Range("'Hid. - Proracun'!Q19").Value = 0.01
    Range("'Hid. - Proracun'!U19").GoalSeek goal:=0, changingcell:=Range("'Hid. - Proracun'!Q19")
    
    isWorking = False
    
    
End If
   
    
      
End If






Application.ScreenUpdating = True

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
@PlenkY94
Welcome to MrExcel
Try this:
VBA Code:
Sub GoalSeek()

Application.ScreenUpdating = False

Static isWorking As Boolean
Dim n As Long, i As Long

n = Range("B" & Rows.Count).End(xlUp).Row  'find last row with data

For i = 2 To n 'start at row 2
    If Range("'Hid. - Proracun'!B" & n).Value = "Cijev" Then
    
        If Round(Range("'Hid. - Proracun'!Q" & n).Value, 4) <> 0 And Not isWorking Then
            isWorking = True
            
            Range("'Hid. - Proracun'!Q" & n).Value = 0.01
            Range("'Hid. - Proracun'!U" & n).GoalSeek goal:=0, changingcell:=Range("'Hid. - Proracun'!Q" & n)
            
            isWorking = False
            
            
        End If
          
    End If
Next

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Sorry, it should be i instead of n as the counter:
VBA Code:
Sub GoalSeek()

Application.ScreenUpdating = False

Static isWorking As Boolean
Dim n As Long, i As Long

n = Range("B" & Rows.Count).End(xlUp).Row  'find last row with data

For i = 2 To n 'start at row 2
    If Range("'Hid. - Proracun'!B" & i).Value = "Cijev" Then
    
        If Round(Range("'Hid. - Proracun'!Q" & i).Value, 4) <> 0 And Not isWorking Then
            isWorking = True
            
            Range("'Hid. - Proracun'!Q" & i).Value = 0.01
            Range("'Hid. - Proracun'!U" & i).GoalSeek goal:=0, changingcell:=Range("'Hid. - Proracun'!Q" & i)
            
            isWorking = False
            
            
        End If
          
    End If
Next

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Sorry, it should be i instead of n as the counter:
VBA Code:
Sub GoalSeek()

Application.ScreenUpdating = False

Static isWorking As Boolean
Dim n As Long, i As Long

n = Range("B" & Rows.Count).End(xlUp).Row  'find last row with data

For i = 2 To n 'start at row 2
    If Range("'Hid. - Proracun'!B" & i).Value = "Cijev" Then
   
        If Round(Range("'Hid. - Proracun'!Q" & i).Value, 4) <> 0 And Not isWorking Then
            isWorking = True
           
            Range("'Hid. - Proracun'!Q" & i).Value = 0.01
            Range("'Hid. - Proracun'!U" & i).GoalSeek goal:=0, changingcell:=Range("'Hid. - Proracun'!Q" & i)
           
            isWorking = False
           
           
        End If
         
    End If
Next

Application.ScreenUpdating = True

End Sub
Thank you for your help!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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