+ Reply to Thread
Results 1 to 3 of 3

How do I change a recorded macro to make it general?

  1. #1
    Registered User
    Join Date
    05-15-2012
    Location
    Salem, Oregon
    MS-Off Ver
    Excel 2007
    Posts
    7

    How do I change a recorded macro to make it general?

    I recorded a macro to pull text file data into a template. I wanted to make it apply to any file I was pulling in, so I replaced the date information in the file name referenced in the code with the wildcard * so that (I assumed) it would just pull in the first file it found in the folder. When I run it, the template file is pulled up - but I get the message:

    Run-time error '1004':
    The file could not be accessed. Try one of the following:
    • Make sure the specified folder exists.
    • Make sure the folder that contains the file is not read-only.
    • Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *
    • Make sure the file/path name doesn't contain more than 218

    When I go to the Debug, the bolded line below is what is highlighted as the problem, but I'm not savvy enough to troubleshoot this.

    Sub TSU_IVR_Reports()
    '
    ' TSU_IVR_Reports Macro
    ' Pulls in data from TSU text files and saves as a new file
    '

    '
    ChDir _
    "C:\Documents and Settings\donajose\Desktop\TSU Cycle time\TSU IVR report import"
    Workbooks.Open Filename:= _
    "C:\Documents and Settings\donajose\Desktop\TSU Cycle time\TSU IVR report import\TSU IVR report template.xlsm"
    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Documents and Settings\donajose\Desktop\TSU Cycle time\TSU IVR report import\2012*.txt" _
    , Destination:=Range("$A$5"))
    .Name = "20120506_1"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = True
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False End With
    Columns("A:Q").Select
    Range("A3").Activate
    Columns("A:Q").EntireColumn.AutoFit
    Selection.ColumnWidth = 20.29
    Columns("A:Q").EntireColumn.AutoFit
    Range("A2:Q2").Select
    ActiveWorkbook.SaveAs Filename:= _
    "C:\Documents and Settings\donajose\Desktop\TSU Cycle time\TSU IVR report import\TSU IVR 2012*.xlsm" _
    , FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    End Sub

    When I ran the macro as recorded, it reproduced the correct results (see attached). Ultimately I need to run this for a large number of files, but don't need it to loop (yet).

    Thanks for any help provided!
    Attached Files Attached Files
    Last edited by excelVBAn00b; 05-16-2012 at 02:02 PM. Reason: Attached example result file

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: Building a multipart looping macro

    Forum Rules
    Please read and adhere to these simple rules!

    1. Use concise, accurate thread titles. Your post title should describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change the title of the thread, click EDIT on the original post, then click the Go Advanced button, then change the title. If two or more days have passed, the EDIT button will not appear, and you need to ask a moderator to change the title.

    Want to get your question answered quickly?
    Ensure your question is not too vague. Don't assume anyone is familiar with your problem. While you can upload small attachments, describe your problem in the body of the post. We are fortunate to have several Excel gurus, but few mind-readers.
    On the other hand, skip irrelevant details. Be descriptive and concise. Short, direct, and to-the-point questions with apt thread titles are almost always answered promptly.
    Keep the scope reasonably narrow. Questions like, "How do I set up an accounting system in Excel?" might be a long time waiting.
    Explain what you've already tried. ("Calculation is set to automatic, but formulas still don't compute") so helpers don't waste your time or theirs.
    Post a WORKBOOK. Nobody wants to type data from a picture or paste text from your post into a spreadsheet as a prelude to helping. To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.
    If your question has not been answered within a day, consider adding another post with any additional information you believe is relevant. If you think your post is good as is, just reply to your own thread with the words "Bump no response", which will bring it to the top of the forum.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    05-15-2012
    Location
    Salem, Oregon
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How do I change a recorded macro to make it general?

    Bump no response

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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