+ Reply to Thread
Results 1 to 14 of 14

Runtime error '13' on Upper Case Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    08-21-2013
    Location
    Jacksonville, FL, USA
    MS-Off Ver
    Excel 2010
    Posts
    61

    Runtime error '13' on Upper Case Macro

    The following macro changes any lower case text entered in a cell to upper case; however, I'm getting a runtime error '13' on the .Value line when pressing the Delete key to remove cell contents. How do I resolve this error?

    Option Explicit
    ------------------
    Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
    If Not .HasFormula Then
    .Value = UCase(.Value)
    End If
    End With
    End Sub

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Runtime error '13' on Upper Case Macro

    Hi,

    change your if statement to:

    If Not .HasFormula And .Value <> "" Then
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    08-21-2013
    Location
    Jacksonville, FL, USA
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Runtime error '13' on Upper Case Macro

    Still getting Run-time '13' error: Type mismatch.
    Happens when a cell is cleared (.ClearContents).
    If Not .Has...... line highlighted yellow.

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If Not .HasFormula And .Value <> "" Then
            .Value = UCase(.Value)
        End If
    End With
    End Sub

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Runtime error '13' on Upper Case Macro

    The code works fine for me when ClearContents is used to clear a cell or the cell is deleted manually or with code.

    I can only replicate the error when multiple cells are cleared/deleted.

    So either disable events before you clear or check the no of cells that have been changed.
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        With Target
            If .Cells.Count >1 Then Exit Sub
    
            If Not .HasFormula And .Value <> "" Then
                .Value = UCase(.Value)
            End If
        End With
    End Sub
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    08-21-2013
    Location
    Jacksonville, FL, USA
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Runtime error '13' on Upper Case Macro

    This needs to work when either single or multiple cells are cleared.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Runtime error '13' on Upper Case Macro

    Why do you need the code to work if cells are being cleared?

    If they are being cleared then there's no value to convert to upper case.

  7. #7
    Registered User
    Join Date
    08-21-2013
    Location
    Jacksonville, FL, USA
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Runtime error '13' on Upper Case Macro

    The macro works in a budge/execution worksheet. As the month progresses, entries are made (everything in UCase to make easy to read), and at the end of the month, the worksheet is copied to create a new month spreadsheet, whereupon last month entries are removed - but account titles, vendor names, income sources, etc. remain - thus the ClearContents. Also, if a mistake is made in a single cell and contents later deleted and replaced, i do not want a error on the UCase code. Further if i have a UCase macro error, it prevents subsequently-occuring macros from working. The code is now working as needed. Thanks very much for your help.
    Last edited by Phil Hageman; 04-03-2015 at 12:11 PM.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Runtime error '13' on Upper Case Macro

    Always better to handle multiple change
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range
        Application.EnableEvents = False
        For Each r In Target
            If Not r.HasFormula And r.Value <> "" Then
                r.Value = UCase(r.Value)
            End If
        Next
        Application.EnableEvents = True
    End Sub

  9. #9
    Registered User
    Join Date
    08-21-2013
    Location
    Jacksonville, FL, USA
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Runtime error '13' on Upper Case Macro

    Jindon, Could you elaborate on this? What improvement results if I use your code rather than mine? I'm always open to improvement and want to learn something from your input. r/Phil

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Runtime error '13' on Upper Case Macro

    e.g

    Copy multiple cells and paste to anywhere or select multiple cells and enter any string then enter with Ctrl + Enter.

    It will convert all the cells that are changed.

    Your code will ignore the change made in multiple cells at a time.

  11. #11
    Registered User
    Join Date
    08-21-2013
    Location
    Jacksonville, FL, USA
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Runtime error '13' on Upper Case Macro

    Please excuse my ignorance on this, but what is getting "converted?"

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Runtime error '13' on Upper Case Macro

    Convert small cap characters to upper case?
    r.Value = UCase(r.Value)

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Runtime error '13' on Upper Case Macro

    Why do you want this code to run on empty cells?

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Runtime error '13' on Upper Case Macro

    Phil, the code converts any cell contents to upper case, when the cell contents change. Your original code skipped the conversion if there were multiple cells, Jindon's converts each of the cells that changed even if multiple cells are modified at the same time. Given that this is a worksheet change event, the expectation is not that blanks are converted, but rather skipped.

+ 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] VBA macro UPPER auto case
    By Peterino in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 11-17-2014, 03:34 PM
  2. [SOLVED] Code fails with runtime error 91 in some case.
    By lavinius in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-02-2013, 05:36 AM
  3. Amend Upper Case macro to ignore brackets
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2008, 05:30 PM
  4. [SOLVED] Changing file in all upper case to upper and lower case
    By Sagit in forum Excel General
    Replies: 14
    Last Post: 08-25-2005, 10:05 PM
  5. Replies: 1
    Last Post: 03-09-2005, 05:06 PM

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