+ Reply to Thread
Results 1 to 5 of 5

Cell = Range("A1") Type mismatch

Hybrid View

NicholasL Cell = Range("A1") Type... 03-23-2014, 01:31 PM
AB33 Re: Cell = Range("A1") Type... 03-23-2014, 01:43 PM
NicholasL Re: Cell = Range("A1") Type... 03-23-2014, 02:45 PM
HaHoBe Re: Cell = Range("A1") Type... 03-23-2014, 02:57 PM
HaHoBe Re: Cell = Range("A1") Type... 03-24-2014, 01:58 AM
  1. #1
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Cell = Range("A1") Type mismatch

    I'm trying to clear all cells on a sheet that are not in the Range between A1:F1

    This is what my code looks like:

    Sub ClearTable()
    Sheets("Tables").Activate
    
    Dim WorkRange As Range
    Dim thisCell As Range
    
    Set WorkRange = ActiveSheet.UsedRange
    For Each Cell In WorkRange
        If Cell = Range("A1") Or Range("B1") Or Range("C1") Or Range("D1") Or Range("E1") Or Range("F1") Then
        Else
        Cell.Clear
        End If
    Next Cell
    End Sub
    The idea was to go through each cell in WorkRange (which is every cell defined by the .UsedRange method -- which I'm assuming just means every non-empty cell in the sheet).

    If the cell is in the range I don't want deleted then nothing happens, else clear the cell.

    On the line "If Cell = Range("A1") Or Range("B1")...." It yields a Type Mismatch error.

    Thoughts?

    Thanks!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Cell = Range("A1") Type mismatch

    If cell = Range("A1") Or cell = Range("B1") Or cell = Range("C1") Or cell = Range("D1") Or cell = Range("E1") Or cell = Range("F1") Then

  3. #3
    Registered User
    Join Date
    06-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2011
    Posts
    64

    Re: Cell = Range("A1") Type mismatch

    Thank you!
    That worked fine, I didn't realize I had to be so explicit in the structure.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Cell = Range("A1") Type mismatch

    Hi, NicholasL,

    why not shorten the code to
    Sub ClearTable2()
    
    Dim WorkRange As Range
    Dim thisCell As Range
    
    With Sheets("Tables")
      Set WorkRange = .UsedRange
      For Each thisCell In WorkRange
        If Intersect(thisCell, .Range("A1:F1")) Is Nothing Then
          thisCell.Clear
        End If
      Next thisCell
    End With
    
    Set WorkRange = Nothing
    End Sub
    or
    Sub ClearTable3()
    
    Dim WorkRange As Range
    Dim thisCell As Range
    
    With Sheets("Tables")
      Set WorkRange = .UsedRange
      For Each thisCell In WorkRange
        Select Case thisCell.Address
          Case "$A$1", "$B$1", "$C$1", "$D$1", "$E$1", "$F$1"
          Case Else
            thisCell.Clear
        End Select
      Next thisCell
    End With
    Set WorkRange = Nothing
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Cell = Range("A1") Type mismatch

    Hi, Hi, NicholasL,,

    if the UsedRange only covers Columns A to F you may use
    Sub ClearTable3()
    Sheets("Tables").UsedRange.Offset(1, 0).ClearContents
    End Sub
    or
    Sub ClearTable4()
    With Sheets("Tables")
      .Range("A2:F" & .Range("A" & Rows.Count).End(xlUp).Row).ClearContents
    End With
    End Sub
    Ciao,
    Holger

+ 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. Instr() function giving error: "Type mismatch"
    By arjun.majumdar in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-22-2013, 04:17 AM
  2. Excel 2010 (Run-time error '13' type mismatch) "Debug" and "Continue" Grayed out.
    By Jeronimo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2012, 06:42 PM
  3. Replies: 5
    Last Post: 08-08-2010, 03:03 PM
  4. [SOLVED] Merged cells cause "Runtime error 13 type mismatch" problem
    By Ralph Malph in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2006, 08:55 AM
  5. [SOLVED] Error Handling to mitigate "Run Time Erorr 13 Type Mismatch"
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2005, 09:05 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