Solved

Command button help

Posted on 2011-02-15
5
322 Views
Last Modified: 2012-06-27
Please can you help: -

The attached code does everything I need it to except omit the "_0" from when it saves the file for the very first time.

It currently saves the file to: - C:\Documents and Settings\Administrator\My Documents\Gary\project\New Folder\

It then gives it the following file name:- "Summary_" & Format(Sheets("Low Volume").Range("C5").Value, "YYYY-MM-DD")
Extn = ".xls"

For example today’s file name would be: - “Summary_2011-02-15_0”

However I wish to amend the code so that the 1st output or recording would equal “Summary_2011-02-15”

Only was I try to save the same file name twice would the code save the worksheet as:-

“Summary_2011-02-15_1”

Third time would be “Summary_2011-02-15_2” etc


Please can you help, the command button is labelled "Archive" on my sheet
Private Sub CommandButton2_Click()
Dim strPath As String, strFileName As String
Dim Extn As String, strFname As String
Dim INUm As Long, CheckMe As Long

strPath = "C:\Documents and Settings\Administrator\My Documents\Gary\project\New Folder\"
strFileName = "Summary_" & Format(Sheets("Low Volume").Range("C5").Value, "YYYY-MM-DD")
Extn = ".xls"

strFname = Dir(strPath & strFileName & "_" & INUm & Extn)
Do While Len(strFname) <> 0
INUm = INUm + 1
strFname = Dir(strPath & strFileName & "_" & INUm & Extn)
Loop

Sheets("Low Volume").Copy
ActiveWorkbook.SaveAs Filename:=strPath & strFileName & "_" & INUm & Extn
ActiveWorkbook.Close
CheckMe = MsgBox("Clear Contents?", vbYesNo)
If CheckMe = vbYes Then

Sheets("Low Volume").Unprotect Sheets("DataSheet").Range("B2").Value
ThisWorkbook.ActiveSheet.Range("B14:K414,W14:W414,N14:N414,Q14:Q414").ClearContents
Sheets("Low Volume").Protect Sheets("DataSheet").Range("B2").Value
 End If
End Sub

Open in new window

gary-test3.xls
0
Comment
Question by:Gazza83
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 34899869
Try changing line 10 to:

strFname = Dir(strPath & strFileName & Extn)

0
 

Author Comment

by:Gazza83
ID: 34902092
If I replace line 10 it then comes up with the command prompt... File already exists do you wish to replace it. If I click no then, VB goes into error
0
 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
ID: 34902339
As well as the change above, replace line 17 with
If INUm = 0 Then
    ActiveWorkbook.SaveAs Filename:=strPath & strFileName & Extn
Else
    ActiveWorkbook.SaveAs Filename:=strPath & strFileName & "_" & INUm & Extn
End If

Open in new window

0
 

Author Comment

by:Gazza83
ID: 34902597
Thanks Stephen, seems to work really well. Has solved the "_0" issue

Private Sub CommandButton2_Click()
Dim strPath As String, strFileName As String
Dim Extn As String, strFname As String
Dim INUm As Long, CheckMe As Long

strPath = "C:\Documents and Settings\Administrator\My Documents\Gary\project\New Folder\"
strFileName = "Summary_" & Format(Sheets("Low Volume").Range("C5").Value, "YYYY-MM-DD")
Extn = ".xls"

strFname = Dir(strPath & strFileName & Extn)
Do While Len(strFname) <> 0
INUm = INUm + 1
strFname = Dir(strPath & strFileName & "_" & INUm & Extn)
Loop

Sheets("Low Volume").Copy
If INUm = 0 Then
    ActiveWorkbook.SaveAs Filename:=strPath & strFileName & Extn
Else
    ActiveWorkbook.SaveAs Filename:=strPath & strFileName & "_" & INUm & Extn
End If
ActiveWorkbook.Close
CheckMe = MsgBox("Clear Contents?", vbYesNo)
If CheckMe = vbYes Then

Sheets("Low Volume").Unprotect Sheets("DataSheet").Range("B2").Value
ThisWorkbook.ActiveSheet.Range("B14:K414,W14:W414,N14:N414,Q14:Q414").ClearContents
Sheets("Low Volume").Protect Sheets("DataSheet").Range("B2").Value
 End If
End Sub
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 34902600
Good, glad it worked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now