Greater Than and Less Than Logic

Partjob

Board Regular
Joined
Apr 17, 2008
Messages
139
Hi
I am trying to restrict where a row can be inserted on a sheet and I have this working as follows
The user must pick a row between the Low and High row variable or it will loop through and they have to try again

Code:
Do
    MyRow = Application.InputBox("Enter Row Number for Insertion", "WHERE TO INSERT", Default:=69, Type:=1)
    If MyRow = 0 Then Exit Sub: Rem cancel pressed
    MyRow = Int(MyRow)
    Loop Until (LowRow < MyRow And MyRow < HighRow)

I want to put a little message just before the Loop until line as follows, just to explain that they have made an error and what they need to do

Code:
        If Not LowRow < MyRow And MyRow < HighRow Then
        MsgBox "That is not allowed", vbOKOnly, "INCORRECT ROW SELECTION"
        End If

I can not get the logic correct though, I have tried various permutations of this with no success.
I have put the one that I believe should work but doesn't.
All I have really done is used "Not" on the If to reverse the logic but it fails.

Thanks in advance

David (Partjob)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi
I am trying to restrict where a row can be inserted on a sheet and I have this working as follows
The user must pick a row between the Low and High row variable or it will loop through and they have to try again

Code:
Do
    MyRow = Application.InputBox("Enter Row Number for Insertion", "WHERE TO INSERT", Default:=69, Type:=1)
    If MyRow = 0 Then Exit Sub: Rem cancel pressed
    MyRow = Int(MyRow)
    Loop Until (LowRow < MyRow And MyRow < HighRow)

I want to put a little message just before the Loop until line as follows, just to explain that they have made an error and what they need to do

Code:
        If Not LowRow < MyRow And MyRow < HighRow Then
        MsgBox "That is not allowed", vbOKOnly, "INCORRECT ROW SELECTION"
        End If

I can not get the logic correct though, I have tried various permutations of this with no success.
I have put the one that I believe should work but doesn't.
All I have really done is used "Not" on the If to reverse the logic but it fails.

Thanks in advance

David (Partjob)

I think the problem here is that you are using AND instead of OR.

The value entered cannot be both greater than highrow and less than lowrow

Code:
Sub pick_row()
Dim lowROW As Long, highROW As Long, myROW As Long
Dim rng As Range
Dim ws As Worksheet
Dim varI As Variant

Set ws = ActiveSheet
lowROW = 75
highROW = 125
varI = False
Do Until varI = True
    myROW = Application.InputBox("Enter Row Number for Insertion", "WHERE TO INSERT", Default:=69, Type:=1)
    If myROW = 0 Then
        Exit Sub: Rem cancel pressed
    End If
    myROW = Int(myROW)
    If myROW < lowROW Or myROW > highROW Then
        MsgBox "That is not allowed", vbOKOnly, "INCORRECT ROW SELECTION"
    Else
        MsgBox "Good selection"
        varI = True
    End If
Loop
End Sub
 
Upvote 0
Thank you, as this is work it will be tested tomorrow, I can't see why this won't work it looks right to me.
Seems quite simple when you see it correct.
Thanks again
 
Upvote 0

Forum statistics

Threads
1,221,527
Messages
6,160,342
Members
451,638
Latest member
MyFlower

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