Renaming file with a suffix based on availability of work sheet.

amaresh achar

Board Regular
Joined
Dec 9, 2016
Messages
108
Office Version
  1. 365
Platform
  1. Windows
Hi,

In continuation to below mentioned post, I want to modify the code (from the below post) such that, If the file has a worksheet named "Q", then it should add a suffix "_Q" to the newly generated file name.


Any help would be great... Thanks in advance.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
try this


VBA Code:
    'add the below to the Dim Statements at the start of the Sub
    'Dim ws As Worksheet, suffix As String
    
    'replace the existing renaming code with the below
    
    Set wb = Workbooks.Open(newpath & "\" & Ffiles)
    'new code to read sheet names
    suffix = ""
    For Each ws In wb.Worksheets
    If ws.Name = "Q" Then
    suffix = "_Q"
    Exit For
    Else
    suffix = ""
    End If
    Next
    
    
    If wb.Sheets.Count >= 1 Then
        newfilename = wb.Sheets("Sheet1").Range("B3").Value & "_" & wb.Sheets("Sheet1").Range("D7").Value & "_" & wb.Sheets("Sheet1").Range("G5").Value & suffix
    Else
        newfilename = ""
    End If
    wb.Close False
 
Upvote 0
Solution
W
try this


VBA Code:
    'add the below to the Dim Statements at the start of the Sub
    'Dim ws As Worksheet, suffix As String
   
    'replace the existing renaming code with the below
   
    Set wb = Workbooks.Open(newpath & "\" & Ffiles)
    'new code to read sheet names
    suffix = ""
    For Each ws In wb.Worksheets
    If ws.Name = "Q" Then
    suffix = "_Q"
    Exit For
    Else
    suffix = ""
    End If
    Next
   
   
    If wb.Sheets.Count >= 1 Then
        newfilename = wb.Sheets("Sheet1").Range("B3").Value & "_" & wb.Sheets("Sheet1").Range("D7").Value & "_" & wb.Sheets("Sheet1").Range("G5").Value & suffix
    Else
        newfilename = ""
    End If
    wb.Close False
Thanks... Working great...!!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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