Save sheets named from cells that are in range. [VBA]

fredhedge

New Member
Joined
Mar 18, 2019
Messages
2
Hi pros

I have been fighting with this day trying different solutions. I have 40 rows in column C on page 1Date. If the cell matches a sheet name inside this workbook it should save that sheet as external separate workbook as the value in the cell. Currently getting 1004 error, object does not support this property or method.

Code:
Function WorksheetExists(sName As String) As Boolean
    WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)")
End Function

Sub Saving()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim xRg As Range
Dim wSh As Worksheet
Dim wS As Worksheet
Dim wBk As Workbook
Dim xPath As String
Set wSh = ActiveWorkbook.Worksheets("1Date")
Set wBk = ActiveWorkbook


Quarter = ((Month(Date) + 2) \ 3) - 1
Year1 = Format(DateAdd("m", -1, Date), "yyyy")


xPath = Application.ActiveWorkbook.path


For Each xRg In wSh.Range("C2:C40")
[COLOR=#ff0000]    For Each wS In wBk[/COLOR]
    
            If WorksheetExists(xRg.Value) = True Then
            xRg = sName
            ActiveWorkbook.Worksheets(sName).Copy
            Application.ActiveWorkbook.SaveAs filename:=xPath & "\" & sName.Name & Space(1) & "q" & Quarter & Space(1) & Year1 & ".xlsx"
            Application.ActiveWorkbook.Close False
            Else
            End If
    Next wSh
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about
Code:
For Each xRg In Wsh.Range("C2:C40")
   If WorksheetExists(xRg.Value) = True Then
      sname = xRg.Value
      wbk.Worksheets(sname).Copy
      Application.ActiveWorkbook.SaveAs filename:=XPath & "\" & sname & Space(1) & "q" & Quarter & Space(1) & Year1 & ".xlsx"
      Application.ActiveWorkbook.Close False
   End If
Next
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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