VB Look up when the table isnt in the first row

VB Look up when the table isnt in the first row

AMN3161
Advocate Advocate
751 Views
7 Replies
Message 1 of 8

VB Look up when the table isnt in the first row

AMN3161
Advocate
Advocate

Before I ask the engineer to make me a separate page in our project BOMs that has the table in the first row. Can a VB look up be done that has a table down a few rows from the top? My code works great when I delete the first few rows that has project information but if i leave them it cant find my columns 

 

I am trying to build a automated BOM code while making it work in the existing workflows and infrastructure. If i have to ask the engineer to change our template that's fine but i want to avoid that

 

0 Likes
Accepted solutions (1)
752 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

   Are you talking about working with BOM data that has been exported to Excel?  I'm trying to picture what scenario in which the "table" doesn't start on the first row.  If you are dealing with Excel, then yes, you can work around the functional part of your spreadsheet not starting on the first row.  You can specify which row (or column) to treat as having the column names (category names) and which row (or column) the working data starts.  There are also ways to capture (or specify) which row (or column) is the last one being used by the table (working data).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

AMN3161
Advocate
Advocate

The project BOM is something where as the engineers select equipment then they populate a excel with that equipment, then i use that excel to put parts into my Inventor Assembly so i know what to use. I erased any sensitive information before putting it on the post but i attached a screen shot.

 

As you can see the table I need to do a look up on is a few rows down the beginning of the work book. My code will be pulling information from that table to populate my iproperties. Right now the VB look up works great when the table is at the top of the workbook but that is not how the engineer BOMs are set up. It cant find the columns when with the workbook in the state that is in currently (attached photo)

 

Inventor doesn't populate that Engineering BOM, it will be populating my Inventor Iproperties, but that table will always be in the same location cell wise. It will never be a row up or down, it will always start there.

0 Likes
Message 4 of 8

AMN3161
Advocate
Advocate
Accepted solution

Anyone looking for a way to do this without the logic getting confused by the blank cells before the table. This will just go down a column until it find the value its looking for. Then carry over that row number that can be used to find adjacent information in different columns. Not the cleanest but it works

oXLS = "C:\Users\anikel\Desktop\1129752 BOM.xls"
oSheet = "BOM"

For r = 1 To 1001

i = String.Compare(iProperties.Value("Project", "Part Number"), GoExcel.CellValue(oXLS, oSheet, "F" & r), True)

If i = 0 Then
        
iProperties.Value("Project", "Stock Number") = GoExcel.CellValue(oXLS, "BOM", "B" & r)
r = 1500
        
Else If i = -1
        
        iProperties.Value("Project", "Stock Number") = "The Part Number is not in the Project BOM"
        
End If

Next r

 

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

Tip:

Since you said the table data in the Excel spreadsheet will always start on the same row number, you could change the number 1 in your line:

For r = 1 To 1001

to that row number, then it will start searching at that row, instead of at the top of the spreadsheet.

Not a big deal, but it might save a little bit of process time.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 8

AMN3161
Advocate
Advocate

Good point,

 

the processing time is already significantly longer than a look up but it might just have to be a necessary evil to get this to work

 

thank you for the input

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

I have a couple more variations the code for this task you can try, to see if they process any faster for you.

Both these rules are assuming your data starts on row 10, so you may need to edit that start number.

The first variation uses the built-in GoExcel.FindRow function, but properly specifies where to start looking.

Here's the code for that version:

Dim oXLS As String = "C:\Users\anikel\Desktop\1129752 BOM.xls"
Dim oSheet As String = "BOM"

'<<<< THESE TWO ROWS ARE IMPORTANT >>>>>>>>
'the row that contains the column headers
GoExcel.TitleRow = 9
'the row where the table data starts
GoExcel.FindRowStart = 10

Dim oPN As String = iProperties.Value("Project", "Part Number")

'If found (oRow = row number) ; else if (oRow = -1) it was not found
Dim oRow As Integer = GoExcel.FindRow(oXLS, oSheet, "Manufacturer Part No.", "=", oPN)

If oRow = -1 Then 'it was not found
	iProperties.Value("Project", "Stock Number") = "The Part Number is not in the Project BOM"
Else 'it was found
	iProperties.Value("Project", "Stock Number") = GoExcel.CellValue("B" & oRow)
End If

Then the second variation uses the InStr() function, along with some strategic plans to exit the loop and the rule as soon as possible.  The InStr function will either return a zero (if not found), or if it finds it, it will return a positive Integer representing the target string's position from the left within the source string.  Very simple.  Since this version accesses Excel to inspect each row, I chose to have it open Excel at the start, then not specify the file and sheet name within each check, then close it at the end, to hopefully speed up the process.

Here's the code for this version:

Dim oXLS As String = "C:\Users\anikel\Desktop\1129752 BOM.xls"
Dim oSheet As String = "BOM"
GoExcel.Open(oXLS, oSheet)
GoExcel.DisplayAlerts = False

Dim oPN As String = iProperties.Value("Project", "Part Number")
Dim oRow, i As Integer
'<<< CHANGE 10 TO YOUR FIRST ROW OF DATA >>>
For oRow = 10 To 1001
	If InStr(GoExcel.CellValue("F" & oRow), oPN, CompareMethod.Text) > 0 Then 'found it
		iProperties.Value("Project", "Stock Number") = GoExcel.CellValue("B" & oRow)
		Exit For
		Exit Sub
	ElseIf GoExcel.CellValue("F" & oRow) = Nothing Then
		'if this cell should never be empty in a 'used' row,
		'then it has reached the end of the 'used' rows and can exit
		'instead of continuing to 1001 rows
		'so:
		iProperties.Value("Project", "Stock Number") = "The Part Number is not in the Project BOM"
		Exit For
		Exit Sub
	End If
	'if the cell's value was neither a match nor Nothing then,
	'it simply goes to the next row
Next
GoExcel.Close

 See if either of these works any faster or smoother for you.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

AMN3161
Advocate
Advocate

I had no idea was even a option, ill go with that then because i prefer to stick with the find row because of the quicker processing time 

GoExcel.FindRowStart = 2

 

0 Likes