Vb Code to work in "save as file" and not work in "original file"

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
Dear Sir
I need help, I am using following code to save as new file;
Code:
Sub filename_cellvalue()ActiveWorkbook.Save
Dim SourceWB As Workbook
Dim NewWB As Workbook
Dim strPath As String
Dim FileName As String


If Dir(ThisWorkbook.Path & "\Backup", vbDirectory) = vbNullString Then MkDir ThisWorkbook.Path & "\Backup"
Path = ThisWorkbook.Path & "\Backup" & "\"


With Application
    .DisplayAlerts = False
    .ScreenUpdating = False
End With


Set SourceWB = ActiveWorkbook


With SourceWB
    .Save
    FileName = .Sheets("Summary").Range("T1").Value
    If Dir(.Path & "\Backup", vbDirectory) = vbNullString Then MkDir .Path & "\Backup"
    strPath = .Path & "\Backup\"
    .SaveCopyAs (strPath & FileName & ".xlsb")
End With


Workbooks.Open (strPath & FileName & ".xlsb")


Set NewWB = ActiveWorkbook


With NewWB




For sh = 1 To Sheets.Count
Sheets(sh).Visible = -1
Next sh




Application.DisplayAlerts = False
Sheets(Array("Reports", "Consolidated Report", "Welcome")).Delete
Application.DisplayAlerts = True
   
Sheets("New Style").Shapes.Range(Array("ColorA3")).Delete
Sheets("New Style").Shapes.Range(Array("ColorA3")).Delete
    
Sheets("Garment Detail").Shapes.Range(Array("ColorA3")).Delete
Sheets("Garment Detail").Shapes.Range(Array("ColorA3")).Delete


Sheets("Picture").Shapes.Range(Array("ColorA3")).Delete
Sheets("Picture").Shapes.Range(Array("ColorA3")).Delete
    
Sheets("Operations").Shapes.Range(Array("ColorA3")).Delete
Sheets("Operations").Shapes.Range(Array("ColorA3")).Delete
 
Sheets("Layout").Shapes.Range(Array("ColorA3")).Delete
Sheets("Layout").Shapes.Range(Array("ColorA3")).Delete
     
Sheets("Report").Shapes.Range(Array("ColorA3")).Delete
Sheets("Report").Shapes.Range(Array("ColorA3")).Delete
       
Sheets("Summary").Shapes.Range(Array("ColorA3")).Delete
Sheets("Summary").Shapes.Range(Array("Button 553")).Delete
Sheets("Summary").Shapes.Range(Array("Button 554")).Delete
Sheets("Summary").Shapes.Range(Array("Button 555")).Delete
Sheets("Summary").Shapes.Range(Array("Button 556")).Delete
Sheets("Summary").Shapes.Range(Array("Button 627")).Delete


Sheets("Short").Select
ActiveWindow.SelectedSheets.Visible = False


End With


NewWB.Close SaveChanges:=True


With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
End With
End Sub

Already below code is save as Thisworkbook VB Project, I want it to work on save as file but not to work on original file;
Code:
Public Sub Workbook_Open()  Dim Rng As Range
  Set Rng = ThisWorkbook.Sheets("Summary").Range("C56")
  
    With Rng
        .Value = .Value + 1
    End With
    
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Maybe change the Workbook_Open in the original workbook to something like this:

Code:
Public Sub Workbook_Open()  
    Dim Rng As Range
    
    If Instr(ThisWorkbook.Path, "\Backup\") Then
        Set Rng = ThisWorkbook.Sheets("Summary").Range("C56")
        With Rng
            .Value = .Value + 1
        End With
    End If
    
End Sub
so that the innards only run if the workbook is in the Backup folder.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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