excel vba to loop through cells and fix via an input box

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
528
Office Version
  1. 365
Platform
  1. Windows
hi all

i have this below excel sheet
Book1
ABC
1item #quantity description
2123452apples
3010004banana
401001,555555oranges
5010026kiwis
6010038watermelon
701004,6666610pears
Sheet1


i would like an excel VBA that loops through column A and finds any cell that is greater then 5 digits if it finds it i want an input box to be able to change it and the input box should only accept 5 digits numeric values

thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Perhaps something like this-

VBA Code:
Sub fivedig()

    Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
    Dim rng As Range, cel As Range
    
    Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        For Each cel In rng
            If IsNumeric(cel) Then
                Do Until Len(cel) = 5
                    If Not Len(cel) = 5 Then
                        cel = InputBox("The currrent Cell is->   " & cel & "   <-Please correct this so that it is a 5 digit number.")
                        If cel = vbNullString Then Exit Sub
                    End If
                Loop
        End If
    Next
    
End Sub
 
Upvote 0
Another option:
VBA Code:
Sub BORUCH()
Dim c As Range, sVar As String
    For Each c In Range("A2", Cells(Rows.Count, "A").End(xlUp))
       If Not c Like "#####" Then
           Do
               sVar = Application.InputBox("Insert 5 digit:", Type:=2)
               
               If sVar = False Then Exit Sub  'if user cancel
                   If Not sVar Like "#####" Then
                       MsgBox "Incorrect ... "
                   Else
                       c = sVar
                       Exit Do
                   End If
           
           Loop While Not sVar Like "#####"
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,665
Members
449,045
Latest member
Marcus05

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