Copy only required cells to new workbook

CaptainGravyBum

New Member
Joined
Dec 1, 2023
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a macro set up to copy data from one sheet onto a new workbook before saving, but the command isn't copying just the data, it appears to be pulling in 200+ blank rows as well.
Is there a way to just copy the selected cells from sheet1?

Dim savePath As Variant
Dim newWb As Workbook
Dim newSheet As Worksheet
Dim sheetName As String
Dim currentDateAndTime As String
Dim defaultPath As String

Set ws = ThisWorkbook.Sheets("Sheet1")
currentDateAndTime = Format(Now(), "ddmmyyyy_hhmmss")
sheetName = ws.Name
Set newWb = Workbooks.Add
Set newSheet = newWb.Sheets(1)
ws.Cells.Copy newSheet.Cells
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
This "just" copies the used range:
VBA Code:
ws.UsedRange.Copy newSheet.Range("A1")
Note that the usedrange *may* start in a different cell than A1.
 
Upvote 1
Hi @jkpieterse
Thanks for your response. I tried this and the result is still the same unfortunately, I must be missing something with the spreadsheet because when saving as csv there are 200 rows extra created.
Maybe I'll have to admit defeat. I'm off to bang my head against a wall.
 
Upvote 0
Try running the code below on a copy of your sheet then try the code by @jkpieterse

VBA Code:
Sub LoseThatWeightx()

    Dim xx As Long, LastRow As Long, LastCol As Long

    Application.ScreenUpdating = False

    With ActiveSheet
        LastRow = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                              LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        LastCol = .Cells.Find(What:="*", After:=.Range("A1"), LookIn:=xlValues, _
                              LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        .Range(.Cells(1, LastCol + 1), .Cells(Rows.Count, Columns.Count)).Delete
        .Range(.Cells(LastRow + 1, 1), .Cells(Rows.Count, Columns.Count)).Delete
    End With
   
    xx = Application.ActiveSheet.UsedRange.Rows.Count
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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