Excel VBA Command Button to open new workbook and copypaste rows

dlmoore99

Board Regular
Joined
May 20, 2011
Messages
88
Office Version
  1. 2019
Platform
  1. MacOS
I have a userform that contains two date pickers and a command button. I would like to copy all rows (14 columns) that fall between the two date pickers from my worksheet("TGT") to a new workbook worksheet when I click the cmd button. I keep getting a debugger at the "Range" code line. Thank you in advance. This is what I have so far:

[Private Sub cmdCSV_Click()
Dim lastrow As Long, i As Long, erow As Long
Dim sheetdate As Date, startdate As Date, enddate As Date
Dim wb As Workbook
Set wb = Workbooks.Add

startdate = DTPicker1.Value
enddate = DTPicker2.Value
lastrow = Worksheets("TGT").UsedRange.Rows.Count
For i = 2 To lastrow
sheetdate = Cells(i, 1)
If sheetdate >= startdate And sheetdate <= enddate Then
erow = Worksheets("TGT").UsedRange.Rows.Count + 1
Range(Cells(i, 1), Cells(1, 14)).Copy Destination:=wb.Worksheets("sheet1").Cells(erow, 1)

End If
Next i
End Sub
]
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I have a userform that contains two date pickers and a command button. I would like to copy all rows (14 columns) that fall between the two date pickers from my worksheet("TGT") to a new workbook worksheet when I click the cmd button. I keep getting a debugger at the "Range" code line. Thank you in advance. This is what I have so far:

Code:
Private Sub cmdCSV_Click()
 Dim lastrow As Long, i As Long, erow As Long
 Dim sheetdate As Date, startdate As Date, enddate As Date
 Dim wb As Workbook
 Set wb = Workbooks.Add

 startdate = DTPicker1.Value
 enddate = DTPicker2.Value
 lastrow = Worksheets("TGT").UsedRange.Rows.Count
 For i = 2 To lastrow
    sheetdate = Cells(i, 1)
        If sheetdate >= startdate And sheetdate <= enddate Then
            erow = Worksheets("TGT").UsedRange.Rows.Count + 1
            Range(Cells(i, 1), Cells(1, 14)).Copy Destination:=wb.Worksheets("sheet1").Cells(erow, 1)

    End If
 Next i
 End Sub

Can anyone help Please. Thank you.
 
Upvote 0
Try
Code:
   With Worksheets("TGT")
      If sheetdate >= StartDate And sheetdate <= enddate Then
         .Range(.Cells(i, 1), .Cells(1, 14)).Copy Destination:=wb.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
      End If
   End With
 
Upvote 0
Fluff, I entered code you provided where I thought it should go and now receive Runtime error 9 subscript out of range for lastrow = Worksheets("TGT").UsedRange.Rows.Count

Code:
Private Sub cmdCSV_Click()
Dim lastrow As Long, i As Long, erow As Long
Dim sheetdate As Date, startdate As Date, enddate As Date
Dim wb As Workbook
Set wb = Workbooks.Add

startdate = DTPicker1.Value
enddate = DTPicker2.Value
[COLOR=#FF0000]lastrow = Worksheets("TGT").UsedRange.Rows.Count[/COLOR]
For i = 2 To lastrow
    sheetdate = Cells(i, 1)
   With Worksheets("TGT")
      If sheetdate >= startdate And sheetdate <= enddate Then
         .Range(.Cells(i, 1), .Cells(1, 14)).Copy Destination:=wb.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
      End If
   End With
Next i
End Sub
 
Last edited:
Upvote 0
Do you have a sheet called TGT?
 
Upvote 0
Check you don't have any trailing/leading spaces.
 
Upvote 0
I can't find any. Did I place your code in the correct place?

Code:
Private Sub cmdCSV_Click()
Dim lastrow As Long, i As Long, erow As Long
Dim sheetdate As Date, startdate As Date, enddate As Date
Dim wb As Workbook
Set wb = Workbooks.Add
startdate = DTPicker1.Value
enddate = DTPicker2.Value
lastrow = Worksheets("TGT").UsedRange.Rows.Count
For i = 2 To lastrow
    sheetdate = Cells(i, 1)
[COLOR=#FF0000]        With Worksheets("TGT")
            If sheetdate >= startdate And sheetdate <= enddate Then
            .Range(.Cells(i, 1), .Cells(1, 14)).Copy Destination:=wb.Worksheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1)
            End If[/COLOR]
        End With
Next i
End Sub
 
Upvote 0
yes, you've got it in the right place, but if you're getting an error on this line
Code:
lastrow = Worksheets("TGT").UsedRange.Rows.Count
It's not getting to the new part.
The error message that you got normally means that it can't find a sheet called TGT.
Select the TGT sheet & run this, what does it say?
Code:
Sub chk()
MsgBox "|" & ActiveSheet.Name & "|"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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