Copying, Pasting and Saving USED Range Only

fluffynicesheep

Board Regular
Joined
Oct 27, 2009
Messages
69
Hi,

I currently have a spreadsheet that contains a tab that I want to copy and paste into a new document. It'll then save, close and exit both documents.

The VBA below works fine at the moment, and does as I need .... apart from one thing ... the new spreadsheet goes up to column WQW, and therefore makes scrolling through it a bit of a nightmare!!

The report tab that it gets the data from will always go up to column EN only, and that's where I'd like the new document to finish. The number of rows could well change each time .... so basically I need my range to be all used rows up to column EN ... and that's what I need to go into the new spreadsheet.

If you can let me know the line that needs changing from below that'll be great!

VBA Code:
Sub Save_and_Close()
Dim wbA As Workbook
Dim wbB As Workbook
Dim FName As String
  Sheets("Report").Select
Range("EM1").Value = Date
Range("EN1").Value = "Error Report -"
ActiveSheet.Range("A1").Select
    Application.ScreenUpdating = False

    Set wbA = ThisWorkbook

    wbA.Sheets("Report").Visible = True
   
    wbA.Sheets("Report").Copy

    Set wbB = ActiveWorkbook

    With wbB
   
        With .Sheets(1).UsedRange
            .Copy
            .PasteSpecial xlValues
            .PasteSpecial xlFormats
        End With
       
        Dim nm As Name
Dim DeleteCount As Long

'Loop through each name and delete
  For Each nm In ActiveWorkbook.Names
     
        If InStr(1, nm.RefersTo, "#REF!") Then nm.Delete
      On Error GoTo 0
    DeleteCount = DeleteCount + 1

Skip:
   
  Next

 
'Reset Error Handler
  On Error GoTo 0
     ActiveSheet.Range("A1").Select

        Application.CutCopyMode = False

FName = "Y:\National Careers Service - Mail Merge\E-Mail Attachments\Error Reports\Error Report - " & _
        Format(Range("EE2"), "dd-mm-yyyy") & ".xlsm"
       
   ActiveWorkbook.SaveAs Filename:=FName, _
                          FileFormat:=xlOpenXMLWorkbookMacroEnabled
        .Close SaveChanges:=False

    End With
     Application.ScreenUpdating = False
Sheets("Report").Select
ActiveWindow.ScrollColumn = 1
ActiveWindow.ScrollRow = 1
Cells(1, 1).Activate

Sheets("Dashboard").Select
ActiveWindow.ScrollColumn = 1
ActiveWindow.ScrollRow = 1
Cells(1, 1).Activate
ActiveCell.Next.Select

ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If the new workbook goes upto column WQW, then so does the report sheet your copying.
 
Upvote 0
Hi, thanks Fluff.

Is there any way to therefore select and copy up to row EN only - as anything past this is blank cells, and not required in the new workbook.

thanks
 
Upvote 0
Why not just clean-up the report sheet, so that the used range only refers to actual data?
That would
a) remove this problem
b) possibly reduce the size of your workbook
c) possibly improve the workbooks performance.

Always best to remove the source of the problem rather then jump through hoops trying to circumvent it.
 
Upvote 0
Hi Fluff,

The report sheet is actually an exported document from an external CRM system and unfortunately I cannot clean this up ..... that's just the way the product exports. The top row also has merged columns in.

So at the moment my macro in workbook 2 is simply opening up this report sheet and copying all cells into the new workbook - Once copied into a new sheet, it then unmerges all, and I can start working on it. But the fact that I select all cells is where I think the issue is. Therefore I'm just trying to find a way to copy all of the rows (A1 to EN something) from the external workbook - every line that has data in column EN.

I think once I can get that range sorted ... it should work.

Thanks
 
Upvote 0
In that case, how about
VBA Code:
        With .Sheets(1).UsedRange
            .Copy
            .PasteSpecial xlValues
            .PasteSpecial xlFormats
            .Parent.Range("EO:XFD").EntireColumn.Delete
        End With
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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