2

I am working on a program/macro that intertwines VBA with AutoCAD Mechanical 2020. Based on a userform input, the below variable "cw" is stored to output into the sheet space based on the below line of code. It currently outputs in a single line and I am needing it to output into 2 different lines without actually deleting the value of the variable "cw".

Lines of Code that shows variable "cw":

enter image description here

How it currently outputs in sheet space:

enter image description here

How I need it to outputs in sheet space:

enter image description here

Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
GoPokes
  • 23
  • 3
  • 1
    You need to look at the vba split method. create some new variables, split up cw (which does not destroy it) and reformat the & cw to use the new variables. – freeflow Dec 11 '20 at 17:43

2 Answers2

0
Dim cwARR() as Variant
Dim cw2 as String
cw2=cw
cw2=replace(cw2," lbs","")
cwARR = Split(cw2, " + ")  
'cwARR(0) will equal 551,000 cwARR(1) 198,000 etc

You can then output the cwARR() so ...."counterweight package:" & cwArr(0) & " lbs" into your text goto the next linespace (you will have to tell it the textposition to start at) for the next cwARR() output and cw is preserved. That should get you started.

TobyPython
  • 85
  • 7
0

As mentioned in the comments, you will need to split apart your counterweight text and reformat it as needed. You will also use Format Codes within the reformatted text to get the alignment correct. Currently, the text is like this:

   cw = "198,400 + 551,200 + 1,222,333 lbs"

and you need to end up with the text like this:

   nw = "1,222,333 lbs.\P551,200 lbs.\P198,400 lbs."

Notice the embedded Format Codes. Finally, here is the code to make this happen:

   cw = "198,400 + 551,200 + 1,222,333 lbs"
   
   nw = Replace(cw, " +", "")
   nw = Replace(nw, " lbs", "")
   nw = Split(nw, " ")
   nw = nw(2) & " lbs." & "\P" & nw(1) & " lbs." & "\P" & nw(0) & " lbs."
   
   Set text3 = ThisDrawing.PaperSpace.AddMText(textpoint3, 5, "Counterweight Package:")
   text3.AttachmentPoint = acAttachmentPointBottomRight
   Set text4 = ThisDrawing.PaperSpace.AddMText(textpoint4, 5, nw)
   text4.AttachmentPoint = acAttachmentPointBottomRight

This approach will work for any number of weights.

Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
  • This is very helpful! One question: "cw" can be a combination of pre-set values based on what the user chooses. The values are not always "198,400 + 551,200 lbs". That said, would I change this line item: cw = mainuserform.CWComboBox.value?? – GoPokes Dec 14 '20 at 15:05
  • The code will work with any pre-set values. There is an assumption of 2 values so if you have more, or less, you will need to modify the code. – Brian M Stafford Dec 14 '20 at 15:19
  • Thank you for your help! This is a massive project for me (Estimated 250,000 - 300,000 lines of code now) that has reduced the time it takes me to complete some documentation from ~1 hour to about 5 minutes if I have everything I need readily available. This is in the output phase of the code and was tripping me up for a while how to properly code it. Again, thank you! – GoPokes Dec 14 '20 at 15:30
  • One last question - in the instance that there are ever more values, then those values are reflected in the final nw code line where the 3rd counterweight value would be nw(2) right? – GoPokes Dec 14 '20 at 15:39
  • Good deal. Thank you, Brian – GoPokes Dec 14 '20 at 15:56
  • Brian - I think I am missing something. I wrote the below line of code and it still outputs to 2 lines instead of splitting the 3 values into 3 lines. Is there another "\S" that needs added to split the 3rd line from the second? nw = "\S" & nw(1) & " lbs. Superstructure" & "^" & nw(0) & " lbs. Central" & "^" & nw(2) & " lbs. Rear Tray – GoPokes Dec 17 '20 at 15:17
  • I played around with this and wasn't able to figure out how to make it work with a single MText object.. Is it possible? Perhaps, but I don't know for sure. However, I was able to get it to work with 2 MText objects. I could update the answer if this would be helpful. – Brian M Stafford Dec 17 '20 at 16:04
  • Yes I think that would be helpful. If you dont mind – GoPokes Dec 17 '20 at 16:24