Paste active workbook sheet to new workbook but no extra rows

mssbass

Active Member
Joined
Nov 14, 2002
Messages
252
Platform
  1. Windows
My code takes the data from the worksheet selected through a filedialog to my current worksheet (Staffing_Open_Report); however, I only have 750 rows in the worksheet selected so it pastes #N/A values from 751-2500. How do I change the code to only paste the number of rows from the selected workbook? The problem is in the 3rd row from bottom: Application.Workbooks("Star Ninja Tool 10.xlsm").Sheets("Staffing_Open_Report").Range("B2:AF2500").Value = ActiveWorkbook.Sheets("Staffing_Open_Report").Range("A2", ActiveCell.SpecialCells(xlLastCell)).Value


Dim template As Object
Dim myfile As Object
Dim wsDest As Worksheet
Dim lDestLastRow As Long

Set wsDest = Workbooks("Star Ninja Tool 10.xlsm").Worksheets("Staffing_Open_Report")
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row

wsDest.Range("A3:AF" & lDestLastRow).ClearContents

Set template = ActiveWorkbook
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "\\ccx.carecentrix.com\shares\public\TATMonitoring\RoutingPointStaffingReport"
.filters.Clear
.Title = "Staffing Open Report Selection"
If Not .Show Then
MsgBox "No file selected.": Exit Sub
End If
Workbooks.OpenText .SelectedItems(1)
End With

Set myfile = ActiveWorkbook
ActiveWorkbook.Sheets(1).Activate
Application.Workbooks("Star Ninja Tool 10.xlsm").Sheets("Staffing_Open_Report").Range("B2:AF2500").Value = ActiveWorkbook.Sheets("Staffing_Open_Report").Range("A2", ActiveCell.SpecialCells(xlLastCell)).Value

myfile.Close
template.Activate
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

BobUmlas

Well-known Member
Joined
Mar 14, 2002
Messages
1,181
Change the "offending" statement to 2 statements:
n= ActiveCell.SpecialCells(xlLastCell).Row
Application.Workbooks("Star Ninja Tool 10.xlsm").Sheets("Staffing_Open_Report").Range("B2").Resize(n-1).Value = ActiveWorkbook.Sheets("Staffing_Open_Report").Range("A2", ActiveCell.SpecialCells(xlLastCell)).Value
 

mssbass

Active Member
Joined
Nov 14, 2002
Messages
252
Platform
  1. Windows
That only brought in column A from the source document. The source document has columns A:AE
 

BobUmlas

Well-known Member
Joined
Mar 14, 2002
Messages
1,181
Oops - sorry - didn't notice that. Change to:
n= ActiveCell.SpecialCells(xlLastCell).Row
Application.Workbooks("Star Ninja Tool 10.xlsm").Sheets("Staffing_Open_Report").Range("B2").Resize(n-1,31).Value = ActiveWorkbook.Sheets("Staffing_Open_Report").Range("A2", ActiveCell.SpecialCells(xlLastCell)).Value
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,155
Messages
5,835,697
Members
430,379
Latest member
QCLAN

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
Top