Close workbook and save as the same name (Overwrite)

Razor_Rob

Board Regular
Joined
Aug 18, 2022
Messages
65
Office Version
  1. 365
Platform
  1. Windows
My code opens a workbook and I want to close it with the same file name, no user entry (Save As), just overwrites on the same file.

VBA Code:
Dim wbImport As Workbook
Dim wsImport As Worksheet
Dim cell As Range
Dim fileNameAndPath As Variant
Dim lDestLastRow As Long

    'Open a workbook
    fileNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.csv), *.csv", Title:="Select File To Be Opened")
    If fileNameAndPath = False Then Exit Sub
    'Workbooks.Open Filename:=fileNameAndPath
    Set wbImport = Workbooks.Open(Filename:=fileNameAndPath, ReadOnly:=True)
    Set wsImport = wbImport.Worksheets(1)
    
Application.EnableEvents = False

    'Find last row in the destination range based on data in column A
    lDestLastRow = wsImport.Cells(wsImport.Rows.Count, "A").End(xlUp).Row
    
    'Remove spaces from the mobile number
    With wsImport
        .Range("Q3:Q" & lDestLastRow).Replace " ", vbNullString, xlPart
    End With

    'Add Zero in front of the mobile number
    With wsImport
        .Range("Q3:Q" & lDestLastRow).NumberFormat = "@"      'format range as text
        For Each cell In .Range("Q3:Q" & lDestLastRow)
             cell.Value = Format(cell * 1, "0000000000")      'Convert each cell
        Next cell
    End With
    
    'Clear contents of existing data range
    wsImport.Range("AO3:AY" & lDestLastRow).ClearContents
    wsImport.Range("BB3:BF" & lDestLastRow).ClearContents
    
    'Close the Source Workbook
    'wbImport.Close SaveChanges:=True
    
    Application.DisplayAlerts = False
    
    On Error Resume Next
    'Set wbImport = Workbooks(fileNameAndPath & ".csv")
    Set wbImport = Workbooks(fileNameAndPath)
    If wbImport Is Nothing Then
        'On Error GoTo zero
        Exit Sub
    Else
        Workbooks(fileNameAndPath).Close SaveChanges:=True, Filename:=fileNameAndPath
    End If
    
    'ActiveWorkbook.SaveAs Filename:=fileNameAndPath, FileFormat:=51
    
    
    
    Application.DisplayAlerts = True
    
    
    MsgBox "Added Zeros in front of the Mobile Number and Deleted Employer Info not required", vbOKOnly
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Well I found out the issue, I couldnt believe it was so simple as having the open workbook as read only....
Thanks everyone
 
Upvote 0
Well I found out the issue, I couldnt believe it was so simple as having the open workbook as read only....
Thanks everyone
😁 I think I saw that too but since I usually use SaveAs not to alter the original, I did not pay much attention on it 🤭. Things happened sometimes.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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