This code return Error 91 for line set value of the NotCopySheet variable. When I only keep the code to the MsgBox line, VBA run fine. I have a named range "NotCopy" in the sheet I run. I don't have much VBA experience. This is a code I found to do what I need. I will use the NotCopySheet variable to stop copying/delete a few first sheets.
Sub CopyWorkbookValue()
Dim Output As Workbook, Source As Workbook
Dim sh As Worksheet
Dim FileName As String
Dim OriginalName As String
Dim firstCell
Dim NotCopySheet As Integer
OriginalName = ActiveWorkbook.Name
NotCopySheet = ActiveSheet.Range("NotCopy").Cells(1, 1).Value
MsgBox "The number of sheet ignore is " & NotCopySheet
Application.ScreenUpdating = False
Set Source = ActiveWorkbook
Set Output = Workbooks.Add
Application.DisplayAlerts = False
Dim i As Integer
For Each sh In Source.Worksheets
Dim newSheet As Worksheet
' select all used cells in the source sheet:
sh.Activate
sh.UsedRange.Select
Application.CutCopyMode = False
Selection.Copy
' create new destination sheet:
Set newSheet = Output.Worksheets.Add(after:=Output.Worksheets(Output.Worksheets.Count))
newSheet.Name = sh.Name
' make sure the destination sheet is selected with the right cell:
newSheet.Activate
firstCell = sh.UsedRange.Cells(1, 1).Address
newSheet.Range(firstCell).Select
' paste the values:
Range(firstCell).PasteSpecial Paste:=xlPasteColumnWidths
Range(firstCell).PasteSpecial Paste:=xlPasteFormats
Range(firstCell).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Next
' delete the sheets that were originally there
While Output.Sheets.Count > Source.Worksheets.Count
Output.Sheets(1).Delete
Wend
FileName = "C:\Dropbox\0 EPAS Export\ValueOnly_" & OriginalName
Output.SaveAs FileName
'Output.Close
Application.ScreenUpdating = True
End Sub
Error 91on lineNotCopySheet = ActiveSheet.Range("NotCopy").Cells(1, 1).Value? What is the value ofActiveSheet.Range("NotCopy").Cells(1, 1).Value? Are you sure the range is namesNotCopy?2 (=SHEET()). 3), that's a text, and you are trying to store it in a numeric variable. That may cause the error.=SHEET(). The value that the function return is 2. The number displayed in the cell/range is 2. The rangeNotCopyhas only 1 cell.