View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
timmulla timmulla is offline
external usenet poster
 
Posts: 46
Default savings files with the use of variables

Everybody thanks for your help! LabElf, I really liked the way you wrote
your code to solve my problem, so I decided to use your method. However,
after writing my code I'm getting errors pertaining to my If, ElseIf
statements. The error says "compile error - Else without If. If you don't
mind, and have a free moment could you please look over my code. You will
also notice that I had to add another variable (program) to file path naming
convention.

My code is:

Private Sub CommandButton1_Click()

Dim strYear(0 To 5) As String
strYear(0) = "2005"
strYear(1) = "2006"
strYear(2) = "2007"
strYear(3) = "2008"
strYear(4) = "2009"
strYear(5) = "2010"

Dim strMonth(0 To 11) As String
strMonth(0) = "September"
strMonth(1) = "October"
strMonth(2) = "Novemeber"
strMonth(3) = "December"
strMonth(4) = "January"
strMonth(5) = "February"
strMonth(6) = "March"
strMonth(7) = "April"
strMonth(8) = "May"
strMonth(9) = "June"
strMonth(10) = "July"
strMonth(11) = "August"

Dim strProgram(0 To 5) As String
strProgram(0) = "54000"
strProgram(1) = "54001"
strProgram(2) = "54002"
strProgram(3) = "54010"
strProgram(4) = "54020"
strProgram(5) = "54040"

Dim IYearIndex As Integer
IYearIndex = -1

If optYear0.Value = True Then IYearIndex = 0
ElseIf optYear1.Value = True Then
IYearIndex = 1
ElseIf optYear2.Value = True Then
IYearIndex = 2
ElseIf optYear3.Value = True Then
IYearIndex = 3
ElseIf optYear4.Value = True Then
IYearIndex = 4
ElseIf optYear5.Value = True Then
IYearIndex = 5

End If

Dim IMonthIndex As Integer
IMonthIndex = -1

If chkMonth0.Value = True Then IMonthIndex = 0
ElseIf chkMonth1.Value = True Then
IMonthIndex = 1
ElseIf chkMonth2.Value = True Then
IMonthIndex = 2
ElseIf chkMonth3.Value = True Then
IMonthIndex = 3
ElseIf chkMonth4.Value = True Then
IMonthIndex = 4
ElseIf chkMonth5.Value = True Then
IMonthIndex = 5
ElseIf chkMonth6.Value = True Then
IMonthIndex = 6
ElseIf chkMonth7.Value = True Then
IMonthIndex = 7
ElseIf chkMonth8.Value = True Then
IMonthIndex = 8
ElseIf chkMonth9.Value = True Then
IMonthIndex = 9
ElseIf chkMonth10.Value = True Then
IMonthIndex = 10
ElseIf chkMonth11.Value = True Then
IMonthIndex = 11

End If

Dim IProgramIndex As Integer
IProgramIndex = -1

If chkProgram0.Value = True Then IProgramIndex = 0
ElseIf chkProgram1.Value = True Then
IProgramIndex = 1
ElseIf chkProgram2.Value = True Then
IProgramIndex = 2
ElseIf chkProgram3.Value = True Then
IProgramIndex = 3
ElseIf chkProgram4.Value = True Then
IProgramIndex = 4
ElseIf chkProgram5.Value = True Then
IProgramIndex = 5

End If


Dim strRoot As String
strRoot = "H:\QuickBooks\Finance\MIP\MONTHLY GL UPLOAD FILES\FY" &
strYear(IYearIndex) & "uploaded files"

Dim strUpload As String
strUpload = strRoot & "\" & strMonth(IMonthIndex) & " " &
strYear(IYearIndex) & strProgram(IProgramIndex) & "GL UPLOAD.csv"

ChDir strRoot
ActiveWorkbook.SaveAs Filename:=strUpload, FileFormat:=xlNormal,
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False



End Sub


I really appreciate your help!

--
Regards,

timmulla


"LabElf" wrote:

As I see it, you need to separate the variable parts of your file paths from
the repeated parts, and make a better method for selecting the differences.
I will assume for demonstration purposes that the varying parts of your file
paths are the part after "FY" (" 2004", " 2005", etc.) and the month+year
part of the destination file name ("September 2005", "October 2005", etc.).
You need to put these into arrays, for example:
Dim strYear(0 to 6) As String
strYear(0) = "2000"
strYear(1) = "2001"
... etc., and
Dim strMonth(0 To 11) As String
strMonth(0) = "January"
strMonth(2) = "February"
... etc.

Now go through your checkboxes to create indices:
Dim iYearIndex as Integer
iYearIndex = -1
If chkYear0.Value = True Then iYearIndex = 0
ElseIf chkYear1.Value = True Then iYearIndex = 1
...
ElseIf chkYear6.Value = True Then iYearIndex = 6
End If

Dim iMonthIndex as Integer
iMonthIndex = -1
If chkMonth0.Value = True Then iMonthIndex = 0
ElseIf chkMonth1.Value = True Then iMonthIndex = 1
...
ElseIf chkMonth11.Value = True Then iMonthIndex = 11
End If

(You can use the -1 default values on the indices to detect if the user
didn't check any of the boxes.)

Then construct your file path strings:
strRoot = "H:\QuickBooks\Finance\MIP\MONTHLY GL UPLOAD FILES\FY " & _
strYear(iYearIndex) & " uploaded files"
strUpload = strRoot & "\" & strMonth(iMonthIndex) & " " &
strYear(iYearIndex) & _
" GL UPLOAD.csv"

You then can have your ChDir and Save File commands occur only once, as:
ChDir strRoot
ActiveWorkbook.SaveAs Filename:=strUpload, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

If you use list boxes instead of check boxes, you can generate your indices
quicker, or get your month and year components directly from the choices in
the listboxes, but that's up to you.
--
In theory, there is no difference between theory and practice; in practice,
there is.


"timmulla" wrote:

I need a little help with a macro I've created. I currently have a form with
check boxes indicating months and fiscal years. For example a user can click
the "july" (Checkbox1) and "FY05"(OptionsButton1) and then click a button
that automatically saves the file "September 2005 GL UPLOAD.csv" to a
location that I already specified in code. I used a multiple condition "If
then" statement to accomplish my task. The following code is a example of
this:


If CheckBox1.Value = True Then
If OptionButton1.Value = True Then

Sheets("54040 MONTHLY UPLOAD DATA").Select
Sheets("54040 MONTHLY UPLOAD DATA").Copy
ChDir _
"H:\QuickBooks\Finance\MIP\MONTHLY GL UPLOAD FILES\FY 2005 uploaded
files"
ActiveWorkbook.SaveAs Filename:= _
"H:\QuickBooks\Finance\MIP\MONTHLY GL UPLOAD FILES\FY 2005 uploaded
files\September 2005 GL UPLOAD.csv" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

End If

End If

I basically copied this code numerous times, just changing the object names
and file names to make the macro work properly.The macro works fine, however,
it's not the most effecient macro. I had to write the code (above) 24 times
to accomadate just two fiscal years. My boss wants me to find a more
effecient way of accomplishing this task. He suggested looking into
declaring variables for the file names so when a user clicks a checkbox the
program will automatically know the naming of the file (and where) to save
the file. Can anyone help me with this, or at least point me in the right
direction? I have moderate excel programming skills and limited knowledge on
using variables.

Any information would be appreciated

--
Regards,

timmulla