VBA loop with step / incrament

Abvlecxe

Board Regular
Joined
Sep 10, 2015
Messages
53
Hi I have the following code below which works but as you can see is very manual in terms of the loop until the input entry is correct. Basically I want the code to check that the number the user has entered equals 19 or any row number from 19 with a step /increment of 7 e.g. 26,33,40 etc. up to a maximum of 1002 so in effect my validation range is 19 to 1002, with step of 7 from 19

Any help would be appreciated, thanks.


sub InsertRows()

Dim lastRow As Long
Dim Row1 As Long
Dim Row2 As Long
Dim myvalue As Variant
Dim i As Long
Dim CancelTest As Variant
Dim Row As Range
Dim myPassword As String
myPassword = "Password101"

Application.ScreenUpdating = False

lastRow = 0
Do
myvalue = InputBox("Insert Rows Starting From Input Number:" & Chr(10) & _
"e.g. 19, 26, 33 (Multiples of 7)")
If StrPtr(myvalue) = 0 Then Exit Sub
If Not IsNumeric(myvalue) Then MsgBox "Numeric Values Only" & Chr(10) & _
"Starting From Row 19 In Multiples Of 7"
Loop Until Val(myvalue) = 19 Or myvalue = 26 Or myvalue = 33 Or myvalue = 40 Or myvalue = 47 Or myvalue = 54 Or myvalue = 61 Or myvalue = 68 Or myvalue = 75 Or myvalue = 82 Or myvalue = 89 Or myvalue = 96 _
Or myvalue = 103
If MsgBox("Are you sure?", vbYesNo) = vbNo Then Exit Sub
With Sheet1
.Select
.Unprotect Password:=myPassword

ActiveSheet.Outline.ShowLevels RowLevels:=2

lastRow = Cells(Rows.Count, "B").End(xlUp).Row
Row1 = lastRow - 6
Row2 = lastRow
Rows(Row1 & ":" & Row2).Select
Selection.Copy
End With

With Sheet1
.Select
Range("a" & myvalue).Select
Selection.Insert Shift:=xlDown
On Error GoTo 0
Application.CutCopyMode = False
lastRow = 0
.Range("c11").Select
.Protect Password:=myPassword, AllowFiltering:=True, AllowFormattingCells:=True, DrawingObjects:=False, Contents:=True, UserInterfaceOnly:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True

End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Try changing this:
If Trim(txtdltfrom.Value > txtdltto.Value) Then

to this:
If Trim(txtdltfrom.Value) > Trim(txtdltto.Value) Then
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try changing this:
If Trim(txtdltfrom.Value > txtdltto.Value) Then

to this:
If Trim(txtdltfrom.Value) > Trim(txtdltto.Value) Then



Thanks for reply, i saw this and thought it would work as i can see i was missing the "Trim" reference but it didn't i still get the same error.

However, when i change the > to < it works, but it shouldn't and I've no idea why that works as it's round the wrong way, as i want to stop if the from value is bigger than the to value
 
Upvote 0
For debugging, try adding the blue line. A message box will pop up, see if both textboxes have the correct values.

With Sheet1
.Select
.Protect Password:=myPassword, AllowFiltering:=True, AllowFormattingCells:=True, DrawingObjects:=False, Contents:=True, UserInterfaceOnly:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True
End With

MsgBox "txtdltfrom = " & txtdltfrom.Value & " : " & "txtdltto = " & txtdltto.Value

If Trim(txtdltfrom.Value) > Trim(txtdltto.Value) Then
MsgBox "Delete End Row Cannot be Before Delete Start Row"
txtdltto.SetFocus
Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,223,708
Messages
6,174,002
Members
452,542
Latest member
Bricklin

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