Ugh, merged cells, the bane of VBA.
Very well, we can try and work around it. We'll have the code clear out any formatting in the destination (since we're copying the formatting, shouldn't matter). New code:
Code:
Sub CopySheets()
Dim sourceWB As Workbook
Dim destWB As Workbook
Dim ws As Worksheet
Set sourceWB = ThisWorkbook
'Asssume destination workbook is already open
Set destWB = Workbooks("DestWB.xls")
Application.ScreenUpdating = False
For Each ws In sourceWB.Worksheets
'Check sheet's name and make sure it's visible
If ws.Name <> "Contents" And ws.Visible = True Then
'Clear any formatting currently in destination
destWB.Worksheets(ws.Name).Cells.Clear
ws.UsedRange.Copy
'Paste values and formats to destination workbook
'Assumes destination workbook already has sheets named correctly
With destWB.Worksheets(ws.Name).Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
End With
End If
Next ws
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
For a teaching moment, I noticed that in your edits, you like Select-ing everything. 99% of the time, you don't need to (and shouldn't) select items, as it slows things down, and you can interact with the object directly.
In this line:
destWB.Worksheets(ws.Name)
We're saying to go to the destination workbook, and find a worksheet with same name as our source (ws) name. When you wrote this:
destWB.Worksheets(ws.Name).TheSetRange.Select
It should cause a compile error. TheSetRange was a variable you created that referred to a range on the source worksheet. So, there's no way the code can go to the destination worksheet and select the range on source worksheet.
A analogy to explain:
Each object in VB can be thought of as belonging to a "larger" parent. This is similar to how we can say "Go to the USA (workbook), to the state of Indiana (worksheet), to the city of Indianapolis (range)." If you and I are both in Indiana, I could say "Go to Indianapolis" and you know exactly what I mean, because you're already in Indiana. However, if you were in Great Britain, and I said "Go to Indianapolis", you'd look around and get confused because there's no Indianapolis in Great Britain. Hopefully this explains a little bit about when in VBA we need to call out workbook and/or worksheet names, and when we don't.