data population from other workbook based on date

jmccollom

New Member
Joined
Oct 1, 2014
Messages
31
Hi all,
A little background on what trying to do. We have some financial sheets in sub folders of the main folder in My Documents. Currently we are copying and pasting Values from the previous year in. What I am trying to do is populate that data automatically based on the month & year of the current workbook.

Any help would be greatly appreciated.

Thanks in advance
Jeremy
 
Here is my intro to VBA for you- I had this all typed up, saved on my comp, ready to post in a blog, once I get one...

The programming language used in creating Excel macros is known as VBA (Visual Basic for Applications).
To open the macro editor / VBA editor, from Excel, press Alt-F11.
The building blocks of a macro are known as functions and procedures. Functions can do all the same things + more than what procedures can do, so I always use functions, unless a procedure is explicity required.

To get to where you can type in or paste in a general function or procedure, click Insert --> Module. To start a function, type the following line:
function myFunction

Then press Enter. It should automatically put () after your function name for you, and create an "End Function" line for you.

If you want or need to use a procedure instead of a function for any reason, instead of typing "function", type "sub"- like this-
sub myProcedure

and press Enter. Now you're ready to start coding in VBA. Now if you ever find any code in an internet search and try it out, you can copy and paste that code into your function or procedure (if it already has a "function ...()" or a "sub ...()" starting line then you don't need to type your own).

To execute a procedure or function, just click anywhere inside that procedure or function and then press F5.


The basics:
VBA is what is known as "object-oriented". That means there are several types of "objects" that you can use in your code, like a Workbook, a Worksheet, a Range, Cells, etc. An object can be of a certain object type, or "class", and can have certain "properties", which consist of "methods" or functions. You can store an instance of an object in a "variable" and then refer to it and it's properties (methods and functions) by that variable.

For example, Worksheets is a property of the ActiveWorkbook object, so the following code will return an "array" (a list) of all the worksheets in the currently active workbook, into a variable called "sheets":

set sheets = ActiveWorkbook.Worksheets

Before going on, make sure you understand- a lot of people will refer to what's inside an Excel file as a worksheet or Excel sheet, but what an Excel file really holds is actually called a workbook- and that workbook can hold many worksheets within it. A new workbook starts out with 3 worksheets. (Keep in mind that we developers sometimes speak a different language- a more precise language than many casual Excel users.)

There are actually specific ways you can refer to the default 3 sheets that a new workbook comes with- the following code returns an array of all cells in sheet 1-

set allCells = Sheet1.cells

Once you have a variable of an array, you can refer to one of the items in the array like this-

set myCell = allCells(1,2)

This will return the cell in the first row and 2nd column- the 1st # refers to row #, 2nd # refers to column #.

You can also refer to a certain cell by using the "range" function of a worksheet object. (a "cell" object is actually a specific type of a "range" object.) If you don't specify the worksheet object, the code will just use the currently active worksheet, like this :

set myCell = range("A2")

Once you have a reference to certain cell, you can use the "value" property to see or set the value inside it- this example is also introducing the debug.print method, which simply displays what you tell it to, into a certain place in your code editor, known as the "immediate" window :

debug.print range("A2").value

Notice you didn't have to set a variable to that cell first- you can use the value property right after the range function. We do this a lot to make our code more compact.

It was mentioned you can set the value of a cell with the value property as well- like this-

range("A2").value = "hello world"

It would be good for you to know that "value" is the default property of a range object when you're using it in context of doing something with the value. So you can actually do this-

range("A2") = "hello world"

Hopefully you're learning that writing computer code is a delicate thing- you have to be very particular. A computer is even worse than a human at reading your mind. The developers of VBA tried to make things easy for you in some things, but don't make any assumptions about how something works- look it up, try it out, and prove it. Never make unwarranted assumptions.

Here's a good time to tell you some of the most important things you need to know- Important Thing #1. you can use the "intellisense" of an object to see it's properties/methods/functions - for example, in your code editor, type "range." and you should see a drop-down appear of all the properties of a range object.

Important Thing #2. After you have typed or selected any object / method / function, you can (and should) press F1 to get help on how to use it.

Important Thing #3: Please don't be afraid to research and use trial and error to find out if you're understanding correctly what you learn about how things work. You usually won't be able to break something beyond repair- but do make sure that you make backups of your workbooks or data before experimenting.

From here you really should be able to teach yourself just about everything else you need to know by using the intellisense and the help topics. But here are a few more hints of important things that you should search for in the help topics :

loops (There are different types of loops you can learn about, with advantages & disadvantages to each)
"if" statement (conditional statement)
range.offset
range.end
range.find, range.findnext
mid, left, val (functions)

For your added convenience (aren't I a nice guy), here are a few more important things so you don't have to dig around in the help topics so much:

Here is the syntax of 4 different ways of using the "if" statement (syntax just means the specific format and way that you have to use it):

method #1:
if (condition) then (statement)

method #2:
if (condition) then (statement) else (statement)

method #3:
if (condition) then
(statement)
end if

way #4:
if (condition) then
(statement)
else
(statement)
end if


Here is my favorite way of doing a loop:

do
(statements)
if (break loop condition) then exit do
(statements)
loop


EVENTS

Events are another very important thing to understand- Excel calls certain procedures that are named a certain way whenever certain events are called. Example- if you want certain code to run whenever someone changes something on sheet 1- put that code inside a procedure named Worksheet_Change- but your "sub" line starting the procedure has to look like this-

Private Sub Worksheet_Change(ByVal Target As Range)

(Actually it doesn't necessarily have to be private, but you can learn more about that later)
The part inside the parentheses is a declaration of "parameters" that the procedure can use. There's just one parameter here- named Target. The "ByVal" means the parameter is passed "by value"- again, you can learn more about that later- not real important right now. the "As Range" is telling you the data type of the parameter variable- Range. For this procedure, this parameter tells you which cell, or range or cells, was changed. So your code can check what cell it was and/or what value it changed to, and take action based on that.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Thank you for the information.

I did try the code you gave me but get a "Run-time error '91' -- Object variable or With block variable not set" at this line:

dest=ActiveWorkbook.Worksheets("Last years numbers")

Any ideas?
Thanks
 
Upvote 0
Change that to :
set dest = ActiveWorkbook.Worksheets("Last years numbers")

Also make sure you have a sheet named "Last years numbers", spelled exactly that way, with no leading or trailing spaces.
 
Upvote 0
Hi.
. Just a very minor point I noticed in passing. I think with XL 2013 they start with just one sheet?
. Otherwise a nice short intro.
not sure- I thought it was 3 with the 2013 version also, but I have a poor memory sometimes. I've been using version 2007.
 
Upvote 0
Hi,

Sorry took so long to get a reply in. It has been hectic the last couple of weeks.

I have tried the code and have now been getting an 'Error 13: Type Mismatch' at the 'y=Mid(fn,p-4,4)-1'. I have tried in a little to debug it on my own but not having any luck. The other thing I notice and have been trying to find a solution (I figure that is one of the best ways to help me learn as well) is that the variables, i.e. fn, p, & y, are all showing to be 'Empty'. Also the Copy and Dest ranges are showing ******** variable or With block not set>.

I have been working onit as well to help me better understand and learn this process. I appreciate all of your help.

Jeremy
 
Upvote 0
I was able to get some code to do what I wanted it to do, however, I am still need a tweak I can't quite figure out. Everything does what it is supposed to as long as the source is "static", i.e., the file name and location is in the macro. I need to be able to open the needed file based on the date in a specific cell of the current workbook.

I have been searching for solutions but have been unsuccessful. I am not sure if it is the way I am searching or if I am just missing something.

The file locations (for source and current) are all in the same location:

c:\Users\Elevator\Desktop\Cash Reconciliations\ -- each year then has a folder (i.e. 2014) and the file name is the month (i.e. 01)

I have copies of the code that using but am not sure of the best practice to share, i.e., copy in or share file.

Any help would be greatly appreciated.
Jeremy
 
Upvote 0
Either way, as long as you explain in enough detail what you want it to be doing exactly.
 
Upvote 0
I was able to find the solution to my problem. It works the way that intended. I just need to work on a few tweaks. Thank you ztodd for your help. It was instrumental in helping me find the solution.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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