[vba] How can I make a loop on this code?

roman10

New Member
Joined
Aug 17, 2011
Messages
2
Hello to all, I wrote something like this:

[..
..
..
If Range("C7").Value = False then

Range("B7").Copy Range("B2")
ThisWorkbook.Refreshall
Thisworkbook.SaveAs Range("B7")

Elseif Range("C7").Value = True then
End if

If Range("C8").Value = False then

Range("B8").Copy Range("B2")
ThisWorkbook.Refreshall
Thisworkbook.SaveAs Range("B8")

Elseif Range("C8").Value = True then
End if

If Range("C9").Value = False then

Range("B9").Copy Range("B2")
ThisWorkbook.Refreshall
Thisworkbook.SaveAs Range("B9")

Elseif Range("C9").Value = True then
End if

...
...
]

and go on repeating these steps for C10, C11, etc. It works, but I think that this could be written in few rows of code rather than on pages of code.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
something like
Code:
    For i = 7 To 12 ' change as required
        If Range("C" & i).Value = False Then
        
            Range("B" & i).Copy Range("B2")
            ThisWorkbook.RefreshAll
            ThisWorkbook.SaveAs Range("B" & i)
        
        ElseIf Range("C" & i).Value = True Then
        End If
    Next i
 
Upvote 0
something like
Code:
    For i = 7 To 12 ' change as required
        If Range("C" & i).Value = False Then
        
            Range("B" & i).Copy Range("B2")
            ThisWorkbook.RefreshAll
            ThisWorkbook.SaveAs Range("B" & i)
        
        ElseIf Range("C" & i).Value = True Then
        End If
    Next i

Thanks really a lot Weaver!

This works great!

Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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