Trouble getting results from Listobject Table Column when searching

malcolmlyle

New Member
Joined
May 20, 2024
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I have a listobject, which is this data:

1717109549982.png


I'm trying to match a calculated wiresize and then select the next "usable" wiresize (not all wiresizes are available, so I have a column that the user can define what sizes they want to block). Also I have option to oversize the selection intentionally to compensate for voltage drop, for example.

VBA Code:
Public Function NextWireSize(wSize As String, iOversize As Integer) As String

    Dim oSrchTbl As ListObject
    Dim i As Integer
    Dim iNrows As Integer
    Dim iUseRow As Integer
    Dim iMatchRow As Integer
    Dim rScolumn As Range
    Dim rUcolumn As Range
    Dim sSize As String
    
    ' define constants - set column numbers for table
    Const tSizeCol As Integer = 1       ' text size column
    Const tUseCol As Integer = 3        ' Boolean Column (hidden)
               
    ' initialize variables
    Set oSrchTbl = ThisWorkbook.Sheets("Options").ListObjects("WireSizesUse")
    
    With oSrchTbl
        Set rScolumn = .ListColumns(tSizeCol).DataBodyRange
        Set rUcolumn = .ListColumns(tUseCol).DataBodyRange
    
    NextWireSize = "Error"              ' default value
    iMatchRow = 0
    
    iNrows = oSrchTbl.DataBodyRange.Rows.Count
    ' find the current size
    For i = 1 To iNrows
        If sSize = rScolumn(i).Value Then
            iMatchRow = i               ' save row # of the match
            Exit For
        End If
    Next i

    If iMatchRow = 0 Then GoTo ErrorExit                     ' no match, return "Error"
    If (iMatchRow + iOversize) > iNrows Then GoTo ErrorExit  ' no possible match, return "Error"
    
    iUseRow = 0                                              ' default value for no match
    For i = (iMatchRow + iOversize) To iNrows   ' start at found row, and look for use match
        If rUcolumn(i).Value = True Then        ' this is the first match that's also TRUE
            iUseRow = i                         ' use this row, it's the next larger usable row
            Exit For
        End If
    Next i

    If iUseRow <> 0 Then NextWireSize = rScolumn(iUseRow)

    End With
    
ErrorExit:
End Function

The problem is, i'm not referencing this correctly as it's never finding a match and i'm not able to address the value of the data from tSizeCol (col 1).

I've tried using worksheet function match and find, but I can't get those to behave either. I'm lost on why this is not returning the value based on an index.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
In this line shouldn't it be wSize
VBA Code:
If sSize = rScolumn(i).Value Then
or should sSize be something derived from wSize
 
Last edited:
Upvote 0
Solution
maybe like this
VBA Code:
    ' find the current size
    sSize = "#" & wSize
    For i = 1 To iNrows
        If sSize = rScolumn(i).Value Then
            iMatchRow = i               ' save row # of the match
            Exit For
        End If
    Next i
 
Upvote 0
maybe like this
VBA Code:
    ' find the current size
    sSize = "#" & wSize
    For i = 1 To iNrows
        If sSize = rScolumn(i).Value Then
            iMatchRow = i               ' save row # of the match
            Exit For
        End If
    Next i
The wire sizes are stored as text strings with the "#" in front of them. The initial selection (based on ampacity requried) seeds the correctly formatted string from standard ampacity tables in the workbook, so I don't think this is a "#" issue.
 
Upvote 0
In this line shouldn't it be wSize
VBA Code:
If sSize = rScolumn(i).Value Then
or should sSize be something derived from wSize
I guess I'd been looking at it so long that I coudln't see the forest for the trees. Thanks for the QA/QC!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top