Solved

Command button help

Posted on 2011-02-15
5
340 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

813 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

17 Experts available now in Live!

Get 1:1 Help Now