jonathanwang003
Board Regular
- Joined
- May 9, 2014
- Messages
- 123
Hi Guys,
I feel as if I have a simple issue but a sophisticated answer. So I have excel files I need to open that are named by month and year. The goal of this macro is that when I press a button, it automatically opens the file. There are 2 combo boxes, one for "Month" and one for "Year." That way, the user selects the date they want, press a button, and there we have it.
So here's a slight issue, I need to open the month prior to the set date in the combo boxes. So if I select February 2014, I need to open last months file, January 2014. So now, when I select January 2014, it opens December 2014! I need it to open December 2013, the year before, since January is the beginning of the year.
So here's the variables I set and defined, but I can't seem to get January to work properly. I believe I need some sort of IF/Then to set the year back one year if the month of January is chosen. So if January is not chosen, the following code works.
Mo1 is the prior month.
Mo2 is the current month.
This code doesn't work right because later on I have to save the file as the current month. Example: when I open up December 2013 file, I need to save it as January 2014, but the variables will save it as January 2013... so the same problem in a different way.
I feel as if I have a simple issue but a sophisticated answer. So I have excel files I need to open that are named by month and year. The goal of this macro is that when I press a button, it automatically opens the file. There are 2 combo boxes, one for "Month" and one for "Year." That way, the user selects the date they want, press a button, and there we have it.
So here's a slight issue, I need to open the month prior to the set date in the combo boxes. So if I select February 2014, I need to open last months file, January 2014. So now, when I select January 2014, it opens December 2014! I need it to open December 2013, the year before, since January is the beginning of the year.
So here's the variables I set and defined, but I can't seem to get January to work properly. I believe I need some sort of IF/Then to set the year back one year if the month of January is chosen. So if January is not chosen, the following code works.
Mo1 is the prior month.
Mo2 is the current month.
This code doesn't work right because later on I have to save the file as the current month. Example: when I open up December 2013 file, I need to save it as January 2014, but the variables will save it as January 2013... so the same problem in a different way.
Code:
Public Box1 As Object
Public Box2 As Object
Public Box3 As Object
Public Mo1 As String
Public Mo2 As String
Public Yr1 As String
Public Yr2 As String
Set Box1 = [Sheet1].ComboBox1
Set Box2 = [Sheet1].ComboBox2
'Converts the date entries to the date _
abbreviations for the file name.
If Box1 = "January" Then Yr1 = Yr2 - 1
If Box1 = "January" Then Mo1 = "Dec"
If Box1 = "February" Then Mo1 = "Jan"
If Box1 = "March" Then Mo1 = "Feb"
If Box1 = "April" Then Mo1 = "Mar"
If Box1 = "May" Then Mo1 = "Apr"
If Box1 = "June" Then Mo1 = "May"
If Box1 = "July" Then Mo1 = "Jun"
If Box1 = "August" Then Mo1 = "Jul"
If Box1 = "September" Then Mo1 = "Aug"
If Box1 = "October" Then Mo1 = "Sep"
If Box1 = "November" Then Mo1 = "Oct"
If Box1 = "December" Then Mo1 = "Nov"
If Box1 = "January" Then Mo2 = "Jan"
If Box1 = "February" Then Mo2 = "Feb"
If Box1 = "March" Then Mo2 = "Mar"
If Box1 = "April" Then Mo2 = "Apr"
If Box1 = "May" Then Mo2 = "May"
If Box1 = "June" Then Mo2 = "Jun"
If Box1 = "July" Then Mo2 = "Jul"
If Box1 = "August" Then Mo2 = "Aug"
If Box1 = "September" Then Mo2 = "Sep"
If Box1 = "October" Then Mo2 = "Oct"
If Box1 = "November" Then Mo2 = "Nov"
If Box1 = "December" Then Mo2 = "Dec"