Excel VBA - Open monthly file names based on Month being worked on

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I need some advise.

I want to automate some data entry in excel. We have a spreadsheet where data is manually entered into cells E15, E16, F15, F16, O15, O16, P15, P16, Y15, Y16, Z15, and Z16. Row 15 is for current month data and row 16 is for prior month data. There are two files that the user will have to open to gather this data. The filename would be something like "Filename_(Mar_Final)" for current month and "Filename_(Feb_Final)" for prior month. The month is the only things that changes in the filename.

I want to write a VBA code that will in the end be embedded into a button that the user would just click and all the information will populate. My concern would be how can I get the right filenames to open month after month?
As in if I am working on March, then I will need to open the Mar_Final file and the Feb_Final file. If I am working on April, I will need the Apr_Final file and the Mar_Final file. What is the best way to accomplish this task?

Let me know if you need any further clarification.

Thank you
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try
Code:
strMth = MonthName(month(Now()), True)
If month(NOW()) = 1 then
    strPriorMth = MonthName(12, True)
else
    strPriorMth = MonthName(month(Now())-1, True)
End If
 
Upvote 0
Hello,

Would that work even if the months name in the file are labeled as the first three letters of the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)?


Thank you
 
Upvote 0
Yes.
The MonthName function has 2 parameters,

MonthName(month Number, Abbreviated)

the first is month number in the range 1-12 (which is why the prior month needs testing in case the current month is Jan which would give a prior month of 0 if we just did current month - 1)

The second tells Excel whether to use the full month name if the parameter is set to false (e.g. January) or a 3 letter abbreviated form if the parameter is set to True (e.g. Jan)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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