[VBA] Add 1 To Cell Then Save As

AllDay

New Member
Joined
Jul 28, 2016
Messages
12
Goal:

Start with 1 file in a folder. "C:\FL\Test1.xlsx"

In Cell A1 of that file is the value 1.

I need to open that file. Add 1 to Cell A1 then save as a new file. "C:\FL\Test2.xlsx"


I already have the VBA for the ActiveWorkbook.SaveAs to read off of the Range. I am just in need of what to add to add 1 after it's saved off the previous file.


TIA
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Please post the code that you have.
 
Upvote 0
Please post the code that you have.



Code:
Sub SaveAsB()

Dim FolderPath As String
Dim FileName As String


'Remember time when macro starts
  StartTime = Timer


'Input Location Of Files Being Updated


FolderPath = "C:\FL"


If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath + "\"


FileName = Dir(FolderPath & "*.xlsx")
Do While FileName <> ""
Application.ScreenUpdating = False
Set WB = Workbooks.Open(FolderPath & FileName)
FileName = Dir


Call B


ActiveWorkbook.Close SaveChanges:=True


Loop


Application.ScreenUpdating = True


'Determine how many seconds code took to run
  MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")


'Notify user in seconds
  MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation


End Sub



Code:
Sub B()

Dim FilePath As String
Dim FileName As String
FilePath = Sheets("Instructions").Range("A1")
FileName = Sheets("Instructions").Range("A2").Value
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=FilePath & "\" & FileName & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=True




End Sub
 
Upvote 0
If you want to add one, simply add a line like:
Code:
Sheets("Instructions").Range("A1") = Sheets("Instructions").Range("A1") + 1
Just be sure to save the file after this code runs, or else those changes won't be saved!
 
Upvote 0
So I had tried using that but if I start with 1. I need to get to 1000. It doesn't keep generating new files I don't think. I just runs through whatever amount of files I have in there and changes that one.

Are you saying to save then move the file and repeat 1000 times?

I guess what I'm trying to do it start with 1 and get to 1000 by saving as
 
Upvote 0
What exactly is in cells A1 and A2 of your instruction sheet?
Is it cell A1 or A2 you are looking to update?

I would use a loop to do what you want, something like:
Code:
Sub B()

Dim FilePath As String
Dim FileName As String
Dim x as Long

FilePath = Sheets("Instructions").Range("A1")

For x = 1 to 1000
    Sheets("Instructions").Range("A2").Value = x
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:=FilePath & "\" & x & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Application.DisplayAlerts = False
    ActiveWorkbook.Close SaveChanges:=True
Next x

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top