Macro Mystery - Code suddenly stopped working?

SuperFerret

Well-known Member
Joined
Mar 2, 2009
Messages
515
Hi all,

I have a macro which has been working for just over a year. Unfortunately as I've been seconded onto another project someone else has been amending the workbook and now the code doesn't work.

This is in Excel 2007 but we used to have to save it as a 2003-2007 compatible, but now we can save as xlsx or xlsm as required.

Code:
Sub Save_Me()
Dim ws As Worksheet, wb As Workbook
Application.DisplayAlerts = False
For Each ws In Sheets
    If ws.Name = "Item_Creation" Then
        Workbooks.Add
        Set wb = ActiveWorkbook
        With wb
            .SaveAs Filename:="S:\TRANS\Creates\" & Environ("username") & "\" & ws.Name & "_C" & ws.Range("A2").Value & "_" & Format(Date, "DD MMM") & FileExtStr = ".xlsm": FileFormatNum = 52
            ws.Copy Before:=.Sheets(1)
            .Sheets(1).Cells.Copy
            .Sheets(1).Cells.PasteSpecial xlValues
            .Save
            .Close
        End With
    End If
Next ws
Application.DisplayAlerts = True
End Sub

I get the error:
Run-time error '1004':
Excel cannot insert the sheets into the destination workbook, because it contains fewer rows and columns than the source workbook. To move or copy data to the destinations workbook, you can select the data, and then use the copy and paste commands to insert it into the sheets of another workbook.

Now I get why this is, but I need to be able to save this one worksheet (if I can save it as an xls then great) but I'm stuck. If someone could point me in the right direction I would be most appreciative :)
 
Hey Ferret, I'm good thanks, guessing I'm having less coding issues than you are! Rory's suggestion should work, mine was only in response to the code in #4 where you're defining FilePath based on the ws.Name but at that point in the code, ws hadn't been defined. You'd need to either explicitally define it prior (e.g. Set ws = ) or as Rory has within a FOR LOOP (e.g. For Each ws In ..)
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Thanks Rory that works a treat! :)

Jack, I always have coding issues! Granted the ones I am wrestling with nowadays tend to more SQL based than VBA! I might have a look at tweaking the other code as you suggested, just get my brain ticking over again.
 
Upvote 0
Thanks for all your help CircledChicken, you had more of an idea how to solve it than my stupid brain :LOL:

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,452
Messages
6,124,916
Members
449,195
Latest member
Stevenciu

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