Hello There,
I have a sub which search a given value in a Table and returns a single row to the worksheet.
There might be cases that the given search value in a Table can have multiple rows. I would like a construction that will get the total number of rows found for the given search value in the Table. If returned search is greater than 1 then it should display the rows accordingly.
Please find attach a sample file for your reference.
Look forward to hearing from you.
Below are the sub construction which I am using. I would like to keep the same concept of first checking via the function if search value exists as it used across the entire workbook.
Regards,
Don
Sub to get Table values and display in worksheet, if search found
Function to Check if the Given Search value exists in Table
I have a sub which search a given value in a Table and returns a single row to the worksheet.
There might be cases that the given search value in a Table can have multiple rows. I would like a construction that will get the total number of rows found for the given search value in the Table. If returned search is greater than 1 then it should display the rows accordingly.
Please find attach a sample file for your reference.
Look forward to hearing from you.
Below are the sub construction which I am using. I would like to keep the same concept of first checking via the function if search value exists as it used across the entire workbook.
Regards,
Don
Sub to get Table values and display in worksheet, if search found
Code:
Sub DisplayTableValues()
Dim EntWB As Workbook, DispWS As Worksheet
Dim MstWSName_Transaction As Worksheet, MstTblName_Transaction As String, O_MstTbl_Transaction As Variant 'ListObjects
Dim TblSearchColumn As String, TblSearchColumnValue As String
Dim fnd As Range
Set EntWB = ThisWorkbook
Set DispWS = EntWB.Worksheets("Display")
Set MstWSName_Transaction = EntWB.Worksheets("Transaction")
MstTblName_Transaction = "Tbl_Transaction"
Set O_MstTbl_Transaction = MstWSName_Transaction.ListObjects("Tbl_Transaction") '(MstTblName_Transaction)
TblSearchColumn = "Customer Number"
TblSearchColumnValue = "1001"
If CheckValueExistsInTable(O_MstTbl_Transaction, TblSearchColumn, TblSearchColumnValue) Then
DispWS.[FSr] = vbNullString
DispWS.[FInvoiceNumber] = vbNullString
DispWS.[FCustomerNumber] = vbNullString
'Display totla rows found
DispWS.FTotalRows = 0
If DispWS.FTotalRows > 1 Then
'Enable All Buttons
End If
'Check Value found in Master Table
Set fnd = O_MstTbl_Transaction.ListColumns(TblSearchColumn).DataBodyRange.Find(What:=TblSearchColumnValue, LookIn:=xlValues, LookAt:=xlPart) 'xlWhole)
DispWS.[FSr] = Intersect(O_MstTbl_Transaction.ListColumns("Sr.").DataBodyRange, fnd.EntireRow).Value
DispWS.[FInvoiceNumber] = Intersect(O_MstTbl_Transaction.ListColumns("Invoice Number").DataBodyRange, fnd.EntireRow).Value
DispWS.[FCustomerNumber] = Intersect(O_MstTbl_Transaction.ListColumns("Customer Number").DataBodyRange, fnd.EntireRow).Value
Else
MsgBox TblSearchColumnValue & " not found."
'Disable All Buttons
End If
End Sub
Function to Check if the Given Search value exists in Table
Code:
Function CheckValueExistsInTable(ByVal pMasterTableName As ListObject, ByVal pMasterTableColumnName As String, ByRef pFieldRangeNameValue As String) As Boolean
Dim fnd1 As Range
'Check Value found in Master Table
Set fnd1 = pMasterTableName.ListColumns(pMasterTableColumnName).DataBodyRange.Find(What:=pFieldRangeNameValue, LookIn:=xlValues, LookAt:=xlPart) 'xlWhole)
If Not fnd1 Is Nothing Then
CheckValueExistsInTable = True
Else
CheckValueExistsInTable = False
End If
End Function