I have created this thread to take a different approach than in my previous thread.
I was inspired by a post by @Dan_W in that thread that suggested to look at the xml code in the excel file.
This thread has the same goal to attempt to alter those problematic excel files that you have tried to erase the extra blank rows below the range of data in a sheet that seemingly can't be deleted. IE. after you attempt to delete those rows and then save the file, reopen the file, those extra 'blank' rows are still there & you remain with a seemingly way too big file.
I have seen some steps suggested on the internet to manually change the extension of an excel file to a zip entension, unzip that file, perform some alterations to an internal file in the zip file, rezip the files, rename the zip file back to the original file extension. I have NOT seen a script that performs all of those steps via a script for you.
I decided to create a script that will perform all of those manual steps I found, as well as a few others that will actually achieve the objective of correcting those problematic excel files that seem to have the lastcell row 'locked' to the maximum row # allowed in excel in a sheet.
When I say 'locked' I am referring to excel file sheets that you have tried to erase the extra blank rows below the range of data in a sheet that seemingly can't be deleted. IE. after you attempt to delete them and then save the file, reopen the file, those extra 'blank' rows are still there & you remain with a seemingly way too big file.
This script will ask you for the excel file that has the problematic sheet of 'ghost' blank rows that seeming can't be deleted. It will then ask you for a row # that should be the row # to start deleting all the way to the end of the file, which in these cases will be row # 1048576.
The script will then run and produce a new excel file that has '_shortened' added to the original excel file name. In other words, the script will not alter the original excel file that is selected by the user at the beginning of the script.
When the script is completed, you should see a newly created excel file that is noticeably smaller in file size than the original problematic file. Open up the new excel file that was created, in excel and see if those problematic 'blank' rows are gone.
This code appears to work at least twice as fast as the code in my previous thread mentioned.
Also let me know if the link to the test file in the previously mentioned thread is no longer valid.
Let me know how it works on those problematic files & anything that needs to be changed.
Right now the code is set up just for excel files with one sheet. If files with with multiple sheets are desired, additional code will be needed.
I was inspired by a post by @Dan_W in that thread that suggested to look at the xml code in the excel file.
This thread has the same goal to attempt to alter those problematic excel files that you have tried to erase the extra blank rows below the range of data in a sheet that seemingly can't be deleted. IE. after you attempt to delete those rows and then save the file, reopen the file, those extra 'blank' rows are still there & you remain with a seemingly way too big file.
I have seen some steps suggested on the internet to manually change the extension of an excel file to a zip entension, unzip that file, perform some alterations to an internal file in the zip file, rezip the files, rename the zip file back to the original file extension. I have NOT seen a script that performs all of those steps via a script for you.
I decided to create a script that will perform all of those manual steps I found, as well as a few others that will actually achieve the objective of correcting those problematic excel files that seem to have the lastcell row 'locked' to the maximum row # allowed in excel in a sheet.
When I say 'locked' I am referring to excel file sheets that you have tried to erase the extra blank rows below the range of data in a sheet that seemingly can't be deleted. IE. after you attempt to delete them and then save the file, reopen the file, those extra 'blank' rows are still there & you remain with a seemingly way too big file.
This script will ask you for the excel file that has the problematic sheet of 'ghost' blank rows that seeming can't be deleted. It will then ask you for a row # that should be the row # to start deleting all the way to the end of the file, which in these cases will be row # 1048576.
The script will then run and produce a new excel file that has '_shortened' added to the original excel file name. In other words, the script will not alter the original excel file that is selected by the user at the beginning of the script.
When the script is completed, you should see a newly created excel file that is noticeably smaller in file size than the original problematic file. Open up the new excel file that was created, in excel and see if those problematic 'blank' rows are gone.
VBA Code:
Sub ZipAndUnzipExcelFileV_DeleteRows()
'
Dim DialogBox As FileDialog
Dim StartingRowToDelete As Long
Dim SourceFullName As String
'
Set DialogBox = Application.FileDialog(msoFileDialogFilePicker) ' Create DialogBox to ask for file to use
DialogBox.AllowMultiSelect = False ' Allow only one file to be selected
DialogBox.Title = " "
DialogBox.Title = DialogBox.Title & _
"Select (.xlsx, .xlsm, .xlam) file to remove excess rows from" ' Establish DialogBox.Title
'
If DialogBox.Show <> -1 Then ' If no file selected then ...
Exit Sub ' Exit the sub
Else ' Else
SourceFullName = DialogBox.SelectedItems(1) ' Save the Path with file name & extension to SourceFullName
End If '
'
StartingRowToDelete = Application.InputBox("Select the start row # to " & _
"start deleting from", "Row #", Type:=1) ' Ask user what row to start deleting rows from
'
Dim startTime As Single
startTime = Timer ' Start the stopwatch
'
Dim XML_EndRowDeleteCode As Double, XML_StartRowDeleteCode As Double
Dim XML_File As Long
Dim FSO As Object, ZipFile_Builder_Extracter As Object
Dim AdditionToFileName As String
Dim SourceFileExtension As String, SourceFileName As String, SourceFilePath As String
Dim XML_ReplacementSheetDataString As String, XML_RowDeleteString As String
Dim XML_FileContents As String, XML_SheetFile As String
Dim TemporaryZipFullFilePathNameExtension As Variant, TemporaryZipFilePath As Variant
'
With CreateObject("Scripting.FileSystemObject") '
SourceFilePath = .GetParentFolderName(SourceFullName) & "\" ' Get the path of the file selected
SourceFileName = .GetBaseName(SourceFullName) ' Get the file name of the file selected
SourceFileExtension = .GetExtensionName(SourceFullName) ' Get the extension of the file selected
End With '
'
AdditionToFileName = "Temp" & Format(Now, " dd-mmm-yy h-mm-ss") ' Use date/time to create unique file name to add to selected file name
TemporaryZipFullFilePathNameExtension = SourceFilePath & _
AdditionToFileName & ".zip" ' Save unique file name to TemporaryZipFullFilePathNameExtension
'
On Error Resume Next ' Ignore errors and continue
FileCopy SourceFullName, TemporaryZipFullFilePathNameExtension ' Copy and rename original file to a zip file with a unique name
'
If Err.Number <> 0 Then ' If error detected then ...
MsgBox "Unable to copy " & SourceFullName & vbNewLine _
& "Verify the selected file is closed and try again" ' Alert the user
Exit Sub ' Exit the sub
End If '
On Error GoTo 0 ' Enable Excel error handling
'
TemporaryZipFilePath = SourceFilePath & AdditionToFileName & "\" ' Save folder path for TemporaryZipFilePath
MkDir TemporaryZipFilePath ' Create the temporary folder to unzip to
'
Set ZipFile_Builder_Extracter = CreateObject("Shell.Application") ' Create ZipFile_Builder_Extracter
ZipFile_Builder_Extracter.Namespace(TemporaryZipFilePath).CopyHere _
ZipFile_Builder_Extracter.Namespace(TemporaryZipFullFilePathNameExtension).items ' Extract the files into the temporary folder
'
'-----------------------------------------------------------------------------------
'
' loop through each file in the \xl\worksheets folder of the unzipped file
'
' How will the problematic sheet be determined that needs the 'last row' corrected? ... Would have to enter the Codename to check for in each file ???
'
XML_SheetFile = Dir(TemporaryZipFilePath & "\xl\worksheets\*.xml*") ' Save xml file name into XML_SheetFile
'
Do While XML_SheetFile <> "" ' Loop while an xml file is found
' Read text of the xl\worksheets file to a variable
XML_File = FreeFile '
Open TemporaryZipFilePath & "xl\worksheets\" & _
XML_SheetFile For Input As XML_File ' Open the found xml file
XML_FileContents = Input(LOF(XML_File), XML_File) ' Save the contents of the file into XML_FileContents
Close XML_File ' Close the XML_File
'
'-----------------------------------------------------------------------------------
'
' Do your code here ...
XML_StartRowDeleteCode = 0
XML_StartRowDeleteCode = InStr(1, XML_FileContents, "</row><row r=" & """" & _
StartingRowToDelete & """") ' Look through XML_FileContents for "</row><row r=" &
' ' """" & StartingRowToDelete & """" ... 3280
If XML_StartRowDeleteCode > 0 Then
XML_EndRowDeleteCode = InStr(XML_StartRowDeleteCode, _
XML_FileContents, "</sheetData>") ' Look through XML_FileContents for "</sheetData>" ... 41883802
'
XML_RowDeleteString = Mid(XML_FileContents, XML_StartRowDeleteCode, _
XML_EndRowDeleteCode - XML_StartRowDeleteCode) ' Load the entire range of XML rows to delete into XML_RowDeleteString
'
XML_FileContents = Replace(XML_FileContents, XML_RowDeleteString, "") ' Erase the string that pertains to the 'Blank' rows range
'
XML_FileContents = Replace(XML_FileContents, "</sheetData>", _
"</row></sheetData>") ' Correct the end of the last row line remaining in the file
'
XML_FileContents = Replace(XML_FileContents, " zeroHeight=" & _
"""" & "1" & """", "") ' Delete the hidden rows flag
End If
'
'-----------------------------------------------------------------------------------
'
' Output final changes to the file
XML_File = FreeFile '
Open TemporaryZipFilePath & "xl\worksheets\" & _
XML_SheetFile For Output As XML_File '
Print #XML_File, XML_FileContents ' save data back to xml file
Close XML_File ' Close the xml file
'
XML_SheetFile = Dir ' Look for additional Excel file names
Loop ' Loop back check for next XML_File in directory
'
' *********************************************
'
' Create empty Zip File
If Len(Dir(SourceFilePath & AdditionToFileName & ".zip")) > 0 Then Kill _
SourceFilePath & AdditionToFileName & ".zip" ' If zip file already exists then delete it
Open SourceFilePath & AdditionToFileName & ".zip" For Output As #1 '
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) ' Write signature for an empty Zip file ...
' ' "PK|- " 18 spaces(null) at the end
Close #1 '
'
'
' Copy files into the zip file
ZipFile_Builder_Extracter.Namespace(SourceFilePath & AdditionToFileName & _
".zip").CopyHere _
ZipFile_Builder_Extracter.Namespace(TemporaryZipFilePath).items ' Copy items to zip file
'
On Error Resume Next ' Ignore errors and continue
Do Until ZipFile_Builder_Extracter.Namespace(SourceFilePath & _
AdditionToFileName & ".zip").items.Count = _
ZipFile_Builder_Extracter.Namespace(TemporaryZipFilePath).items.Count ' Loop until all items have been copied to the zip file
Application.Wait (Now + TimeValue("0:00:01")) ' Wait 1 second to allow more compressing of the files being added
Loop ' Loop back
On Error GoTo 0 ' Enable Excel error handling
'
' Delete the temporary files & folders created earlier
Set FSO = CreateObject("scripting.filesystemobject") ' Create filesystemobject
FSO.deletefolder SourceFilePath & AdditionToFileName ' Delete all temporary files & folders that were created
'
' Rename the newly created file to the original file extention
Name SourceFilePath & AdditionToFileName & ".zip" As SourceFilePath & _
SourceFileName & "_Shortened" & "." & SourceFileExtension ' Set name of new 'shortened' file
'
Debug.Print "Completed in " & Timer - startTime & " Seconds." ' Display total run time to 'Immediate window' ... CTRL+G
MsgBox "Completed in " & Timer - startTime & " Seconds." ' Display pop up box that alerts user that script is complete
End Sub
This code appears to work at least twice as fast as the code in my previous thread mentioned.
Also let me know if the link to the test file in the previously mentioned thread is no longer valid.
Let me know how it works on those problematic files & anything that needs to be changed.
Right now the code is set up just for excel files with one sheet. If files with with multiple sheets are desired, additional code will be needed.