Works but is klunky - help make it elegant

DChambers

Active Member
Joined
May 19, 2006
Messages
257
This code works but I would like to make it more readable and remove excessive or redundant lines. Anyone want to try?

Code:
‘Create and prepare the new workbook
   ‘Exit if workbook already exists<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
      fileExistsStatus = Dir(NewWB)<o:p></o:p>
      If fileExistsStatus <> "" Then<o:p></o:p>
         MsgBox “The Workbook already exists – delete it and restart macro”<o:p></o:p>
         Exit Sub<o:p></o:p>
      End If<o:p></o:p>
   ‘Create workbook with 2 worksheets: Shortage and Reference<o:p></o:p>
      On Error GoTo Err_RestoreDefaultWorksheet<o:p></o:p>
      ‘Save the orininal number of worksheets created with a new workbook<o:p></o:p>
      OrgNumOfWS = Application.SheetsInNewWorkbook<o:p></o:p>
      Application.SheetsInNewWorkbook = 2<o:p></o:p>
      Workbooks.Add<o:p></o:p>
      Worksheets("Sheet1").Name = “Shortage”<o:p></o:p>
      Worksheets("Sheet2").Name = “Reference”<o:p></o:p>
Err_RestoreDefaultWorksheets:
      ‘Restore the original number of worksheets created with a new workbook<o:p></o:p>
      Application.SheetsInNewWorkbook = OrgNumOfWS <o:p></o:p>
<o:p></o:p>

Thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
1)
This isn't too clunky. I prefer readability and clear (self-defining) logic to finding the shortest possible statements - your code is well commented and proceeds in organized and logical steps. That said...

2)
You could separate the DIR() test into a separate function.

2)
Is newWB a string? If so, a naming convention such as strNewWb or sNewWb is a good habit to get into (intCount, lngRow, blnFound, wsMySheet, wbMyWorkbook, dtmMyDate, etc...)

3)
It's curious that you restore the original number of sheets if there's an error, but your code will change the number of sheets permanently if there's no error. Is that what you want? If so, probably no need to error handle - just make the change. Otherwise ...

To change number of sheets only when necessary:
Code:
Sub Test()
Dim strNewWBPath As String
Dim wb As Workbook
Dim blnOneSheetInWorkbook As Boolean

'Create and prepare the new workbook
   'Exit if workbook already exists
      If fileExistsStatus(strNewWBPath) Then
         MsgBox "The Workbook already exists – delete it and restart macro"
         Exit Sub
      End If
   
   'Create workbook with 2 worksheets: Shortage and Reference
      If Application.SheetsInNewWorkbook < 2 Then blnOneSheetInWorkbook = True
      Set wb = Workbooks.Add
      wb.Worksheets(1).Name = "Shortage"
      wb.Worksheets(2).Name = "Reference"
      If blnOneSheetInWorkbook Then Application.SheetsInNewWorkbook = 1

End Sub

'----------------------------------------------------------
Function fileExistsStatus(strFullPath As String) As Boolean
    If Dir(strFullPath) > "" Then fileExistsStatus = True
End Function

To restore the number of sheets in all cases:
Code:
Dim intOriginalNumberOfSheets As Integer

   'Create workbook with 2 worksheets: Shortage and Reference
      intOriginalNumberOfSheets = Application.SheetsInNewWorkbook
      Application.SheetsInNewWorkbook = 2
      Set wb = Workbooks.Add
      wb.Worksheets(1).Name = "Shortage"
      wb.Worksheets(2).Name = "Reference"
      Application.SheetsInNewWorkbook = intOriginalNumberOfSheets
 
Upvote 0
It's curious that you restore the original number of sheets if there's an error

Oops...brushing my teeth tonight it hit me that the line to restore the original number of sheets does execute even without an error. So, no problem there actually.

AB
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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