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
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,999
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.
 

Forum statistics

Threads
1,082,246
Messages
5,363,984
Members
400,772
Latest member
solbebe

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top