Change Code - Input Boxes

imcfcl

New Member
Joined
Sep 23, 2011
Messages
18
I have shown below a small part of code that I use for creating new worksheets. It ask's the user for all of the data that needs to be shown on the new sheet.
Code:
    Sheets("NewSheetInfo").Select
    Range("c3").Select
    ActiveCell.Value = InputBox("Door Number?")
        Range("d3").Select
    ActiveCell.Value = InputBox("Location on Site?")
        Range("e3").Select
    ActiveCell.Value = InputBox("Door Type?")
        Range("f3").Select
    ActiveCell.Value = InputBox("Manufacturer?")
        Range("g3").Select
    ActiveCell.Value = InputBox("Motor Type?")
        Range("h3").Select
    ActiveCell.Value = InputBox("Operation - Electric/Manual/Automatic ?")
        Range("i3").Select
    ActiveCell.Value = InputBox("Usage? High or Low")

I need to add something to the above code for the first input box (door number) by which Column C, in a sheet called 'Database' will be checked to see if the entered number already exists. If the number has already been used, then everything messes up further down the line.

If the number entered already exists, I need an 'OK' box telling the user that the number they tried is already in use, and then the same input box for the user to try a different number. It would also be good if there could be a 'cancel' button to end sub if user doesn't have a unique number to use....

Hope that makes sense? Thanks in advance
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Start by doing a manual Find in column C on the Database sheet with the macro recorder and integrate that into your code.
 
Upvote 0
Here's some code you can try. I'm not sure if I've stuck it at the right point in your code. However it does pick up numbers in the Database column C range that I enter into the inputBox. Hope it helps. It's something to work on and improve.
Code:
Sheets("NewSheetInfo").Select
    Range("c3").Select
 
Set ws = Sheets("Database")
Set Rng = ws.Range(ws.Range("C1"), ws.Range("C" & Rows.Count).End(xlUp))
 
Do
ValidEntry = True
 
    ActiveCell.Value = InputBox("Door Number?")
 
    For Each Dn In Rng
 
    If ActiveCell.Value = Dn Or ActiveCell.Value = "" Then
 
    ValidEntry = False
 
    End If
    Next Dn
 
    If ValidEntry = False Then
    MsgBox ("Number in use, choose another")
    End If
 
    Loop Until ValidEntry
 
MsgBox("number not found") <-- remove     
 
      Range("d3").Select
   ActiveCell.Value = InputBox("Location on Site?")
 
Last edited:
Upvote 0
Minor modifcation - exits sub if user enters nothing or cancels:

Code:
Do
ValidEntry = True
       
    ActiveCell.Value = InputBox("Door Number?")
    If ActiveCell.Value = "" Then
     Exit Sub
    End If
    
    For Each Dn In Rng
 
    If ActiveCell.Value = Dn Then
     
    ValidEntry = False
    
    End If
    Next Dn
      
    If ValidEntry = False Then
    MsgBox ("Number in use, choose another")
    End If
         
    Loop Until ValidEntry
    MsgBox ("Good Number")
 
Upvote 0
Hi there,

apologies for missing this, I managed to knock something up before I realised you had responded.

I just added a cell doing a vlookup on the Door Number in Col C in the database, and and iferrror cell in C4 and C5, which meant if there number wasn't there, vlookup would error, and c5 would read 'continue'.

Does the below look ok? Or woul dyour code be better?

Code:
    Sheets("NewSheetInfo").Select
    
Dim test As String
 Do
    Range("c3").Select
    ActiveCell.Value = InputBox("Door Number?")
    
    If ActiveCell.Value = "" Then
    Sheets("NewSheetInfo").Visible = False
    Sheets("Home").Activate
    MsgBox = "You Cancelled"
    Exit Sub
    End If
    
    test = Range("C5").Value
    MsgBox "Door Reference number already Exists - Pick another number"
    Loop Until test = "continue"
 
Upvote 0
Morning GTO,

I'm still very new to VBA, a user form wasn't something I thought off, everything seems to work as it is now.

I'm just always interested in other peoples ways and means... as I say, I'm new to it all but do want to get better.
 
Upvote 0
If your code works for you and you understand what it is doing then use that.
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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