Line or lines needed to ignore if sheet not available in a VBA code

jack109

Board Regular
Joined
May 10, 2020
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Hi

Is there a piece of code that could be added to this, so that if a sheet isn't available ihn te workbook it will just ignore and move onto the save the next CSV file instead of me having a debug issue.

Thanks

VBA Code:
Sub CSV_SAVE()

Sheets("t_faves").Select
ChDir "C:\SPREADS\"
ActiveWorkbook.SaveAs Filename:="C:\SPREADS\CSV\t_faves.csv", _
FileFormat:=xlCSV, CreateBackup:=False

Sheets("j_faves").Select
ChDir "C:\SPREADS\"
ActiveWorkbook.SaveAs Filename:="C:\SPREADS\csv\j_faves.csv", _
FileFormat:=xlCSV, CreateBackup:=False

Sheets("BF_FLAT").Select
ChDir "C:\SPREADS\"
ActiveWorkbook.SaveAs Filename:="C:\SPREADS\csv\BF_FLAT.csv", _
FileFormat:=xlCSV, CreateBackup:=False


ChDir "C:\SPREADS\"
    ActiveWorkbook.SaveAs Filename:="C:\SPREADS\SETUP.xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
       

Sheets("SPREADSHEET COPY").Select
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Does this help at all ?4

VBA Code:
Sub CSV_SAVE()

Dim ws As Worksheet
Dim ShtName As Variant
Dim sPath As String, sPathCsv As String
Dim arrShtNames As Variant

arrShtNames = Array("t_faves", "j_faves", "BF_FLAT")
sPath = "C:\SPREADS\"
sPathCsv = sPath & "csv\"

For Each ShtName In arrShtNames

    If Evaluate("isref('" & ShtName & "'!A1)") Then
        Set ws = Worksheets(ShtName)
        ws.SaveAs Filename:=sPathCsv & ShtName & ".csv", _
            FileFormat:=xlCSV, CreateBackup:=False
    End If

Next ShtName

ThisWorkbook.SaveAs Filename:=sPath & "SETUP.xlsm", FileFormat _
    :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
       
Sheets("SPREADSHEET COPY").Select
End Sub
 
Upvote 0
Is there a way I could amalgamate both solution's?

I've put the code into the sheet so to see if the worksheet exists and that works fine. I was wondering this peice of code can be changed?
VBA Code:
arrShtNames = Array("t_faves", "j_faves", "BF_FLAT")
sPath = "C:\SPREADS\"
sPathCsv = sPath & "csv\"

I would like to use the sheet names from the column (F) that is used for =WorksheetExists(F4) code instead. F4 to F40 would contain the list of the sheets I want to save as CSV files. That way when I want a new sheet to be used I can add it to column F instead of manualling typing it into this arrShtNames = Array("t_faves", "j_faves", "BF_FLAT")

Thanks
 
Upvote 0
Try:

VBA Code:
Sub CSV_SAVE_v02()

Dim ws As Worksheet, wsMain As Worksheet
Dim ShtName As Variant
Dim sPath As String, sPathCsv As String
Dim arrShtNames As Variant
Dim i As Long

Set wsMain = ActiveSheet                ' <--- Ideally use Worksheets("ActualSheetName")

With wsMain
    arrShtNames = .Range("F4", .Cells(Rows.Count, "F").End(xlUp)).Value
End With

sPath = "C:\SPREADS\"
sPathCsv = sPath & "csv\"

For i = 1 To UBound(arrShtNames)
    ShtName = arrShtNames(i, 1)
    If Evaluate("isref('" & ShtName & "'!A1)") Then
        Set ws = Worksheets(ShtName)
        ws.SaveAs Filename:=sPathCsv & ShtName & ".csv", _
            FileFormat:=xlCSV, CreateBackup:=False
    End If

Next i

ThisWorkbook.SaveAs Filename:=sPath & "SETUP.xlsm", FileFormat _
    :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
       
Sheets("SPREADSHEET COPY").Select
End Sub
 
Upvote 0
getting this message

t_faves.csv cannot be accessed. The file may be corrupted, located on a server thats not responding, or read-only.

if I switch to my original code the CSV's save fine?
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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