How to loop a VBA 4 times

keranali

Rules Violation
Joined
Oct 4, 2010
Messages
234
Office Version
  1. 365
Platform
  1. Windows
Hi All how do you make this VBA

Code:
Sub MoveLeft()
       
       Dim pw     As String
    pw = Application.InputBox("Enter password")
    Select Case pw
        Case "6583546"
 
    
    
    Dim Blnks As Range, B As Range
    Dim LR As Long, r As Long, cols As Long
    
    Const ChkCols As String = "C:E,F:I,K:N"
    
    Application.ScreenUpdating = False
    LR = Range("C" & Rows.Count).End(xlUp).Row
    For r = 2 To LR
        On Error Resume Next
        Set Blnks = Intersect(Rows(r), Range(ChkCols)).SpecialCells(xlCellTypeBlanks)
        On Error GoTo 0
        If Not Blnks Is Nothing Then
            For Each A In Blnks.Areas
                cols = A.Columns.Count
                A.Offset(, cols + 1).Insert Shift:=xlToRight
                A.Delete Shift:=xlToLeft
            Next A
            Set Blnks = Nothing
        End If
    Next r
    Application.ScreenUpdating = True

       Case Else
            Exit Sub
    End Select

End Sub

Run 4 times without having to type the password 4 times


Thanks
K
 

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.
Hi keranali,

Try this:

Code:
Sub MoveLeft()
       
    Dim pw As String    
    Dim Blnks As Range, _
        B As Range
    Dim LR As Long, _
        r As Long, _
        cols As Long
    Dim intLoopCount As Integer
    
    Const ChkCols As String = "C:E,F:I,K:N"
    
    pw = Application.InputBox("Enter password")
    
    Select Case pw
        Case "6583546"
    
            For intLoopCount = 1 To 4
                Application.ScreenUpdating = False
                LR = Range("C" & Rows.Count).End(xlUp).Row
                For r = 2 To LR
                    On Error Resume Next
                    Set Blnks = Intersect(Rows(r), Range(ChkCols)).SpecialCells(xlCellTypeBlanks)
                    On Error GoTo 0
                    If Not Blnks Is Nothing Then
                        For Each A In Blnks.Areas
                            cols = A.Columns.Count
                            A.Offset(, cols + 1).Insert Shift:=xlToRight
                            A.Delete Shift:=xlToLeft
                        Next A
                        Set Blnks = Nothing
                    End If
                Next r
                Application.ScreenUpdating = True
            Next intLoopCount

       Case Else
            Exit Sub
    End Select

End Sub

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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