+ Reply to Thread
Results 1 to 4 of 4

Repeating code

Hybrid View

  1. #1
    Registered User
    Join Date
    02-23-2010
    Location
    oxford
    MS-Off Ver
    Excel 2003
    Posts
    8

    Repeating code

    Hi

    Please be gentle - after 10 years since I hung up my VBA programming hat, I've jumped back in it and to be fair I am really struggling. What I am after is probably dead easy but I need some help please

    Below is my code

    Sub test()
    
    Dim strinput As Variant
    
    
    strinput = Application.InputBox(prompt:="How old is the building?", Title:="WD Data inputer", Default:="", Type:=2)
    
    If strinput = False Then
    Range("A1").Value = ""
    Exit Sub
    End If
    
    If strinput = "" Then
    MsgBox "Please enter a value", vbInformation, "WD Inputer"
    Exit Sub
    End If
    
    If strinput = True Then
    Range("A1").Value = "The building is " & strinput & " years old"
    Exit Sub
    End If
    
    
    End Sub
    ----Basically what I am after is if the value entered is blank then the error message appears and the input box appears again until either a value is entered or the use clicks cancel.

    My head is really hurting, Can someone please help and fill in the missing code as appose to telling me how to do it, cause at this point in time I just cant get my head round it from reading the VBA examples.
    Last edited by Leith Ross; 02-23-2010 at 06:14 PM. Reason: Addded Code Tags

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Repeating code

    Welcome to the forum.

    Maybe like this:
    Sub test()
        Dim sInp    As String
    
        Do Until IsNumeric(sInp)
            sInp = InputBox(Prompt:="How old is the building?", _
                            Title:="WD Data inputer")
            If StrPtr(sInp) = 0 Then
                Range("A1").ClearContents
                Exit Sub
            End If
        Loop
    
        Range("A1").Value = "The building is " & sInp & " years old"
    End Sub
    Last edited by shg; 02-23-2010 at 06:44 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Repeating code

    Without changing too much of what you wrote... this should do the trick.

    Sub test()
    
    Dim strinput As Variant
    
    Do
    strinput = Application.InputBox(prompt:="How old is the building?", Title:="WD Data inputer", Default:="", Type:=2)
    
    If strinput = False Then
    Range("A1").Value = ""
    Exit Do
    End If
    
    If strinput = "" Then
    MsgBox "Please enter a value", vbInformation, "WD Inputer"
    'Exit Sub <-- commented
    End If
    
    If strinput = True Then
    Range("A1").Value = "The building is " & strinput & " years old"
    Exit Do
    End If
    Loop Until not strinput = ""
    
    End Sub
    Last edited by shg; 02-23-2010 at 06:43 PM.

  4. #4
    Registered User
    Join Date
    02-23-2010
    Location
    oxford
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Repeating code

    Thats great guys thanks for your help.

    No doubt I'll be posting again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1