Looking for some advice on learning VBA and macros for Excel 2010

chrisge

New Member
Joined
Sep 24, 2013
Messages
21
Hi, I’m looking for some advice on learning some VBA to make macros in Excel 2010. I make a lot of reports in Excel 2010 from csv files that I need to download from a government website. I would like to create a macro to have these csv files automatically added to an Excel workbook database once a week (after putting the csv files into the right folder in Windows Explorer). Then I’d like to create macros to have all this data manipulated the way I normally do it manually in Excel. Can someone please offer me some advice on how long roughly it would take to learn how to do this sort of thing? Would a site like Lynda.com be the right way to learn it? Or would there be enough info on YouTube? Are these macros that I want to create very difficult to do? Do they take long to write? I need to give a proposal to the boss next week!
Thanks so much for any help offered!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Do this in bite size chunks. I'd start from the simplest (and most useful) steps first (from your description, I'd work backwards).

I'd start by using the Macro Recorder built into the VB Editor in Excel.

Setup your normal sheet as you would, "import" your data as you would, once you are ready to start manipulating your data as you need to, start the recorder.


Alt-F11, Macro > Record.

Now switch back to your sheets, and start doing your data manipulations in small steps (like if you transpose a bunch of headers first, use the recorder to show you how to do that).

After awhile of doing this, you'll start to see how you do all of this work via VBA. Once you start to get a handle on this, you can start writing procedures and functions that allow you to put repeat work into a module, so that you can
easily do all of the same work in a single line.

Here is a quick example...

This chunk of code, simply grabs "Col C" from Sheet 2, Copies it.

I then switch to Sheet1, search for "Col C", and I then paste the entire column from Sheet2 to Sheet1 in the corresponding Column.
Code:
Sub Macro()
    Columns("C:C").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Cells.Find(What:="Col C", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    Columns("C:C").Select
    ActiveSheet.Paste
End Sub


Now, this seems very simple, and it is. However, if you have to perform this 10+ times, writing all this code repeatedly gets very tedious and time consuming.

You'll want to take advantage of Procedures (Called Subs in excel) and Functions (the only difference... Subs do work without returning values, functions can do work and return values).

So, I'd write a function like the following (it uses other functions that I'm not going to include, but it reads very easily):

(now, I will warn you, this is from another project I'm working on, so it's quite complicated, but if you can learn how this function works, your well on your way to understanding VBA).

Code:
Public Function CopyDataByHeader(SourceSheet As Worksheet, SourceHeader As String, DestinationSheet As Worksheet, _
DestinationHeader As String, Optional SourceStartRow As Long = 2) As Range

'
' Example: Call CopyDataByHeader(Sheet2, "Last Name", Sheet1, "LastName")
'

' Declare Variables
'*******************************************
Dim nCopyRow As Long
Dim nPasteRow As Long
Dim nCopyCol As Range
Dim nPasteCol As Range
Dim SelRange As Range
Dim DestRange As Range


' Declare the Array used to copy and paste the source to destination
Dim SourceArray() As Variant




Set nCopyCol = SearchForCellLocation(SourceHeader, SourceSheet, True)
Set nPasteCol = SearchForCellLocation(DestinationHeader, DestinationSheet, True)


' Figure out the last Rows used for Source and Destination ranges
nCopyRow = GetRowNumber(SourceSheet, SourceHeader, False)
nPasteRow = GetRowNumber(DestinationSheet, DestinationHeader, True)


' This sets SelRange with the actual Source Range from the Source Worksheet based on HeaderName
Set SelRange = SourceSheet.Cells.Range(Cells(SourceStartRow, nCopyCol.Column).Address, Cells(nCopyRow, nCopyCol.Column).Address)


' This sets DestRange with the actual Calculated Destination as a Range based on the requested HeaderName
' Code still has to be added to determine if it should start at the next blank row, or force start from where the user specifies

Set DestRange = DestinationSheet.Cells.Range(DestinationSheet.Cells(nPasteRow, nPasteCol.Column).Address, DestinationSheet.Cells(nPasteRow + SelRange.Rows.Count - 1, nPasteCol.Column).Address)


' Expand the Array
ReDim SourceArray(1 To SelRange.Rows.Count)


' Populate the Array from the Source range selection
SourceArray = SourceSheet.Range(SelRange.Address)


' This is what actually "dumps" the Source Array information into the Destination.
DestRange.Value = SourceArray


Set DestRange = Nothing
Set SelRange = Nothing
Erase SourceArray


End Function

This is a chunk of code that you would stick in a module (not a Class module however).

You would then "call" this function from your main program code like so:

Code:
Call CopyDataByHeader(Sheet2, "Col C", Sheet1, "Col C")

So you'll notice that with a single "line" of code, I can now repeat all of the above functions that was in our first example, but also takes the functionality to a more advanced level (This particular function allows me to search for both "source" and "destination" columns by the header name, potentially on different sheets (all within the same workbook mind you). I also happen to use a method which is much faster at copying whole columns of data using Arrays. They can be confusing at first, but are incredibly powerful once you understand them.

One of the best sources that I have found for simple raw information is Chip Pearson's online database of information : (Mods, feel free to kill this link if this isn't allow to cross post) : CPearson.com Topic Index

I hope this helps direct you a little, There is no better way to learn this then simply rolling up your sleeves and digging into it... and having a project to work on gives you direction.

Happy learning/coding.
 
Upvote 0
Hey Spyrule that was really generous of you. Thanks so much. I will dig more deeply into what you've written and see if I can get an idea of how long it will take me to accomplish what I'm trying to do. I really appreciate your help, that is an awesome amount of assistance. I'm in Australia and my wife is from Montreal. Ottawa was great when I visited. No snow though this year for Xmas though eh? Thanks again and have a great season.
 
Upvote 0
Hey Spyrule that was really generous of you. Thanks so much. I will dig more deeply into what you've written and see if I can get an idea of how long it will take me to accomplish what I'm trying to do. I really appreciate your help, that is an awesome amount of assistance. I'm in Australia and my wife is from Montreal. Ottawa was great when I visited. No snow though this year for Xmas though eh? Thanks again and have a great season.

Nope, no snow yet, but the christmas tree is out and ready for a white backdrop...
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top