The following vba is giving me an error and I can't figure it out. What I would like to do is take all the worksheets in my active workbook and copy them to another workbook that I open using GetOpenFilename.
When I run it, I get the error Type Mismatch.
Any suggestions from you experts as to what I am doing wrong here? I would really appreciate your help with this.
Smiddy
Dim FromBook As Variant
Dim ToBook As Workbook
Dim FromSheet As Worksheet
Dim ToSheet As Worksheet
Dim strFileName As String
Set FromBook = ActiveSheet
strFileName = Application.GetOpenFilename("All Files (*.*), *.*", 1, "Select File")
If strFileName = "False" Then Exit Sub
Set ToSheet = Workbooks.Open(strFileName)
Dim ws As Worksheet, flg As Boolean
For Each FromSheet In FromBook.Sheets
FromSheet.Copy After:=ToSheet
Next FromSheet
Tried to copy active sheet to sheet I opened. Expected to copy all worksheets from Active workbook into the workbook I opened. Received a Type Mismatch error.
CodePudding user response:
Make changes as below
Dim FromBook As Workbook
Dim ToBook as Workbook
....
Set FromBook = ActiveWorkbook
Set ToBook = Workbooks.Open(strFileName)
..
For Each FromSheet In FromBook.Sheets
FromSheet.Copy After:=ToBook.Sheets(1)
Next
Everything else seems perfect