Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 28 of 28

Thread: VBA Autofilter Loop

  1. #21
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    How many columns are in the table you are working on, the provided code starts looking at column 11 onwards within the table. If your table is smaller for exampe you would need to edit the bit in red below:
        On Error Resume Next        
        For x = 1 To UBound(var) ' rows
            For y = 11 To UBound(var, 2) ' columns
                If var(x, y) = toFind Then
                    tblCol = y: GoTo foundit
                End If
            Next y
        Next
        On Error GoTo 0
    Other than that it's tricky without a sample file.

    The 11 above is the first column to start searching for the value in the table.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  2. #22
    If the problem is being caused by the 255+ characters, then could we only look at columns 11-19 and this would avoid the error?

    The entire table is around 50 columns, but its only the columns 11-19 we are looking in.

    OK, If I copy and paste the entire table (as text) into your test sheet, it works fine. So something somewhere in mines must be causing an issue. I will move your sheet into mines, then tidily move the data over to hat one. Hopefully that should solve it
    Last edited by hmltnangel; 07-28-2022 at 07:43 AM.

  3. #23
    Damn! There was a random #Value error I hadnt noticed

    And it works fine now I corrected that one cell.

    Thanks Georgiboy, I shouldve spotted that way sooner.

    Your code works perfect once I ensure that there are no #Value, or #N/A errors in the table

  4. #24
    Option Explicit
    
    
    Sub test()
        Dim wsRTF As Worksheet
        Dim tblRTF As ListObject
        Dim var As Variant
        Dim toFind As String
        Dim x As Long, y As Long
        Dim tblCol As Integer
        
        toFind = Range("A1").Value
        Set wsRTF = Sheets("Talent")
        Set tblRTF = wsRTF.ListObjects("Talent")
        var = tblRTF.DataBodyRange
        
        For x = 1 To UBound(var) ' rows
            For y = 11 To UBound(var, 2) ' columns
                If var(x, y) = toFind Then
                    tblCol = y: GoTo foundit
                End If
            Next y
        Next
    foundit:
        tblRTF.DataBodyRange.AutoFilter tblCol, toFind
    End Sub
    Remove any errors from the table or code will have an error.

  5. #25
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    The attached is working with long strings and formulae that result in long strings.
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #26
    It works fine now. Thank you so much for helping. I would never have gotten there on my own. The single cell with a #Value error was causing the issue.

    thanks for bearing with me

    As a little tip - if you hover over the "x" or "y" on that line it shows you the row/column where the error exists

  7. #27
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,194
    Location
    Quote Originally Posted by hmltnangel View Post
    It works fine now. Thank you so much for helping. I would never have gotten there on my own. The single cell with a #Value error was causing the issue.

    thanks for bearing with me

    As a little tip - if you hover over the "x" or "y" on that line it shows you the row/column where the error exists

    Happy to help

    As for the tip - it would have helped if I could see your data
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  8. #28
    Dont I know it - I wish I could have. Our internal systems would have had a field day if I had uploaded the sheet itself. anonymised or not. But all worked out fantastic in the end. Now im just tidying it all back to the way I had it - errors corrected


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •