Hello everyone, I am new to VBA for excel and I would like your help for a problem.
I have two excels, A and B to be created in runtime. I need to copy only the values of all the worksheets from A (except one), to B, without specific filters or range.
The following code copies all the sheets of A to B but it always does it on the same sheet of B. I tried to write the instruction to create new sheets in B but I get strange index errors.
Any help is welcome.
Thanks
I have two excels, A and B to be created in runtime. I need to copy only the values of all the worksheets from A (except one), to B, without specific filters or range.
The following code copies all the sheets of A to B but it always does it on the same sheet of B. I tried to write the instruction to create new sheets in B but I get strange index errors.
Any help is welcome.
Thanks
VBA Code:
Sub Finalize()
Dim xlsxFullName As String
Dim newWb As Workbook
Dim wkSht As Worksheet
Dim destSheet As Worksheet
'Dim i As Integer
With Application.FileDialog(msoFileDialogSaveAs)
.Title = "Save sheet values"
If .Show Then
xlsxFullName = .SelectedItems(1)
Else
xlsxFullName = ""
End If
End With
If xlsxFullName <> "" Then
Set newWb = Workbooks.Add
'i = 0
For Each wkSht In ThisWorkbook.Sheets
'i = i + 1
If wkSht.Name <> "MAIN" Then
wkSht.Cells.Copy
'Set destSheet = newWb.Sheets.Add(After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))
' Reponse = MsgBox(destSheet.Name, vbInformation)
'newWb.Worksheets(destSheet).Paste
newWb.Worksheets(1).Paste
With newWb.Worksheets(1).UsedRange
.Value = .Value
End With
newWb.Worksheets(1).Name = wkSht.Name
End If
Next
'ThisWorkbook.Worksheets("Sheet2").Cells.Copy
'newWb.Worksheets(1).Paste
'With newWb.Worksheets(1).UsedRange
' .Value = .Value
'End With
'newWb.Worksheets(1).Name = "Sheet2"
'Suppress warning if new workbook already exists
Application.DisplayAlerts = False
On Error Resume Next
newWb.SaveAs xlsxFullName, FileFormat:=xlOpenXMLWorkbook
newWb.Close SaveChanges:=False
If Err.Number = 0 Then
MsgBox "Values and formatting saved as " & xlsxFullName, vbInformation
Else
MsgBox "Errors, file not saved", vbExclamation
End If
On Error GoTo 0
Application.DisplayAlerts = True
End If
End Sub