Paste active workbook sheet to new workbook but no extra rows

mssbass

Active Member
Joined
Nov 14, 2002
Messages
253
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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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
 
Upvote 0
That only brought in column A from the source document. The source document has columns A:AE
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,144
Members
453,021
Latest member
Justyna P

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