MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2002, 08:53 PM   #1
kristabelle
New Member
 
Join Date: Apr 2002
Location: Atlanta, GA
Posts: 4
Default

I am trying to write VB to allow me to Save As file names that are numerical in order (ex. name2630, name2631, name2632, etc.). Is there an easier way to do this than to manually change the file name each time? Any help is much appreciated.
kristabelle is offline   Reply With Quote
Old Apr 15th, 2002, 10:01 PM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

using your example, you could set up a loop to create the filename, e.g.:


Dim as integer
dim sName as String
Dim sFirstPart as String


sFirstPart = "Name"
for i = 2630 to 2631
sName = sFirstPart & i
'put your save as code here using sName as the filename.
next


HTH
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 15th, 2002, 10:16 PM   #3
kristabelle
New Member
 
Join Date: Apr 2002
Location: Atlanta, GA
Posts: 4
Default

That'll do it. Many thanks!
-KB
kristabelle is offline   Reply With Quote
Old Apr 15th, 2002, 10:28 PM   #4
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Is this all done at once or is it going to be an ongoing tally?

I'll assume it is ongoing unless you say otherwise...

This will search the named folder and increment the value of the highest filename(value) found by one and then save the active workbook to the same named path with the new incremented values/filename...

Tongue-twister!

Anyway, try it out.

Sub SaveIncrementNumFileName_xls()
Dim SaveAsThisName As String
Dim FolderToSearch As String
Dim i As Integer
Dim FoundFileNameValue(2) As Long

With Application.FileSearch
FolderToSearch = "C:TestNum" 'edit this to your path
.LookIn = FolderToSearch
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
SaveAsThisName = .FoundFiles(i)
FoundFileNameValue(1) = Val(Mid(SaveAsThisName, Len(FolderToSearch) + 2, _
Len(SaveAsThisName) - Len(FolderToSearch) + 1))
If FoundFileNameValue(1) > FoundFileNameValue(2) Then _
FoundFileNameValue(2) = FoundFileNameValue(1)
Next
End If
End With
FoundFileNameValue(2) = FoundFileNameValue(2) + 1
ActiveWorkbook.SaveAs FolderToSearch & "/" & FoundFileNameValue(2) & ".xls"
End Sub

Oh well, post already answered.
Something new for me anyway.
Tom



[ This Message was edited by: TsTom on 2002-04-15 21:32 ]
Tom Schreiner is offline   Reply With Quote
Old Apr 15th, 2002, 10:31 PM   #5
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi Kristabelle,

Here is a macro that when you run it will add one to the file name of the active workbook and do a SaveAs. To install it, use Alt-TMV and paste the code into the macro module code pane that appears. To make its use easy you can assign this macro to a button or, using the instructions commented in the code, make it the workbook's default Save mode.

Sub IncrementAndSaveAs()

'This macro saves the active workbook, which is assumed to have
'a filename composed of a name and a number, such as "Inventory060.xls".
'This macro increments the filename to Inventory061.xls, and does a
'SaveAs to this filename. To make this macro execute automatically
'on each save, use the Workbook_Save event macro with the code:

'Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' If Not SaveAsUI then
' SaveAsUI = False
' Cancel = False
' IncrementAndSaveAs
' EndIf
'End Sub

'Note: The number of numeric digits you use in the file name determines
' how many saves can be done before the number wraps to zero. For
' example, after Inventory999.xls would be Inventory000.xls, so three
' digits allows for 999 saves before wrapping to zero.

'find out how many digits in current filename

Dim CurrentName As String
Dim NewName As String
Dim nLen As Integer
Dim NewNumber As String

CurrentName = ActiveWorkbook.Name
If Right(CurrentName, 4) = ".xls" Then
'strip off .xls
CurrentName = Left(CurrentName, Len(CurrentName) - 4)
End If

For nLen = 6 To 1 Step -1
NewNumber = Right(CurrentName, nLen)
If IsNumeric(NewNumber) Then
GoTo SetNewName
End If
Next nLen
MsgBox "File name does not end with numeric string" & vbCrLf & _
"New numeric version cannot be created.", _
vbCritical, "Save Not Done"
Exit Sub

SetNewName:
NewName = Left(CurrentName, Len(CurrentName) - nLen) & _
Right(CStr(CLng(NewNumber) + 1), nLen)

ActiveWorkbook.SaveAs Filename:=NewName

End Sub

__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 11:40 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes