+ Reply to Thread
Results 1 to 5 of 5

Current Code not working

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Current Code not working

    Have to code but getting problem if more then one character is in C13. Works perfect if only one character is typed in cell,how can I change
    it so if more then one invalid character is in cell? Thanks for any suggestions. Z
    For example this get error 45 elm. 4/23/2015
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim str, i As Long
        
        str = Array(".", "/", "\")
    
        With Range("C13")
            For i = LBound(str) To UBound(str)
                If InStr(1, .Value, str(i)) > 0 Then
    
                jobbox1.Show
                  
                    End If
            Next i
        End With
    End Sub

    This is the code on userform yes button

    Private Sub CommandButton1_Click()
    Dim str, i As Long
    
         'List of characters
        str = Array(".", "/", "\")
    
        With Range("C13")
            For i = LBound(str) To UBound(str)
                If InStr(1, .Value, str(i)) > 0 Then
                    
                        .Value = Replace(.Value, str(i), "")
                    End If
                
            Next i
        End With
         Unload Me
         
        Range("C13").Select
        
    End Sub
    Last edited by zplugger; 04-09-2015 at 10:04 AM.

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Current Code not working

    There is no need to check with InStr. Just loop trough invalid chars and use the replace. If the char is not found nothing will happen.

    Private Sub CommandButton1_Click()
    Dim str, i As Long
    
         'List of characters
        str = Array(".", "/", "\")
    
        With Range("C13")
            For i = LBound(str) To UBound(str)
                        .Value = Replace(.Value, str(i), "")
            Next i
        End With
        Unload Me  
    End Sub
    If you are pleased with a member's answer then use the Star icon to rate it.

  3. #3
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Current Code not working

    Thanks buran
    Getting error still? This only happens if more then one character is inputed.
    Form already displayed;can't show modally

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Current Code not working

    Oh, I misunderstood your problem. Sorry

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim str As Variant
        Dim i As Long
        Dim bInvalidChar As Boolean
    
        If Not Intersect(Target, Range("C13")) Is Nothing Then    'This will prevent rest of the code run if C13 has not changed
            str = Array(".", "/", "\")
            With Range("C13")
                bInvalidChar = False
                For i = LBound(str) To UBound(str)
                    If InStr(1, .Value, str(i)) > 0 Then
                        bInvalidChar = True
                    End If
                Next i
            End With
            If bInvalidChar Then    'Invalid Char Found
                jobbox1.Show
            End If
        End If
    End Sub
    Private Sub CommandButton1_Click()
        Dim str As Variant
        Dim i As Long
        'List of characters
        str = Array(".", "/", "\")
    
        With Range("C13")
            For i = LBound(str) To UBound(str)
                .Value = Replace(.Value, str(i), "")
            Next i
        End With
        Unload Me
    End Sub

  5. #5
    Forum Contributor
    Join Date
    03-05-2009
    Location
    usa
    MS-Off Ver
    Excel 2016 32Bit
    Posts
    1,173

    Re: Current Code not working

    Thanks buran, seems to be working now.

+ 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. [SOLVED] Set PivotTable Filter to Current Day, Current Week, Current Month, or Current Year
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2014, 08:31 AM
  2. [SOLVED] VBA code to save current worksheet as temporary PDF file and then add to my existing code
    By brianfromla in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-20-2014, 08:35 AM
  3. [SOLVED] Need vba code for my current working formula
    By devawad in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-08-2014, 12:32 AM
  4. Code to insert current date (but NOT current time)
    By mjwillyone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-13-2013, 09:37 AM
  5. [SOLVED] add current time (hh:mm:ss) as well as current date (dd:mm:yyyy) into the code
    By papasideris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2012, 02:29 PM

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