Actuarial Outpost

Actuarial Outpost (http://www.actuarialoutpost.com/actuarial_discussion_forum/index.php)
-   Software & Technology (http://www.actuarialoutpost.com/actuarial_discussion_forum/forumdisplay.php?f=17)
-   -   Best way to learn VBA. (http://www.actuarialoutpost.com/actuarial_discussion_forum/showthread.php?t=88423)

Phil 08-10-2006 11:34 AM

VBA help sucks, unless you already knew the answer and just forgot.

Example
You want to learn the For...Next syntax. You're a newbie. You go to Language reference, find "For" and their example is like:

' This example sets the Windows OLE RIMM socket to the DLL speed of the unihex DIP switch relayed by the socket connection of the LPT1 (LPT2) port (CPT[open_com] in Windows XP SP 1).

DIM windows_cgi_bin_interface_comlink as Integer
#DEFINE comlinks.count Application.OLEboundControls
FOR windows_cgi_bin_interface_comlink = comlinks.count TO LPT1.open_port.DLLlinks(,,OLE)
Debug.Print windows_cgi_bin_interface_comlink
Application.parent.ports.&opencom speed:=8008 hex2uni(OLE_bound) Mod 16 'For Windows XP SP 1 please consult user manual
NEXT windows_cgi_bin_interface_comlink

As Seinfeld says, good luck with all that.

Kazodev 08-10-2006 11:48 AM

I just go on here, post my question and wait for Captain to answer :tup:

Bama Gambler 08-10-2006 12:02 PM

Quote:

Originally Posted by Phil (Post 1684930)
VBA help sucks, unless you already knew the answer and just forgot.

Example
You want to learn the For...Next syntax. You're a newbie. You go to Language reference, find "For" and their example is like:

' This example sets the Windows OLE RIMM socket to the DLL speed of the unihex DIP switch relayed by the socket connection of the LPT1 (LPT2) port (CPT[open_com] in Windows XP SP 1).

DIM windows_cgi_bin_interface_comlink as Integer
#DEFINE comlinks.count Application.OLEboundControls
FOR windows_cgi_bin_interface_comlink = comlinks.count TO LPT1.open_port.DLLlinks(,,OLE)
Debug.Print windows_cgi_bin_interface_comlink
Application.parent.ports.&opencom speed:=8008 hex2uni(OLE_bound) Mod 16 'For Windows XP SP 1 please consult user manual
NEXT windows_cgi_bin_interface_comlink

As Seinfeld says, good luck with all that.

lol, I'm not sure what version of help you have installed, but I get the following.

For...Next Statement Example
This example uses the For...Next statement to create a string that contains 10 instances of the numbers 0 through 9, each string separated from the other by a single space. The outer loop uses a loop counter variable that is decremented each time through the loop.

Code:

Dim Words, Chars, MyString
For Words = 10 To 1 Step -1    ' Set up 10 repetitions.
    For Chars = 0 To 9    ' Set up 10 repetitions.
        MyString = MyString & Chars    ' Append number to string.
    Next Chars    ' Increment counter
    MyString = MyString & " "    ' Append a space.
Next Words


Phil 08-10-2006 12:52 PM

A useful example would be this:

DIM i as integer
FOR i = 1 TO 15 STEP 3
Debug.print i
NEXT i

will output as follows:
1
4
7
10
13

Done. Now I know how to use FOR. I'll figure out how to nest two of them or use string concatenation on my own. Or better yet, by going to help on "&", which should have an example like "straw" & "berry" ' equals "strawberry"
but in VBA help probably just refers you back to this same FOR..NEXT example.

whisper 08-10-2006 02:56 PM

Quote:

Originally Posted by Bama Gambler (Post 1684243)
Normally I would concede that. In fact, all my "how to kick course X's ass" have that disclaimer, but in the case I feel too strongly. A good VBA book (vs. the macro recorder) is by far and away a much better place to start for someone that is looking to learn VBA. Why? Simple - there is tons of code for the reader to scan in the book. And guess what, that code is going to be a HELLUVA lot better than the code from the recorder. So if you learn by reading code, then read the code in a good book (vs. reading the code produced by the macro recorder). You will learn more and faster.

I started learning VBA because I wanted to make certain activities I had to perform in a workbook easier. I could have spent time reading the book, and figuring out all the pieces individually - or I could just record the action that I wanted replicated in VBA recorder and use that.

Brad Gile 08-10-2006 05:19 PM

Quote:

Originally Posted by whisper (Post 1685763)
I started learning VBA because I wanted to make certain activities I had to perform in a workbook easier. I could have spent time reading the book, and figuring out all the pieces individually - or I could just record the action that I wanted replicated in VBA recorder and use that.

Unfortunately, many actuaries using VBA have done the same thing, with the following results:
1. Crap code - sometimes from the recorder, sometimes from the bliss of ignorance of the coder.
2. Often, no variable declarations - again, due to total ignorance on the part of the coder.
3. Sloppy or nonexistent documentation of the code, leaving a mass of spaghetti for the poor slob that inherits that code.


Brad

Griffin 1 08-11-2006 09:30 AM

Quote:

Originally Posted by Brad Gile (Post 1686271)
Unfortunately, many actuaries using VBA have done the same thing, with the following results:
1. Crap code - sometimes from the recorder, sometimes from the bliss of ignorance of the coder.
2. Often, no variable declarations - again, due to total ignorance on the part of the coder.
3. Sloppy or nonexistent documentation of the code, leaving a mass of spaghetti for the poor slob that inherits that code.

Which means their code really isn't much different than what you see from many "professional" coders.

whisper 08-11-2006 01:33 PM

Quote:

Originally Posted by Brad Gile (Post 1686271)
Unfortunately, many actuaries using VBA have done the same thing, with the following results:
1. Crap code - sometimes from the recorder, sometimes from the bliss of ignorance of the coder.
2. Often, no variable declarations - again, due to total ignorance on the part of the coder.
3. Sloppy or nonexistent documentation of the code, leaving a mass of spaghetti for the poor slob that inherits that code.


Brad

Yes, many actuaries have used the VBA recorder to generate code.
Many of these are not trying to learn VBA, just generate code that works. The question isn't is the VBA recorder a bad way to generate code (which it is), but is it useful in learning how to program (which it is).

Brad Gile 08-11-2006 03:14 PM

Quote:

Originally Posted by whisper (Post 1687766)
Yes, many actuaries have used the VBA recorder to generate code.
Many of these are not trying to learn VBA, just generate code that works. The question isn't is the VBA recorder a bad way to generate code (which it is), but is it useful in learning how to program (which it is).

But my point, which you ignore, is that a lot of people generate the code with the recorder and routinely use it as is! Often, when this is done, the code will not always work they way the user thinks it will. IOW, the code does NOT "work"! That's not using the recorder as a learning tool.

Moreover, I agree with Bama that the recorder is highly limited as an educational tool. It is most useful for those who already have good programming techniques.

But, whatever.:D

Brad

Griffin 1 08-11-2006 03:39 PM

Quote:

Originally Posted by Brad Gile (Post 1688062)
Moreover, I agree with Bama that the recorder is highly limited as an educational tool. It is most useful for those who already have good programming techniques.

"What's the best way to learn programming?" <> "What's the best way to learn VBA?"


All times are GMT -4. The time now is 05:47 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

Page generated in 0.24345 seconds with 9 queries