+ Reply to Thread
Results 1 to 6 of 6

Input wrong range value handling

  1. #1
    Registered User
    Join Date
    08-13-2016
    Location
    hong kong
    MS-Off Ver
    MS Office for Home and Student 2016
    Posts
    14

    Input wrong range value handling

    I have an input box for user to input value from specific column, when user input wrong value or empty and press Enter, the program will hang with error. Please advise how to handle this situation ? and how to show message to remind the user ?Thanks.

    Sheets("list").Activate

    Dim yn As Integer

    yn = MsgBox(prompt:="If choose Department, Press Yes", Buttons:=vbYesNo + vbQuestion)
    If yn = vbYes Then

    dept = InputBox("if choose staff ID:")
    Range("a1").AutoFilter Field:=2, Criteria1:=dept
    ActiveSheet.UsedRange.Select
    Selection.copy
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = "result"
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

    Else
    ID = InputBox("Staff ID:")
    Range("a1").AutoFilter Field:=1, Criteria1:=ID
    ActiveSheet.UsedRange.Select
    Selection.copy
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = "result"
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
    xlNone, SkipBlanks:=False, Transpose:=False

    End If

    End With

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Input wrong range value handling

    Hi there,

    Try the code below.

    The macro will first check if a sheet 'result' already exists, and if so, delete it.
    The selection then checks if there is at least one record (2 rows with header), if not exit.

    Let us know if that works for you.

    If not, please upload a 'sanitized' sample workbook with your data so we know what your data looks like.


    Please Login or Register  to view this content.
    If your Question is answered; please mark it SOLVED. If you are happy with a member's solution, say 'Thanks' and click the 'Star' to Add Reputation.

  3. #3
    Registered User
    Join Date
    08-13-2016
    Location
    hong kong
    MS-Off Ver
    MS Office for Home and Student 2016
    Posts
    14

    Re: Input wrong range value handling

    Hi, OROOS
    Sorry for late reply.I have upload my working file for you. Would you please help .
    Attached Files Attached Files

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2405 (Windows 11 23H2 64-bit)
    Posts
    81,417

    Re: Input wrong range value handling

    Administrative Note:

    OPENING POST

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2405 (Windows 11 23H2 64-bit)
    Posts
    81,417

    Re: Input wrong range value handling

    Do not ignore moderation posts.

    NO HELP to be offered until the OP has added code tags to the opening post as required.

  6. #6
    Registered User
    Join Date
    08-13-2016
    Location
    hong kong
    MS-Off Ver
    MS Office for Home and Student 2016
    Posts
    14

    Re: Input wrong range value handling

    Hi, AliGW, is it correct ?

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    [/CODE][/CODE]
    Attached Files Attached Files
    Last edited by missy9413; 08-15-2023 at 09:03 AM. Reason: add file and tag code

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA Help with DATE Input Box & Error Handling
    By OxFACTOR in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-22-2014, 02:12 PM
  2. Cancel out of Input box for error handling for dates
    By hermithead in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2013, 05:43 AM
  3. error handling input box
    By nikko50 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-09-2013, 05:53 PM
  4. [SOLVED] Help Handling Input Data
    By smiteme in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-12-2012, 01:33 PM
  5. Proper handling of "cancel" for range-type input box response (type 8)
    By MCCCLXXXV in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-28-2011, 11:19 AM
  6. handling blank input box
    By daviddoria in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-27-2008, 09:55 PM
  7. histogram, wrong input range
    By Camilla in forum Excel General
    Replies: 1
    Last Post: 11-11-2005, 03:25 AM

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