Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 5 of 5

Thread: File Name

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Oklahoma
    Posts
    237
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    If I have a file that over forty people will be using, how can I get my macros to run if the user decides to change the name of the file? If you want to see my code let me know, and I will post it. Any help would be greatly appreciated.

  2. #2
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Unless your code searches for that particular file (i.e. the workbook whose name may be changed) there shouldn't be a problem running the macros. I think you're going to have to show some of your code here.

    Perhaps, just the code that is meant to start the code running.

  3. #3
    Board Regular
    Join Date
    Feb 2002
    Location
    Oklahoma
    Posts
    237
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thank you for the quick response. My code is really basic, and I apologize for that. I am merely learning the great capabilities of VBA.

    Sub CreateExtract()
    Workbooks.Add
    Windows("MFGIndirectModeltest.xls").Activate
    Sheets("FORECAST").Select
    Cells.Select
    Selection.Copy
    ActiveWindow.ActivatePrevious
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:D").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Range("A1:A2000").Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete
    Range("A1").Select
    ActiveWorkbook.SaveAs _
    FileName:="D:Personal" & Worksheets("Sheet1").Range("A1").Value & "I.xls", _
    FileFormat:=xlNormal, _
    Password:="mfgie", _
    WriteResPassword:=""
    ActiveWindow.Close SaveChanges:=True
    Windows("MFGIndirectModeltest.xls").Activate
    Range("A30").Select
    Sheets("Instructions").Select
    End Sub

    I apologize in advance for the code being so long, but I am just scratching the surface of the files

  4. #4
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    That's cool, everyone has to start from somewhere, this part of the code is your problem:


    Workbooks.Add
    Windows("MFGIndirectModeltest.xls").Activate


    I would change this to something like:


    'Declare Variable
    Dim sThisWorkbook As String

    'Intialise variable
    sThisWorkbook = ThisWorkbook.Name

    Workbooks.Add
    Windows(sThisWorkbook).Activate


    Then it shouldn't matter what the workbook is called, it's always going to use the workbook that the code is situatuated in.

    HTH

  5. #5
    Board Regular
    Join Date
    Feb 2002
    Location
    Oklahoma
    Posts
    237
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Mark,

    Thanks for your help, that works fabulously. My next question in the same code above is, the Close Method: I have SaveChanges as True, how can I get the code to overwrite the file. Instead of having a dialogue Box pop up and ask me if I want to overwrite the existing file, just have it automatically overwrite the existing file. Again many thanks for your help.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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