Need assistance in cleaning up a macro

ghrek

Active Member
Joined
Jul 29, 2005
Messages
426
Hi

I have the following macro and I need to tidy up and make a few changes if poss. What it is when it opens up X/BACKUP I need it to auto create a subfolder and then ask me to name it before moving the data from the other files into it.

Macro enclosed.

ActiveWorkbook.SaveAs fileName:="X:\backup\summary.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ChDir "X:\NEW INPUT SCREENS"
Workbooks.Open fileName:="X:\NEW INPUT SCREENS\WEEK 1.xls"
ActiveWorkbook.Save
ChDir "X:\backup"
ActiveWorkbook.SaveAs fileName:="X:\backup\WEEK 1.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ChDir "X:\MKEYNES"
Workbooks.Open fileName:="X:\MKEYNES\WEEK 2.XLS", updatelinks:=3
ChDir "X:\backup"
ActiveWorkbook.SaveAs fileName:="X:\backup\WEEK 2.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ChDir "X:\NEW INPUT SCREENS"
Workbooks.Open fileName:="X:\NEW INPUT SCREENS\WEEK 3.xls"
ChDir "X:\backup"
ActiveWorkbook.SaveAs fileName:="X:\backup\WEEK 3.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ChDir "X:\MKEYNES"
Workbooks.Open fileName:="X:\MKEYNES\WEEK 4.XLS", updatelinks:=0
ChDir "X:\backup"
ActiveWorkbook.SaveAs fileName:="X:\backup\WEEK 4.xls", FileFormat:= _
xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
ActiveWindow.Close
ActiveWindow.Close
ActiveWindow.Close
ActiveWindow.Close
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
What it is when it opens up X/BACKUP I need it to auto create a subfolder and then ask me to name it before moving the data from the other files into it.

Do you want to copy the files to the new folder?
How do you want to auto create the new folder, that is, where do you get the name from?
If the new folder has already been created, do you want the macro to ask for that name?
Finally, are you just copying the file from one folder to another?
 
Upvote 0
Do you want to copy the files to the new folder? - YES

How do you want to auto create the new folder, that is, where do you get the name from?- I want it to auto create a new sub folder and it ask me to name it

If the new folder has already been created, do you want the macro to ask for that name? No as there shouldnt be a folder already created

Finally, are you just copying the file from one folder to another? Ideally yes but I need to break links in the files once moved as I will reuse the files.
 
Last edited:
Upvote 0
I'll give you an example and tell me if it's what you want.

Try this
Code:
Sub test()
    Dim sName As Variant, bFolder As String, dest As String
    
    sName = InputBox("Subfolder name")
    If sName = "" Then Exit Sub
    
    bFolder = "X:\backup"
    bFolder = "C:\trabajo"
    
    If Dir(bFolder & "\" & sName, vbDirectory) = "" Then
        MkDir bFolder & "\" & sName
    End If
    dest = bFolder & "\" & sName & "\"
    
    FileCopy "X:\NEW INPUT SCREENS\WEEK 1.xls", dest & "WEEK 1.xls"
    FileCopy "X:\MKEYNES\WEEK 2.XLS", dest & "WEEK 2.xls"
    FileCopy "X:\NEW INPUT SCREENS\WEEK 3.xls", dest & "WEEK 3.xls"
    FileCopy "X:\MKEYNES\WEEK 4.XLS", dest & "WEEK 4.xls"
    
End Sub
 
Upvote 0
bFolder = "X:\backup"
bFolder = "C:\trabajo"

Sorry just one other question before I try. Is the first bFolder where im getting data from and the 2nd one where I want data to go to?
 
Upvote 0
Sorry
Delete this line:
Code:
[COLOR=#333333]bFolder = "C:\trabajo"[/COLOR]

This is the destination folder
bFolder = "X:\backup"

The destination folder is created a subfolder with the name that you capture in the inputbox

the origin is in each line:
Code:
    FileCopy "[COLOR=#0000ff]X:\NEW INPUT SCREENS\WEEK 1.xls[/COLOR]", dest & "WEEK 1.xls"
    FileCopy "[COLOR=#0000ff]X:\MKEYNES\WEEK 2.XLS[/COLOR]", dest & "WEEK 2.xls"
    FileCopy "[COLOR=#0000ff]X:\NEW INPUT SCREENS\WEEK 3.xls[/COLOR]", dest & "WEEK 3.xls"
    FileCopy "[COLOR=#0000ff]X:\MKEYNES\WEEK 4.XLS[/COLOR]", dest & "WEEK 4.xls"
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Hi I need to add one thing to this if I can

I have amended the macro to add another folder called "summary". It all moves it to the back up file as planned but im trying to amend the links.

The document summary is linked via PASTE SPECIAL to weeks 1-4 and once ive saved them in the file called backup they are still linked to the original weeks 1-4 files in T:\Passenger Accounts\SHARED\passacc\Excel\passacc\NEW INPUT SCREENS.

What im trying to do is to break the links and just copy and paste as the original files T:\Passenger Accounts\SHARED\passacc\Excel\passacc\NEW INPUT SCREENS are going to be used again.

Please correct me if im wrong but I think that once ive moved the files I can copy and paste values in the summary folder and that will break links?

Is there a quick way of doing that as is it copy and paste values to each link?



Sub test()

Dim sName As Variant, bFolder As String, dest As String

sName = InputBox("Subfolder name")

If sName = "" Then Exit Sub

bFolder = "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\backup"

If Dir(bFolder & "" & sName, vbDirectory) = "" Then

MkDir bFolder & "" & sName

End If

dest = bFolder & "" & sName & ""


FileCopy "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\NEW SUMMARY\NEW SUMMARY.XLS", dest & "NEW SUMMARY.xls"

FileCopy "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\NEW INPUT SCREENS\WEEK 1", dest & "WEEK 1.xls"

FileCopy "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\NEW INPUT SCREENS\WEEK 2", dest & "WEEK 2.xls"

FileCopy "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\NEW INPUT SCREENS\WEEK 3", dest & "WEEK 3.xls"

FileCopy "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\NEW INPUT SCREENS\WEEK 4", dest & "WEEK 4.xls"



End Sub
 
Last edited:
Upvote 0
Do you mean to open each book, copy all the cells, paste as values, save the file and close the book?
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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