+ Reply to Thread
Results 1 to 3 of 3

RunTIme Error 13 when trying to find last column with data in specific range.

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,406

    RunTIme Error 13 when trying to find last column with data in specific range.

    Need to find the last cell containing data in any row between Col D and the column before the one headed "Revision reviewed" (Col P in this example, but has to be "variable" as extra cols will be added).

    Code below throws a "RUN TIME ERROR 13" at that point.

    Without it, cannot determine the ranges to check and decide whether a Column should be hidden or not.

    Spreadsheet attached shows the "Start" position in Cols B - Q, and Cols S - Z show what the end result should be.

    Option Explicit
    Dim d As Long, f As Long, q As Long, x As Long
    Dim SelRange As Range, rng As Range
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    'HIDE unused Date cells for Patient Number selected
    
        With Sheet1
    
    'Find the last row
        f = .Cells(.Rows.Count, "B").End(xlUp).Row
        If f < 7 Then f = 7
    
    'Set range to check as Col B ("Patient Number")
        Set SelRange = .Range(.Cells(7, 2), .Cells(f, 2))
        
    'Check if active cell is in the Date range
        If Not Intersect(SelRange, Target) Is Nothing Then
        
    'Exit sub if cell is blank
        If Target.Value = "" Then
        Exit Sub
    
        Else:
    'Set the row you are clicking
        x = ActiveCell.Row
        
    'Find "Revision reviewed" header and go to column before it
    
        d = Application.Match("Revision reviewed", .Range("6:6"), 0)
        
        
    '*****THROWS RUN TIME ERROR 13*******
        
    'Find the last column with data in any row in Date range
        q = .Range(.Cells(7, 4), .Cells(f, d - 1)).Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
        
     'HIDE subsequent blank columns after the last date
        .Columns(q + 1, d - 1).EntireColumn.Hidden = True
        
    'Then HIDE blank columns in Active row
        For Each rng In .Range(.Cells(x, 4), .Cells(x, q - 1))
        If rng.Value = "" Then
        rng.EntireColumn.Hidden = True
        End If
        Next rng
        
        End If
        
     End If
     
     End With
     
    End Sub
    All solutions, suggestions and alternatives welcome as ever.

    Ochimus
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,662

    Re: RunTIme Error 13 when trying to find last column with data in specific range.

    Dim q As Variant
    'Find the last column with data in any row in Date range
     Set q     = .Range(.Cells(7, 4), .Cells(f, d - 1)).Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    
     'HIDE subsequent blank columns after the last date
        If Not q Is Nothing Then
            .Columns(q.Column  + 1).Resize(d - q.Column).EntireColumn.Hidden = True
        End If
    Last edited by protonLeah; 10-18-2019 at 09:08 PM.
    Ben Van Johnson

  3. #3
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,406

    Re: RunTIme Error 13 when trying to find last column with data in specific range.

    Ben,

    Many thanks for prompt response.

    Had to tweak your concept slightly, but following Code snippet now hides all columns correctly.

    Dim q As Variant
    'Find the last column with data in any row in Date range
        Set q = .Range(.Cells(7, 4), .Cells(f, d - 1)).Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    
     'HIDE subsequent blank columns after the last date
        If Not q Is Nothing Then
            .Range(.Columns(q.Column + 2), Columns(d - 1)).EntireColumn.Hidden = True
        p = q.Column
        
        End If
        
    'Then HIDE blank columns in Active row
        For Each rng In .Range(.Cells(x, 4), .Cells(x, p - 1))
        
        If rng.Value = "" Then
        rng.EntireColumn.Hidden = True
        End If
        Next rng
        
     End If
    Thanks again.

    Can now mark this as Solved

    Ochimus

+ 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. Range.Find Method Runtime Error '91' ; MS Online Help Not Helpful
    By Chaunciliscious in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-13-2017, 12:22 PM
  2. [SOLVED] Find column number from specific range
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2016, 09:12 AM
  3. [SOLVED] Runtime Error 91 when using Worksheet().Range().Find().Select
    By michaljireht in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-17-2015, 06:05 AM
  4. Replies: 4
    Last Post: 01-23-2014, 10:57 AM
  5. [SOLVED] VBA Error: Runtime Error 1004: AutoFilter method of Range class failed
    By jl22stac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2013, 07:27 PM
  6. "Runtime error '-2147417848 (80010108)' Method 'find' of object 'Range' failed"
    By excelworker_1 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-14-2012, 09:53 AM
  7. runtime error 1004 data base or list range not valid
    By jay11 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2010, 03:08 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