+ Reply to Thread
Results 1 to 4 of 4

Error message if value is noth within range

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Error message if value is noth within range

    Hello,

    I would like to make a macro (Workbook_SheetChange) to display an error message if the value entered is not within a range of 1000000 and 3999999.
    I already have code to display error message if the entered value already exists in the worksheet

    How can I set this up?

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Error message if value is noth within range

    Add another test to the code you already have. The test will be probably (but it depends on the code you already have):
    if target.value<1000000  or target.value> 3999999 then msgbox "Value out of range"
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    06-26-2014
    Location
    Norway
    MS-Off Ver
    10
    Posts
    43

    Re: Error message if value is noth within range

    Adding this gave me an error.

    Below is the code i currently have:

    Option Explicit
    
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
        'Define your variables.
        Dim ws As Worksheet, EvalRange As Range
        
        'Set the range where you want to prevent duplicate entries.
        Set EvalRange = Range("A1:A60")
        
        'If the value entered already exists in the defined range on the current worksheet, throw an
        'error message and undo the entry.
        If WorksheetFunction.CountIf(EvalRange, Target.Value) > 1 Then
            MsgBox Target.Value & " - the bilagsnr already exists on this sheet."
            Application.EnableEvents = False
            Application.Undo
            Application.EnableEvents = True
        End If
        
        'Check the other worksheets in the workbook.
        For Each ws In Worksheets
            With ws
                If .Name <> Target.Parent.Name Then
                    'If the value entered already exists in the defined range on the current worksheet, throw an
                    'error message and undo the entry.
                    If WorksheetFunction.CountIf(Sheets(.Name).Range("A1:A999"), Target.Value) > 0 Then
                        MsgBox Target.Value & " - bilagsnr already exists on the sheet named " & .Name & ".", _
                        16, "Duplicate found!"
                        Application.EnableEvents = False
                        Application.Undo
                        Application.EnableEvents = True
                        Exit For
                    End If
                End If
            End With
        Next ws
    End Sub
    Where should I add the new code?

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Error message if value is noth within range

    How about adding it at the beginning :

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
        'Define your variables.
        Dim ws As Worksheet, EvalRange As Range
        
        'Set the range where you want to prevent duplicate entries.
        Set EvalRange = Range("A1:A60")
    
        if target.value<1000000  or target.value> 3999999 then msgbox "Value out of range"
    
    'or similarily to duplicate also undo
    
    '    if target.value<1000000  or target.value> 3999999 then 
    '        MsgBox Target.Value & " out of allowed range"
    '        Application.EnableEvents = False
    '        Application.Undo
    '        Application.EnableEvents = True
    '    End If
    
    
        'If the value entered already exists in the defined range on the current worksheet, throw an
        'error message and undo the entry.
        If WorksheetFunction.CountIf(EvalRange, Target.Value) > 1 Then
            MsgBox Target.Value & " - the bilagsnr already exists on this sheet."
            Application.EnableEvents = False
            Application.Undo
            Application.EnableEvents = True
        End If
    '...
    By the way - why not use just data validation in the spreadsheet?

+ 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. Replies: 0
    Last Post: 07-31-2013, 06:03 AM
  2. [SOLVED] run-time error “9”: message saying that the “subscript is out of range”
    By IaLa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-31-2013, 04:28 AM
  3. [SOLVED] Not getting error message I need, instead getting Out of Range Error (Error 9)
    By welborns2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2013, 09:10 AM
  4. Error Message 'Subscript Out of Range'
    By sbnjac80 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-24-2009, 01:44 PM
  5. out of range error message
    By juergenkemeter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2006, 02:10 AM

Tags for this Thread

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