code help

Kevin0427

Board Regular
Joined
Mar 31, 2016
Messages
69
The following code never gets to the ELSE and always displays the message box. I suspect that it is because when I print the variables lastrow is right justified and rowfound is left justified so they are not both numbers. But I am not sure and if so I don't know how to fix it.

Code:
'Get Row Number
    rowfound = InputBox("What is the row number of the project?", "Input Row Number")
    
    'If Input box is cancelled exit sub
    If rowfound = "" Then
    Exit Sub
    End If
    
    'Is row number valid?
    lastrow = Sheets("Tracking Log").Range("B:B")(Rows.Count, 1).End(xlUp).Row
    
    Debug.Print lastrow
    Debug.Print rowfound
    
    If rowfound < 15 Or rowfouund > lastrow Then
        MsgBox "Row Number not valid", vbCritical, "Invalid Row Number"
        Exit Sub
    
    Else
 
I was also thinking you need to take care with the data types since you are getting a string answer. So dealing that I would write:

Code:
Option Explicit

Sub foo()
Dim rsp As String
Dim rowfound As Long
    
    rsp = InputBox("What is the row number of the project?", "Input Row Number")
    
    If rsp = "" Then
        Exit Sub
    End If
    
    If Not IsNumeric(rsp) Then
        MsgBox "Row Number must be a numeric value", vbCritical, "Invalid Row Number"
        Exit Sub
    Else
        rowfound = CLng(rsp)
    End If
    
    If (rowfound < 15) Then
        MsgBox "Row Number must be greater than or equal to 15", vbCritical, "Invalid Row Number"
        Exit Sub
    End If
    
    If (rowfound > Sheets("Tracking Log").Range("B:B")(Rows.Count, 1).End(xlUp).Row) Then
        MsgBox "Row Number cannot be greater than the last available row in Tracking Log", vbCritical, "Invalid Row Number"
        Exit Sub
    End If
    
    MsgBox "Else Reached (all validations passed)"
 
 End Sub
 
Last edited:
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,224,823
Messages
6,181,178
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