Trying to get all sheet names in excel workbook and then give those names to new excel sheet and save it as csv

shayanhaque

New Member
Joined
Mar 13, 2019
Messages
19
Hi,

I'm trying to copy column one of each sheet and save it to new sheet which will then be saved as csv file and name of new sheet should be pulled from old workbook.

if sheet names are If ShtNames(i) = "Sheet1" Or "Error log" Or "JUMPER REPORT" Or "WIRE LABELS" Or "TERMINATION COUNT" Then it should go to next worksheet else it should run the remaining program.

Please help me in this regard.

Code:
Sub wires()
   Dim Ary As Variant
   Dim NewBk As Workbook
   Dim ws As Worksheet
   Dim test As String
   Dim ShtNames() As String
ReDim ShtNames(1 To ActiveWorkbook.Sheets.Count)
For i = 1 To Sheets.Count
ShtNames(i) = Sheets(i).Name
Next i
   For Each ws In ThisWorkbook.Worksheets
ws.Activate
   If ShtNames(i) = "Sheet1" Or "Error log" Or "JUMPER REPORT" Or "WIRE LABELS" Or "TERMINATION COUNT" Then
   ActiveSheet.Next.Select
   
   
   Else
      
   Ary = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value2
   Range("V1").Resize(UBound(Ary)).Value = Ary
   Set NewBk = Workbooks.Add
   NewBk.Sheets(1).Range("A1").Resize(UBound(Ary)).Value = Ary
   NewBk.SaveAs Filename:="C:\Path" & test & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    
   'NewBk.SaveAs Filename:="C:\Users\Documents\test_files" mySheet & " .csv", FileFormat:=xlCSV, CreateBackup:=False
   
   Set Ary = Nothing
   Set NewBk = Nothing
   End If
   
   Next ws


   
   
   
   
End Sub
 
Last edited by a moderator:

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
How about
Code:
Sub shayanhaque()
   Dim Ary As Variant
   Dim NewBk As Workbook
   Dim ws As Worksheet
   Dim Pth As String
   
   Pth = Environ("userprofile")
   For Each ws In ThisWorkbook.Worksheets
      Select Case ws.Name
         Case "Sheet1", "Error log", "JUMPER REPORT", "WIRE LABELS", "TERMINATION COUNT"
         Case Else
            Ary = ws.Range("A1", ws.Range("A" & Rows.Count).End(xlUp)).Value2
            ws.Range("V1").Resize(UBound(Ary)).Value = Ary
            Set NewBk = Workbooks.Add
            NewBk.Sheets(1).Range("A1").Resize(UBound(Ary)).Value = Ary
            NewBk.SaveAs FileName:=Pth & "\" & ws.Name & ".csv", FileFormat:=xlCSV
            
            NewBk.Close False
            Set Ary = Nothing
            Set NewBk = Nothing
      End Select
   Next ws
End Sub
 
Upvote 0
it works perfectly. Thank you. One more question:

If i want to save each file to use defined place, how can i do that?
 
Upvote 0
How do you want the user to select the folder/path?
A pop-up box? or enter the value in a cell?
 
Upvote 0
How about
Code:
Sub shayanhaque()
   Dim Ary As Variant
   Dim NewBk As Workbook
   Dim ws As Worksheet
   Dim Pth As String
   
   With Application.fileDialog(4)
      If .Show = -1 Then Pth = .SelectedItems(1)
   End With
   For Each ws In ThisWorkbook.Worksheets
      Select Case ws.Name
         Case "Sheet1", "Error log", "JUMPER REPORT", "WIRE LABELS", "TERMINATION COUNT"
         Case Else
            Ary = ws.Range("A1", ws.Range("A" & Rows.Count).End(xlUp)).Value2
            ws.Range("V1").Resize(UBound(Ary)).Value = Ary
            Set NewBk = Workbooks.Add
            NewBk.Sheets(1).Range("A1").Resize(UBound(Ary)).Value = Ary
            NewBk.SaveAs FileName:=Pth & "\" & ws.Name & ".csv", FileFormat:=xlCSV
            
            NewBk.Close False
            Set Ary = Nothing
            Set NewBk = Nothing
      End Select
   Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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