+ Reply to Thread
Results 1 to 7 of 7

problem with syntax on simple Class declarations and Sub declarations

  1. #1
    Registered User
    Join Date
    09-25-2011
    Location
    Rhode Island, USA
    MS-Off Ver
    Excel 2010
    Posts
    6

    Unhappy problem with syntax on simple Class declarations and Sub declarations

    I have been having a hard time figuring out the correct syntax to type in an Excel VBA program. When I type these sort of commands into Excel VBA all text is Red as if there are problems with my usage.

    I find a lot of the following type of syntax on the web but can not figure out how to make the same functionality work in Excel. I have written quite a bit of other macros in Excel, but the commands like "GET", "RETURN", "NEW" provide functionality I want to use and can't seem to figure out... Is there a web site that would help with translation that anyone knows about? Is this a VB versus VBA vs .NET problem?

    Please Login or Register  to view this content.
    or also...

    Please Login or Register  to view this content.
    or also...

    Please Login or Register  to view this content.
    I know these could be posted almost as separate questions but rather than flood the board with questions I hoped someone could point me in a general direction. I feel like I am missing something fundamental with the language. I have never used "New", "Public", "Private" declarations.

    I have attached an example of a Class module that I am having a hard time getting the "Red" to stop being "red". I don't expect someone to fix everything, but any help would be appreciated.

    Thanks a bunch.
    Rick
    Attached Files Attached Files
    Last edited by ocean_engineer; 09-25-2011 at 02:22 PM. Reason: violated rules

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: problem with syntax on simple Class declarations and Sub declarations

    The syntax issues are mainly to do with using VB.Net style syntax in VBA.

    You can declare an object and create an New instance by using single line of syntax. But unlike .net you can not pass values into the initialize routine when it is created. You would need to do this on a separate line and write your own Initialize routine to pass the value to.

    There is no Override supported.

    The name of the new object is the same as the Class code module name. So if you want to use GPS you will need to rename the class module Class1 to GPS.

    You will find more information here.
    http://www.cpearson.com/Excel/Classes.aspx
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    09-25-2011
    Location
    Rhode Island, USA
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: problem with syntax on simple Class declarations and Sub declarations

    Thanks for the help. I didn't know enough to know if it was VB.NET, I just knew it was different from what I knew. I had read the reference you linked when I first started investigating how to make this code work. I will reread and make sure I understand everything better. It sounds like I have some more work to go in learning how to translate code from one language to another. I admit that some of this is a little beyond my current knowledge base, but that is what the learning process is all about. I want to make sure I understand what is happening upon implementation anyway.

    Even when I name the first class the same as module name, it is still red. Maybe because of nestled Classes?

    Is it true that I can't put multiple classes in one Class module? I read that somewhere. I assume you can still call one from another though? Since this code started as nestled Classes, I am going to have to figure that out.

    I don't supposse anyone knows a good reference for translation of VB.NET to VBA? Especially one for dummy's that has before and after examples.

  4. #4
    Registered User
    Join Date
    09-23-2011
    Location
    Seattle WA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    5

    Re: problem with syntax on simple Class declarations and Sub declarations

    Quote Originally Posted by ocean_engineer View Post

    Even when I name the first class the same as module name, it is still red. Maybe because of nestled Classes?
    Correct. Think of VBA as the old Visual Basic 6 because really that is what it is closest to being in regards to VB.NET.

    Nestled classes will not work in VBA, you need each class in its own module.

    I don't supposse anyone knows a good reference for translation of VB.NET to VBA? Especially one for dummy's that has before and after examples.
    Not off the top of my head, but here is your code translated to VBA below, note that this line cannot be used in VBA:

    Public Sub As New (ByVal Degrees As Integer, ByVal Minutes As Integer, ByVal seconds As Double, ByVal Direction As CardinalDirection)

    The above line is a constructor and is not supported in VBA.
    In VBA, you'd want to make your own Public sub and call it after CreateObject in order to 'initialize' your class.

    Example:
    =======================
    Please Login or Register  to view this content.
    =======================


    Converted VBA class from the small class and properties you posted:

    Please Login or Register  to view this content.
    Last edited by MarcusBaker1; 09-26-2011 at 03:47 PM.

  5. #5
    Registered User
    Join Date
    09-25-2011
    Location
    Rhode Island, USA
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: problem with syntax on simple Class declarations and Sub declarations

    Thanks again to everyone. I am going to do some Googling for good applicable examples, right off the bat I see something from Micosoft with comparisons between VB,VBA,C# so wish me luck... Honestly just knowing I am looking to go from VB.Net to VBA helps a lot. So many site just list code as "VB". I am going to be much better at noticing the difference now.

    Thanks again.
    Rick

  6. #6
    Registered User
    Join Date
    09-23-2011
    Location
    Seattle WA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    5

    Re: problem with syntax on simple Class declarations and Sub declarations

    Quote Originally Posted by ocean_engineer View Post
    Thanks again to everyone. I am going to do some Googling for good applicable examples, right off the bat I see something from Micosoft with comparisons between VB,VBA,C# so wish me luck... Honestly just knowing I am looking to go from VB.Net to VBA helps a lot. So many site just list code as "VB". I am going to be much better at noticing the difference now.

    Thanks again.
    Rick
    Many sites do list code as just VB without any thought about the code being VB or VB.NET and there is a big difference in most cases, once you learn a few key differences in the languages it is not hard to convert between the two.

    Glad to hear we were able to help in some way!

    Marcus

  7. #7
    Registered User
    Join Date
    09-25-2011
    Location
    Rhode Island, USA
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: problem with syntax on simple Class declarations and Sub declarations

    I don't know if it is OK for me to continue this thread with a related question on the same code.

    I have a few lines left that are causing me difficulty converting from VB .NET to VBA

    Please Login or Register  to view this content.
    and the IF statement in

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.

    and

    Please Login or Register  to view this content.
    I don't think I have to start a new thread for this, but if I am violating any rules, please let me know. I am a newbie at posting to forum's.

    Thanks,
    Rick
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1