iLogic coding help NEEDED!!!!

iLogic coding help NEEDED!!!!

jolsem
Explorer Explorer
475 Views
1 Reply
Message 1 of 2

iLogic coding help NEEDED!!!!

jolsem
Explorer
Explorer

I am currently trying to create a multi-purpose code that can work for multiple projects in the future. So heres the situation, I have bars that when extruded give me a length (Extruded Length = d51). I will then take the value of the extruded length and put it into a Excel file. The problem I am having is that I'd like to somehow skip/bypass lines of code that may not exist or be relevant in that current project without deleting large chunks of code out. Some of these projects may consist of 100 or so bars.

 

 

ExcelFile = "C:\Users\Desktop\INVENTOR_EXCEL\Parameter_Sheet"


If Parameter("(Part):1", "d51") > 0 Then
GoExcel.CellValue(ExcelFile, "Sheet1", "A1")= "(Part):1"
GoExcel.CellValue(ExcelFile, "Sheet1", "B1")= Parameter("(Part):1", "d51")
End If


If Parameter("(Part):2", "d51") > 0 Then
GoExcel.CellValue(ExcelFile, "Sheet1", "A2") = "(Part):2"
GoExcel.CellValue("B2") = Parameter("(Part):2", "d51")
End If


If Parameter("(Part):3", "d51") > 0 Then
GoExcel.CellValue(ExcelFile, "Sheet1", "A3") = "(Part):3"
GoExcel.CellValue("B3") = Parameter("(Part):3", "d51")
End If

.... (ect.)

 

 

EXAMPLE 1:

Problem: I have a project assembled with a "Part 1" and "Part 3"; but does not have a "Part 2"

With the current code that I have it notices that there is no "Part 2" and wants me to delete it in order for it to work.

 

EXAMPLE 2:

Problem: I have the same project as mentioned above, but now I replaced "Part 3" with "Part 2".

I had already deleted the code for "Part 2" and now it doesnt find a "Part 3". So I will have to go in and change everything manual each time.

 

 The only solution I can come up with to solve this problem is to have it skip lines of the code if the part doesn't exsit. But I'm unsure on how to exactly do that. Which is why im reaching out to anyone that might be able to help.  

0 Likes
Accepted solutions (1)
476 Views
1 Reply
Reply (1)
Message 2 of 2

Owner2229
Advisor
Advisor
Accepted solution

Hi, how bout something like this below? You're missing the file name in the "ExcelFile" declaration.

You're always checking the "d51" parameter, or it may vary?

 

Sub Main()
   ' Open the excel file
Dim ExcelFile As String = "C:\Users\Desktop\INVENTOR_EXCEL\Parameter_Sheet\MyExcel.xlsx"
GoExcel.Open(ExcelFile, "Sheet1")
' Write in the parameters
WriteToExcel("(Part):1", 1)
WriteToExcel("(Part):2", 2)
WriteToExcel("(Part):3", 3) '... etc ...

' Save and close the excel file
GoExcel.Save
GoExcel.Close End Sub

Private Sub WriteToExcel(oPart As String, Row As Integer)
' "Try" means it'll atempt to do the action
Try
If Parameter(oPart, "d51") > 0 Then
GoExcel.CellValue("A" & Row) = oPart
GoExcel.CellValue("B" & Row) = Parameter(oPart, "d51")
End If
Catch
End Try
End Sub

I suppose the "Part" varies, if not then you can use "For" loop and make your code even shorter:

 

Sub Main()
   ' Open the excel file
   Dim ExcelFile As String = "C:\Users\Desktop\INVENTOR_EXCEL\Parameter_Sheet\MyExcel.xlsx"
   GoExcel.Open(ExcelFile, "Sheet1")

   ' Write in the parameters
   For i = 1 To 3 ' change the "3" to the actual amount of parts
WriteToExcel("(Part):" & i, i) Next ' Save and close the excel file GoExcel.Save GoExcel.Close End Sub Private Sub WriteToExcel(oPart As String, Row As Integer) ' "Try" means it'll atempt to do the action Try If Parameter(oPart, "d51") > 0 Then GoExcel.CellValue("A" & Row) = oPart GoExcel.CellValue("B" & Row) = Parameter(oPart, "d51") End If Catch End Try End Sub

I haven't tested the code, so there might be some minor issues. But I believe you can tweak them out.

Let me know if it is what you wanted.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods