Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi all.
I'm attempting to create a worksheet name extraction tool and have obtained the following. Code------------------------------------------------------------------------------- Function stGetFileName() As Variant Dim nwShtNm As Worksheet Dim nwShtNm1 As String stGetFileName() = ThisWorkbook.Name 'need code to ensure that a worksheet gets named correctly. 'what I have here is not it. nwShtNm1 = stGetFileName() If Worksheet.Name = "Sum" Or "SUM" Then nwShtNm.Name = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "Summary" Then nwShtNm = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "APN" Then nwShtNm = "APN-" & nwShtNm1 End If End Function ---------------------------------------------------------------------------------------------- This is not working as hoped. I'm activating a workbook, looking for two worksheets-- Sum, or Summary, or SUM, and APN. I then want to rename those two worksheets from the book's name-- generally a 7 character string starting with "DTR-XXX" as shown above. I'm wanting to extract the last 3 digits- xxx. I've tried variations of the above, and this is as close as I've gotten thus far. the function just bounces back and forth between the function's name, and the stGetFileName() = ThisWorkbook.Name line. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Steve this is what I would do. This code will scan all the worksheets in the
active workbook. If the active workbook has a worksheet named "SUM", "Sum", "Summary", or "APN" then it changes that worksheets name by adding the last three characters of the active workbooks name. Option Explicit Sub RenameWorksheets() Dim strWbkName As String Dim wks As Worksheet ' get last 3 characters of the workbook name strWbkName = Right(ActiveWorkbook.Name, 3) ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM" wks.Name = "Sum-" & strWbkName Case "APN" wks.Name = "APN-" & strWbkName End Select Next wks End Sub Hope this helps! If so please click "Yes" below or reply. -- Cheers, Ryan "SteveDB1" wrote: Hi all. I'm attempting to create a worksheet name extraction tool and have obtained the following. Code------------------------------------------------------------------------------- Function stGetFileName() As Variant Dim nwShtNm As Worksheet Dim nwShtNm1 As String stGetFileName() = ThisWorkbook.Name 'need code to ensure that a worksheet gets named correctly. 'what I have here is not it. nwShtNm1 = stGetFileName() If Worksheet.Name = "Sum" Or "SUM" Then nwShtNm.Name = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "Summary" Then nwShtNm = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "APN" Then nwShtNm = "APN-" & nwShtNm1 End If End Function ---------------------------------------------------------------------------------------------- This is not working as hoped. I'm activating a workbook, looking for two worksheets-- Sum, or Summary, or SUM, and APN. I then want to rename those two worksheets from the book's name-- generally a 7 character string starting with "DTR-XXX" as shown above. I'm wanting to extract the last 3 digits- xxx. I've tried variations of the above, and this is as close as I've gotten thus far. the function just bounces back and forth between the function's name, and the stGetFileName() = ThisWorkbook.Name line. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Ryan,
Thank you for your help. I have one last element of this that I need help for. It does indeed grab the last 3 characters of the filename, but those appear to be the file extension. what would I use to strip off the file extension? Again-- thank you! "RyanH" wrote: Steve this is what I would do. This code will scan all the worksheets in the active workbook. If the active workbook has a worksheet named "SUM", "Sum", "Summary", or "APN" then it changes that worksheets name by adding the last three characters of the active workbooks name. Option Explicit Sub RenameWorksheets() Dim strWbkName As String Dim wks As Worksheet ' get last 3 characters of the workbook name strWbkName = Right(ActiveWorkbook.Name, 3) ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM" wks.Name = "Sum-" & strWbkName Case "APN" wks.Name = "APN-" & strWbkName End Select Next wks End Sub Hope this helps! If so please click "Yes" below or reply. -- Cheers, Ryan "SteveDB1" wrote: Hi all. I'm attempting to create a worksheet name extraction tool and have obtained the following. Code------------------------------------------------------------------------------- Function stGetFileName() As Variant Dim nwShtNm As Worksheet Dim nwShtNm1 As String stGetFileName() = ThisWorkbook.Name 'need code to ensure that a worksheet gets named correctly. 'what I have here is not it. nwShtNm1 = stGetFileName() If Worksheet.Name = "Sum" Or "SUM" Then nwShtNm.Name = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "Summary" Then nwShtNm = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "APN" Then nwShtNm = "APN-" & nwShtNm1 End If End Function ---------------------------------------------------------------------------------------------- This is not working as hoped. I'm activating a workbook, looking for two worksheets-- Sum, or Summary, or SUM, and APN. I then want to rename those two worksheets from the book's name-- generally a 7 character string starting with "DTR-XXX" as shown above. I'm wanting to extract the last 3 digits- xxx. I've tried variations of the above, and this is as close as I've gotten thus far. the function just bounces back and forth between the function's name, and the stGetFileName() = ThisWorkbook.Name line. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Replace strWbkName = Right(ActiveWorkbook.Name, 3) with this:
Dim myArray As Variant Dim strWbkName As String ' break apart workbook name where there is a period myArray = Split(ActiveWorkbook.Name, ".") ' return last 3 characters or first portion of workbook name strWbkName = Right(LBound(myArray), 3) -- Cheers, Ryan "SteveDB1" wrote: Ryan, Thank you for your help. I have one last element of this that I need help for. It does indeed grab the last 3 characters of the filename, but those appear to be the file extension. what would I use to strip off the file extension? Again-- thank you! "RyanH" wrote: Steve this is what I would do. This code will scan all the worksheets in the active workbook. If the active workbook has a worksheet named "SUM", "Sum", "Summary", or "APN" then it changes that worksheets name by adding the last three characters of the active workbooks name. Option Explicit Sub RenameWorksheets() Dim strWbkName As String Dim wks As Worksheet ' get last 3 characters of the workbook name strWbkName = Right(ActiveWorkbook.Name, 3) ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM" wks.Name = "Sum-" & strWbkName Case "APN" wks.Name = "APN-" & strWbkName End Select Next wks End Sub Hope this helps! If so please click "Yes" below or reply. -- Cheers, Ryan "SteveDB1" wrote: Hi all. I'm attempting to create a worksheet name extraction tool and have obtained the following. Code------------------------------------------------------------------------------- Function stGetFileName() As Variant Dim nwShtNm As Worksheet Dim nwShtNm1 As String stGetFileName() = ThisWorkbook.Name 'need code to ensure that a worksheet gets named correctly. 'what I have here is not it. nwShtNm1 = stGetFileName() If Worksheet.Name = "Sum" Or "SUM" Then nwShtNm.Name = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "Summary" Then nwShtNm = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "APN" Then nwShtNm = "APN-" & nwShtNm1 End If End Function ---------------------------------------------------------------------------------------------- This is not working as hoped. I'm activating a workbook, looking for two worksheets-- Sum, or Summary, or SUM, and APN. I then want to rename those two worksheets from the book's name-- generally a 7 character string starting with "DTR-XXX" as shown above. I'm wanting to extract the last 3 digits- xxx. I've tried variations of the above, and this is as close as I've gotten thus far. the function just bounces back and forth between the function's name, and the stGetFileName() = ThisWorkbook.Name line. |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
great.
Thank you again. Best. SteveB "RyanH" wrote: Replace strWbkName = Right(ActiveWorkbook.Name, 3) with this: Dim myArray As Variant Dim strWbkName As String ' break apart workbook name where there is a period myArray = Split(ActiveWorkbook.Name, ".") ' return last 3 characters or first portion of workbook name strWbkName = Right(LBound(myArray), 3) -- Cheers, Ryan "SteveDB1" wrote: Ryan, Thank you for your help. I have one last element of this that I need help for. It does indeed grab the last 3 characters of the filename, but those appear to be the file extension. what would I use to strip off the file extension? Again-- thank you! "RyanH" wrote: Steve this is what I would do. This code will scan all the worksheets in the active workbook. If the active workbook has a worksheet named "SUM", "Sum", "Summary", or "APN" then it changes that worksheets name by adding the last three characters of the active workbooks name. Option Explicit Sub RenameWorksheets() Dim strWbkName As String Dim wks As Worksheet ' get last 3 characters of the workbook name strWbkName = Right(ActiveWorkbook.Name, 3) ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM" wks.Name = "Sum-" & strWbkName Case "APN" wks.Name = "APN-" & strWbkName End Select Next wks End Sub Hope this helps! If so please click "Yes" below or reply. -- Cheers, Ryan "SteveDB1" wrote: Hi all. I'm attempting to create a worksheet name extraction tool and have obtained the following. Code------------------------------------------------------------------------------- Function stGetFileName() As Variant Dim nwShtNm As Worksheet Dim nwShtNm1 As String stGetFileName() = ThisWorkbook.Name 'need code to ensure that a worksheet gets named correctly. 'what I have here is not it. nwShtNm1 = stGetFileName() If Worksheet.Name = "Sum" Or "SUM" Then nwShtNm.Name = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "Summary" Then nwShtNm = "Sum-" & nwShtNm1 ElseIf Worksheet.Name = "APN" Then nwShtNm = "APN-" & nwShtNm1 End If End Function ---------------------------------------------------------------------------------------------- This is not working as hoped. I'm activating a workbook, looking for two worksheets-- Sum, or Summary, or SUM, and APN. I then want to rename those two worksheets from the book's name-- generally a 7 character string starting with "DTR-XXX" as shown above. I'm wanting to extract the last 3 digits- xxx. I've tried variations of the above, and this is as close as I've gotten thus far. the function just bounces back and forth between the function's name, and the stGetFileName() = ThisWorkbook.Name line. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
String-in-Cell .... too long??? Word Wrap not working...PLEASE HE | Excel Discussion (Misc queries) | |||
Text string extraction | Excel Worksheet Functions | |||
DCOUNT function not working properly on string comparisons | Excel Worksheet Functions | |||
String Extraction... | Excel Worksheet Functions | |||
data extraction / cleansing a string column in a workbook | Excel Programming |