PDA

View Full Version : Self-Learn Excel VBA



Meghna
07-31-2011, 12:48 AM
Hi,

I am new to VBA macros. I know a bit of Excel, but would like to learn VBA. I have a few PDFs from the internet, but it seems a little too advanced. Please suggest how can I learn VBA.

Excel Fox
08-02-2011, 12:54 AM
Hi Meghna,

Welcome to ExcelFox community.

Start recording macros, and perform actions in the excel spreadsheet.The macro recorder will record macro. A lot of the lines may be superfluous, but you'll get a general perspective of how macro works. Slowly, you'll learn to read and differentiate between lines that actually do the task, and the superfluous ones.

As an example, I recorded my action. I typed in a few lines is what I did.


Sub Macro1()

Range("B3").Select
ActiveCell.FormulaR1C1 = _
"I am recording a macro to understand what the macro recorder records."
Range("B4").Select
ActiveCell.FormulaR1C1 = _
"OK, I'll now write another text. I wonder what the macro recorder would have recorded now."
Range("B5").Select
ActiveCell.FormulaR1C1 = "Oh well, been years since I did something like this."
Range("D3").Select
ActiveCell.FormulaR1C1 = _
"Ok, just for the sake of it, I've scrolled to the right a bit and moved up to cell D3, to type this text."
Range("D4").Select

End Sub

Now, a lot of those lines are superfluous. I could rewrite it like so.


Sub Macro2()

Range("B3").Resize(3, 1).Value = _
Array("I am recording a macro to understand what the macro recorder records.", _
"OK, I'll now write another text. I wonder what the macro recorder would have recorded now.", _
"Oh well, been years since I did something like this.")
Range("D3").Value = "Ok, just for the sake of it, I've scrolled to the right a bit and moved up to cell D3, to type this text."

End Sub

So you see, just record macro, and eventually, the macro will teach you a lot of the syntax. Based on your understanding of how things work, you can then reduce the lines of code as I have did above.

Note: Since Excel 2007 Chart actions are not recorded, and I can assure you a lot of others aren't (for reasons I do not want to pretend I know), if you want to go the learning path by recording, I recommend Excel 97-2003, or Excel 2010.