Excel VBA to Loop

sirmacademy

New Member
Joined
Aug 12, 2018
Messages
17
Hello everyone, I need to make this code run as one. Thank you for your help.
Sub InsertSaturday()
Dim i As Integer
i = 6
Do While i < 100
Cells(i, 3).Value = "Saturday"
i = i + 6
Loop
End Sub


Sub InsertSunday()
Dim j As Integer
j = 7
Do While j < 100
Cells(j, 3).Value = "Sunday"
j = j + 7
Loop
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hello,
you can combine both like,,,
Code:
Sub InsertSaturdaySunday()
 Dim i As Integer
 i = 6
 Do While i < 100
 Cells(i, 3).Value = "Saturday"
 Cells(i+1, 3).Value = "Sunday" ' just add one
 i = i + 6
 Loop
 End Sub
 
Last edited:
Upvote 0
Hi,
taking both your codes, following combined version should produce same result for you


Code:
Sub InsertSaturdayAndSunday()
    Dim i As Integer, j As Integer
    i = 6: j = 7
    Do While i < 100
        Cells(i, 3).Value = "Saturday"
        If j < 100 Then Cells(j, 3).Value = "Sunday"
        i = i + 6: j = j + 7
    Loop
End Sub

Dave
 
Upvote 0
Hello,
you can combine both like,,,
Code:
Sub InsertSaturdaySunday()
 Dim i As Integer
 i = 6
 Do While i < 100
 Cells(i, 3).Value = "Saturday"
 Cells(i+1, 3).Value = "Sunday" ' just add one
 i = i + 6
 Loop
 End Sub
You can write this macro a little more compactly like this...
Code:
[table="width: 500"]
[tr]
	[td]Sub InsertSaturdaySunday()
  Dim i As Integer
  For i = 6 To 100 Step 7
    Cells(i, 3).Resize(2).Value = [{"Saturday";"Sunday"}]
  Next
End Sub[/td]
[/tr]
[/table]
This function can also be written with using a loop...
Code:
[table="width: 500"]
[tr]
	[td]Sub InsertSaturdayAndSunday2()
  Range("C6:C99") = Evaluate("IF(MOD(ROW(6:99)-6,7)=0,""Saturday"",IF(MOD(ROW(6:99)-6,7)=1,""Sunday"",C6:C99))")
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
This code works very fine.
Sub InsertSaturdayAndSunday2() Range("C6:C99") = Evaluate("IF(MOD(ROW(6:99)-6,7)=0,""Saturday"",IF(MOD(ROW(6:99)-6,7)=1,""Sunday"",C6:C99))")End Sub

But it should be Saturday and Sunday will be inserted and it will not replace the value.
 
Upvote 0
But it should be Saturday and Sunday will be inserted and it will not replace the value.
Does this macro do what you want...
Code:
Sub InsertSaturdaySunday()
  Dim i As Integer
  For i = 6 To 100 Step 7
    Cells(i, 3).Resize(2).Insert xlToRight
    Cells(i, 3).Resize(2).Value = [{"Saturday";"Sunday"}]
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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