VBA failing when user changes file name

lee2121

New Member
Joined
Mar 14, 2017
Messages
41
I have a file where some VBA is ran which creates an output into a new file and does some formatting etc and then the VBA return to the original file where the VBa was first ran from. The issue is the user will sometimes change the file name which creates an error in my code.

Please see my code below with the highlighted file name which will change.

The problem is with the reference to (ORIGINAL FILE) as the user will rename the file.

Code:
Sub CreateOrder()
'
' CreateOrder Macro
'
 
'
Response = Application.InputBox("Enter Password to execute", "Password Required")
 
If Not Response = "lee" Then Exit Sub
Application.ScreenUpdating = False
Sheets("Order Create").Visible = xlSheetVisible
Sheets("Order Create").Select
Columns("A:AT").Select
    Selection.Delete Shift:=xlToLeft
    Sheets("ORIGINAL SHEET").Select
    Range("BC23").Select
    Selection.AutoFilter
    Selection.AutoFilter
    ActiveSheet.Range("$BB$23:$BC$4290").AutoFilter Field:=2, Criteria1:= _
        "ORDERED"
    Range("A22:AY4290").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Sheets("Order Create").Visible = True
    Sheets("Order Create").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("C:C,E:E,F:F,G:G,I:I,K:K").Select
    Range("K1").Activate
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlToLeft
    Rows("2:2").Select
    Selection.Delete Shift:=xlUp
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("A1").Select
   
    Call PATOrder_Click
   
    Windows("ORIGINAL FILE.xlsm").Activate
    Sheets("ORIGINAL SHEET").Select
    Selection.AutoFilter
    Range("A21").Select
    Application.ScreenUpdating = True
    MsgBox "PAT Order Created Successfully"
   
     
End Sub
 
 
 
Sub PATOrder_Click()
Dim Rng As Range, Dn As Range, RngAc As Range
Dim Lst As Long, Ac As Long, Ray() As Variant, c As Long
Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
c = 1
For Each Dn In Rng
 Lst = Cells(Dn.Row, Columns.Count).End(xlToLeft).Column
  If Lst > 4 Then
      For Ac = 5 To Lst
         If Dn.Offset(, Ac).Value <> "" Then
            c = c + 1
            ReDim Preserve Ray(1 To 4, 1 To c)
            Ray(1, c) = Dn.Value
            Ray(2, c) = Dn.Offset(, 2)
            Ray(3, c) = Dn.Offset(, Ac).Value * Dn.Offset(, 2).Value
            Ray(4, c) = Cells(1, Ac + 1)
         
End If
      Next Ac
    End If
Next Dn
Ray(1, 1) = "Variety": Ray(2, 1) = "Sales PF": Ray(3, 1) = "Amount": Ray(4, 1) = "For week"
Sheets("Order Create").Visible = xlSheetVeryHidden
Workbooks.Add
ActiveWorkbook.Windows(1).Caption = "PAT_IMPORT"
With Sheets("Sheet1").Range("A1").Resize(c, 4)
    .Value = Application.Transpose(Ray)
    .Borders.Weight = 2
    .Columns.AutoFit
    Range("E1").Select
    ActiveCell.FormulaR1C1 = "Year"
    Range("E2").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""","""",IF(RC[-1]=48,2017,2018))"
    Range("E2").Select
    Selection.AutoFill Destination:=Range("E2:E15000")
    Range("E2:E15000").Select
    Columns("E:E").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("F1").Select
    
    Columns("A:E").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Windows("ORIGINAL FILE.xlsm").Activate
    Sheets("PAT_ORDER").Visible = xlSheetVisible
    Sheets("PAT_ORDER").Select
    Range("A1:AF1").Select
    Selection.Copy
  
    Windows("PAT_IMPORT").Activate
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Rows("1:1").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1").Select
   
    Windows("ORIGINAL FILE.xlsm").Activate
    Sheets("PAT_ORDER").Visible = xlSheetVeryHidden
     
End With
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi
Add these 2 lines to the top of your code
Code:
[COLOR=#0000ff]Dim Wbk As Workbook
Set Wbk = ThisWorkbook[/COLOR]
Response = Application.InputBox("Enter Password to execute", "Password Required")
And then everywhere you have
Code:
    Windows("ORIGINAL FILE.xlsm").Activate
Replace it with
Code:
    Wbk.Activate
 
Upvote 0
Also, if you're interested, this part of your code
Code:
    Columns("A:E").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
could be simplified to
Code:
    With Columns("A:E")
        .Borders(xlDiagonalDown).LineStyle = xlNone
        .Borders(xlDiagonalUp).LineStyle = xlNone
        .Borders.LineStyle = xlNone
    End With
    Columns("B").Resize(, 2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("E").Resize(, 5).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Like wise this
Code:
    Rows("1:1").Select
    Application.CutCopyMode = False
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Becomes
Code:
    Application.CutCopyMode = False
    Rows(1).Resize(11).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
 
Upvote 0
This works perfectly thank you.

Also a big thank you for the cleaner code it's always worth doing.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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