BEGINNER/NOVICE - Add functionality to an existing VBA

I have very, very little experience with VBA (and the majority of that is in Excel) so please direct me to another page or resources as needed...

I came into my design firm and found a nice VBA which pulls text from Excel and rewrites several tags. It goes into the list of files, rewrites tags, saves and closes out. While it's in the file writing the tags, I thought it would be nice if it would also turn on/off levels based on text it's reading from the same spreadsheet.

I tried inserting the code found here for RefLevelOnExample() within the existing sub.

http://communities.bentley.com/Products/MicroStation/w/Microstation__Wiki/working-with-levels-in-microstation-vba

So it ended up being structured as so:

Private Sub CommandButton1_click

Dim *stuff to get data from spreadsheet

Change_Tag
Next I

End Sub

Sub Change_Tag()

Dim *stuff for tags
Dim *stuff for Level_On

Code actions for tags
Code actions for Level_On

myDGN.Save
myDGN.Close

End Sub

I'm leaving out a lot of details but hopefully what I'm saying conceptually comes through. I'm trying to shoehorn the Level_On sub within the Change_Tag. Obviously it doesn't work or I wouldn't be here :(

Is there a concept or method of building subroutines that I should heed? Are there any beginner tutorials out there that helped you guys who are not trained coders?

Thanks!

  • Hi Wole,

    at first (before Jon Summers will advice the same thing ;-) please read Programming Forum Best Practices. It's important to identiify MicroStation version you use and because of you mentioned Excel also, to identify Office version and if you run VBA macro from Excel (so MicroStation is accessed through Application object) or vice versa your code is started from MicroStation and accesses Excel as external app.

    Also, please always use Syntax Highlighter (yellow pencil icon) every time you post a code, plus choose appropriate language in the highlighter setting (should be "VB").

    Unknown said:
    I'm leaving out a lot of details but hopefully what I'm saying conceptually comes through.

    In my opinion to discuss together how to switch on/off levels based on Excel data and how to incorporate such code into existing one makes the whole topic more complex and fuzzy and doesn't bring any benefit.

    Unknown said:
    I'm trying to shoehorn the Level_On sub within the Change_Tag. Obviously it doesn't work or I wouldn't be here :(

    Exactly, it's very very bad idea and "never do it" practice. Keep your code simple, in another words ensure every part of you code do only one thing, not more. Otherwise it will be hard to debug it. It's also not good idea because to change tags (elements) is different from changing level display (settings inside dgn file).

    Unknown said:
    Is there a concept or method of building subroutines that I should heed?

    It's very good question, but does not relate to MicroStation VBA at all, but it belongs to VBA coding itself.

    Unknown said:
    Are there any beginner tutorials out there that helped you guys who are not trained coders?

    There are plenty of general VBA tutorials as book and also as webs like this one.

    Specifically about MicroStation VBA I recommend Learning MicroStation VBA book from Jerry Winter, available in Bentley Store and also from Amazon. There is also training book from envisioncad available.

    This forum is also very rich source of information, another source of information can be Jon Summers' web.

    With regards,

      Jan