+ Reply to Thread
Results 1 to 17 of 17

Loop through multiple files, run macro, then save as Excel workbook in a different folder

  1. #1
    Registered User
    Join Date
    08-15-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Loop through multiple files, run macro, then save as Excel workbook in a different folder

    This is my first post to the forum. I have 115 files all of which have been downloaded from my GPS using GPS Utility software. They are all in GPSU(.asc) format and reside in a single folder. The files can quite easily be converted to Excel and I have recorded a macro (Walktime) which will convert each .asc file singly to Excel, then do several calculations and prepare a data summary within that workbook. My macro does not save the workbook. I have devised some VBA code which loops through all .asc files, runs my macro, and then saves the resultant workbook in xlsx format and with the same filename as the .asc file. My problem is that the Excel workbook is saved in the same folder that contains the .asc files leaving me with a mix of both. What I want to do is keep all .asc files together in one folder, run my code on files within that folder, then save all Excel workbooks in another folder. The VBA code I am using is as follows:

    Please Login or Register  to view this content.
    The above works fine but I do not know how to save the Excel files to a folder other than "Temp". Can someone help me please. I use Excel 2010.

    Thanks

    Peter Kallio
    Last edited by jeffreybrown; 08-17-2013 at 11:59 PM. Reason: As per Forum Rule #3, please use code tags…Thanks.

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    i will get back to you tmrow
    Thanks - Naveed
    -----------------------------
    If the suggestion helps you, then Click * to Add Reputation
    To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
    1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  3. #3
    Registered User
    Join Date
    08-15-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Naveed. From your post a couple of days ago, I get the impression that you might be able to help. I look forward to hearing further from you.

    Peter

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Hi, Peter,

    maybe like this (untested as I do neither have asc files nor do I know what WalktimeTemp is performing):
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    try this...
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-15-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Thanks Naveed and Holger; your help was much appreciated. Neither code offered worked. In both cases I received "Run-time error 1004 - The file could not be accessed". The problem was in the "SaveAs line of code after running my Walktime macro. As I indicated in my original post, my code worked ok but saved the .xlsx files in the same folder as the .asc files. I wanted to separate the two groups of files into different folders. I have resolved the issue by inserting a line of code to also save the files to another folder (Excel Files) thus saving the files twice, once in each of two folders. I then introduced another line of code to delete all .xlsx file from the Temp folder. What I have done might seem a bit clumsy, but it works. My revised code is as follows:

    Sub LoopAllFilesTemp()

    Dim sPath As String, sDir As String

    sPath = "e:\My Documents\Bushwalking\Temp\"
    dPath = "e:\My Documents\Bushwalking\Excel Files\"

    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"

    sDir = Dir$(sPath & "*.asc", vbNormal)
    Do Until Len(sDir) = 0

    Workbooks.Open (sPath & sDir)

    Call WalktimeTemp


    With ActiveWorkbook

    .SaveAs Filename:=Left(.FullName, InStrRev(.FullName, ".")) & "xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

    ActiveWorkbook.SaveAs dPath & ActiveWorkbook.Name
    Kill "e:\my Documents\Bushwalking\Temp\*.xls"
    .Close
    End With

    Thanks again

    Peter

    sDir = Dir$
    Loop

    End Sub

  7. #7
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    please post your WalktimeTemp macro code

  8. #8
    Registered User
    Join Date
    08-15-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Here is the code. It was compiled using the Record Macro feature in Excel.

    Sub WalktimeTemp()
    '
    ' WalktimeTemp Macro
    '
    '
    'This Macro opens an ".asc" file in Excel, then summarises data and calculates walking speed.
    'Open ".asc" file in Excel.

    Columns("A:A").Select
    Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
    Range("A1").Select


    'Remove extraneous fields and data from beginning and end of file.
    'Select column "K" which contains no irrelevant data at beginning or end.
    Columns("K:K").Select
    'Find blank cells in column "K".
    Selection.SpecialCells(xlCellTypeBlanks).Select
    'Delete rows containing blank column "K" cells.
    Selection.EntireRow.Delete
    'Delete first 10 rows.
    Rows("1:12").Select
    Selection.Insert Shift:=xlDown
    'Insert column headings.
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Zone"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Easting"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Northing"
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "Elevation"
    Range("F1").Select
    ActiveCell.FormulaR1C1 = "Date"
    Range("G1").Select
    ActiveCell.FormulaR1C1 = "Clock"
    Range("I1").Select
    ActiveCell.FormulaR1C1 = "Time"
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "Km"
    Range("K1").Select
    ActiveCell.FormulaR1C1 = "Km/Hr"
    Range("l1").Select
    ActiveCell.FormulaR1C1 = "Walk Time"
    Range("M1").Select
    ActiveCell.FormulaR1C1 = "Ascents (m)"
    Range("N1").Select
    ActiveCell.FormulaR1C1 = "Descents (m)"
    'Insert Summary Table headings.
    Range("c3").Select
    ActiveCell.FormulaR1C1 = "Date"

    Range("c4").Select
    ActiveCell.FormulaR1C1 = "Start Time"
    Range("c5").Select
    ActiveCell.FormulaR1C1 = "End Time"
    Range("c6").Select
    ActiveCell.FormulaR1C1 = "Elapsed Time"
    Range("c7").Select
    ActiveCell.FormulaR1C1 = "Walk Time"
    Range("c8").Select
    ActiveCell.FormulaR1C1 = "Distance"
    Range("c9").Select
    ActiveCell.FormulaR1C1 = "Km per Hr"
    Range("G4").Select
    Selection.Font.Underline = xlUnderlineStyleSingle
    ActiveCell.FormulaR1C1 = "Elevation (m)"
    Range("F5").Select
    ActiveCell.FormulaR1C1 = "Start"
    Range("F6").Select
    ActiveCell.FormulaR1C1 = "Finish"
    Range("F7").Select
    ActiveCell.FormulaR1C1 = "Max"
    Range("F8").Select
    ActiveCell.FormulaR1C1 = "Min"
    Range("F9").Select
    ActiveCell.FormulaR1C1 = "Ascents"
    Range("F10").Select
    ActiveCell.FormulaR1C1 = "Descents"
    'Set column widths.
    Columns("G:G").ColumnWidth = 12.43
    Columns("C:C").ColumnWidth = 13.43
    Columns("D:D").ColumnWidth = 11
    Columns("F:F").ColumnWidth = 11.57
    Columns("M:M").ColumnWidth = 10.86
    Columns("N:N").ColumnWidth = 12.57

    'Formatting.
    Range("D4:D7").Select
    Selection.NumberFormat = "[h]:mm:ss"
    Range("D8:d9").Select
    Selection.NumberFormat = "0.00"
    Columns("L:L").Select
    Selection.NumberFormat = "[h]:mm:ss"
    Range("G5:G10").Select
    Selection.NumberFormat = "0.0"
    'Calculate "Walk Times" in Column "L".
    Range("L2").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(RC[-1]<1,"""",IF(R[-1]C[-1]="""","""",RC[-5]-R[-1]C[-5]))"
    Range("L2").Select
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    Range("L2:L3000").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    'Calculate Ascents in Column "M".
    Range("M14").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-8]>R[-1]C[-8],RC[-8]-R[-1]C[-8],"""")"
    Range("M14").Select
    Selection.Copy
    Range("M14:M3000").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    'Calculate Descents in Column "N".
    Range("N13").Select
    ActiveCell.FormulaR1C1 = _
    "=IF(RC[-9]="""","""",IF(RC[-9]<R[-1]C[-9],R[-1]C[-9]-RC[-9],""""))"
    Range("N13").Select
    Selection.Copy
    Range("N13:N3000").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    'Insert Date in Summary Table.
    Range("D3").Select
    ActiveCell.FormulaR1C1 = "=R[10]C[2]"
    'Insert "Start Time" in Summary Table.
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "=R[9]C[3]"
    'Insert "End Time in Summary Table".
    Range("D5").Select
    ActiveCell.FormulaR1C1 = "=LOOKUP(2,1/(C[3]<>""""),C[3])"
    'Calculate and insert "Elapsed Time in Summary Table".
    Range("D6").Select
    ActiveCell.FormulaR1C1 = "=R[-1]C-R[-2]C"
    'Calculate and insert "Walk Time" in Summary Table.
    Range("D7").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[-5]C[8]:R[2993]C[8])"
    'Calculate and insert "Distance" in Summary Table.
    Range("D8").Select
    ActiveCell.FormulaR1C1 = "=LOOKUP(2,1/(C[6]<>""""),C[6])"
    'Calculate and insert walk speed as "Km/Hr" in Summary Table.
    Range("D9").Select
    ActiveCell.FormulaR1C1 = "=R[-1]C/R[-2]C/24"
    'Insert elevation at start in Summary Table.
    Range("G5").Select
    ActiveCell.FormulaR1C1 = "=R[8]C[-2]"
    'Insert elevation at end in Summary Table.
    Range("G6").Select
    ActiveCell.FormulaR1C1 = "=LOOKUP(2,1/(C[-2]<>""""),C[-2])"
    'Calculate and insert maimum elevation in Summary Table.
    Range("G7").Select
    ActiveCell.FormulaR1C1 = "=MAX(C[-2])"
    'Calculate and insert minimum elevation in Summary Table.
    Range("G8").Select
    ActiveCell.FormulaR1C1 = "=MIN(C[-2])"
    'Calculate and insert Total Ascents in Summary Table.
    Range("G9").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[4]C[6]:R[2991]C[6])"
    'Calculate and insert Total Descents in Summary Table.
    Range("G10").Select
    ActiveCell.FormulaR1C1 = "=SUM(R[3]C[7]:R[2990]C[7])"
    Rows("3:11").Select
    Selection.Copy
    ActiveSheet.Paste
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("A1").Select

    End Sub

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Hi, Peter,

    we would love to help you but you would need to add code-tags prior to doing so:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    Ciao,
    Holger

  10. #10
    Registered User
    Join Date
    08-15-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Hello Holger

    I have included Code Tags as you requested. As I said in my last post, my code works ok but might be a little clumsy and could be streamlined to make it faster. I intend to work on that in slower time in the future. What I have done might be of assistance to others. Feel free to comment on my code if you wish and have time, but I will close the thread in a week's time.

    Regards

    Peter

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    i will test today and i will get back to you..

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Hi, Peter Kallio,

    maybe you could explain the code lines
    Please Login or Register  to view this content.
    as the first line might be useless (you have saved the file to a different folder) while the secoind line doesn´t explain why there should be Excel-Files in the folder.

    Maybe you can utilize the follwoing shortening which determines in COlumn A where teh formulas should be uodated to:

    Please Login or Register  to view this content.
    To test the macro you would need to show us a file which should be worked on (asc).

    Ciao,
    Holger

  13. #13
    Forum Contributor
    Join Date
    05-29-2013
    Location
    MD
    MS-Off Ver
    Excel 365
    Posts
    148

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Hi Peter,

    Am I right in thinking that all you want the macro to do different is save the files to a different folder?

    If so, then the line

    Please Login or Register  to view this content.
    is the culprit.

    You should replace
    Please Login or Register  to view this content.
    with

    Please Login or Register  to view this content.
    .FullName has the whole file path in it, while .Name has just the filename.

    Let us know if that works (if that is indeed the problem).

    Thanks,
    Duncan

  14. #14
    Registered User
    Join Date
    08-15-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Thanks, Duncan. You have stated my problem spot-on. I have already tried what you suggested and it doesn't work. I get a syntax error. My latest posted method of saving it separately to my desired destination EXCEL folder, then deleting files already saved to the ASC folder works ok, but there must be a better way. I am not too concerned at the moment because I have something that works.

    Peter

  15. #15
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Hi, Peter,

    maybe
    Please Login or Register  to view this content.
    Ciao,
    Holger

  16. #16
    Registered User
    Join Date
    08-15-2013
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Thanks Holger; that works fine, and solves my problem. Sorry for the delay in getting back to you, but my computer has taken a dislike to the forum and I cannot access it.
    Regards

    Peter

  17. #17
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Loop through multiple files, run macro, then save as Excel workbook in a different fol

    Hi, Peter,

    thanks for the feedback. Please mind to mark this thread Solved via Thread Tools (above the first post in this thread).

    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to create a folder and then save all files in that folder
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2013, 01:49 PM
  2. Macro Needed to Loop, Save, and Name Excel Files as PDFs
    By ahkarchem in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2012, 10:46 AM
  3. Macro to loop through all files in a folder troubleshooting (excel for mac)
    By TSMIII in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-31-2011, 09:31 AM
  4. Run macro for multiple excel files in a folder
    By ciprian in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-30-2011, 05:12 PM
  5. exce; macro to open, save embedded pdf, word, excel, ppt files to a folder
    By mcledavid in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-16-2009, 05:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1