Find Sheet Names, then use correct SaveAs Macro

SuperFerret

Well-known Member
Joined
Mar 2, 2009
Messages
515
Hi All,

I am trying to amend some code prevent me needing 2 seperate macro's that do almost the same thing.

I have workbooks which I have to save 3 worksheets, SU, MER and PF and I have to save these as .txt in a specific Directory. However, there are some workbooks which don't require/contain the SU and MER sheets.

I would like to be able to have one button (in my QAT) that can evaluate whether all 3 sheets are there, or just the PF sheet and save accordindgly. There will always be other sheets in these workbooks that do not require saving (only use for calculations)

I snatched the bones of this code from another thread, but I can't get it to work:
Code:
Sub FetaCheeseElves()
Dim i As Long
 
Application.DisplayAlerts = False
For i = 1 To Worksheets.Count
 
Select Case Sheets(i).Name
     Case "PF"
        ChDir "C:\TRANSIT"
    Sheets("PF").Select
    Application.SaveAs Filename:="C:\TRANSIT\TRANSIT.txt", FileFormat:= _
        xlText, CreateBackup:=False
     Case Else
        ChDir "C:\TRANSIT"
    Sheets("PF").Select
    Application.SaveAs Filename:="C:\TRANSIT\TRANSIT.txt", FileFormat:= _
        xlText, CreateBackup:=False
    Sheets("MER").Select
    Application.SaveAs Filename:="C:\TRANSIT\SEEDS.txt", FileFormat:= _
        xlText, CreateBackup:=False
    Sheets("SU").Select
    Application.SaveAs Filename:="C:\TRANSIT\LEVELS.txt", FileFormat:= _
        xlText, CreateBackup:=False
    End Select
Next i
Application.DisplayAlerts = True
 
End Sub

If anyone can point me in the right direction or offer any advice, I'd appreciate it.
 
Found some errors in function and sub. Now all works. Tested.

Code:
Function SheetExists(SheetName As String) As Boolean
    Dim sh As Worksheet
    On Error Resume Next
    Set sh = Worksheets(SheetName)
    If Not sh Is Nothing Then SheetExists = True
End Function

Sub FetaCheeseElves()

    Dim i As Long, arr As Variant, arr2 As Variant
     
    Application.DisplayAlerts = False
    
    arr = Array("SU", "MER", "PF")
    arr2 = Array("LEVELS", "SEEDS", "TRANSIT")
    
    For i = LBound(arr) To UBound(arr)
        If SheetExists(CStr(arr(i))) Then
            Worksheets(arr(i)).SaveAs filename:="C:\" & arr2(i) & ".txt", _
                                  FileFormat:=xlTextWindows, _
                                  CreateBackup:=False
        End If
    Next

    Application.DisplayAlerts = True
     
End Sub
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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