running a macro in a macro

royboy531

Board Regular
Joined
Nov 11, 2005
Messages
52
I am trying to record a macro that includes a userform to filter data and then a code to select data . After I record the macro involving all the steps that I need and then try to run the recorded macro it doesn't seem to work. Here is the code I have for the userform (its sorts info for date and shift) and the code for selecting data. I am new at this and don't know much about writing code so the record method seemed the way to go. Does anyone know why it might not be working.

[Private Sub btnFilter_Click()
Dim startDate As String
Dim shift As String

'reads date from the userform
startDate = tbstartdate.Value 'tbStartDate is the name of the textbox for Starting Date
shift = tbshift 'tbshift is the name of the textbox for shift

'FILTERS
With Worksheets("Nov2005")
.AutoFilterMode = False
.Range("a1:p1").AutoFilter
.Range("a1:p1").AutoFilter Field:=2, Criteria1:=shift
.Range("a1:p1").AutoFilter Field:=1, Criteria1:=(">=" & startDate), _
Operator:=xlAnd, Criteria2:=("<=" & startDate)

End With
End Sub]

and the code to select the data-

[Sub macro1()
Range("A1:D" & Cells(Rows.Count, "A").End(xlUp).Row).Select
End Sub
]

just in case I was not clear when i record the macro I start at the spread sheet open VB run the userform (filters data), close userform manually (haven't got that code quite figured out yet), run the select code, close VB, copy selected data and paste it on another sheet. I hope that makes sence. any help I can get would be appriciated. thanks
royboy531
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Roy

Try (untested)
Code:
Private Sub btnFilter_Click() 
Dim startDate As String 
Dim shift As String 

'reads date from the userform 
startDate = tbstartdate.Value 'tbStartDate is the name of the textbox for Starting Date 
shift = tbshift 'tbshift is the name of the textbox for shift 

'FILTERS 
With Worksheets("Nov2005") 
.AutoFilterMode = False 
.Range("a1:p1").AutoFilter 
.Range("a1:p1").AutoFilter Field:=2, Criteria1:=shift 
.Range("a1:p1").AutoFilter Field:=1, Criteria1:=(">=" & startDate), _ 
Operator:=xlAnd, Criteria2:=("<=" & startDate) 

End With

Range("A1:D" & Cells(Rows.Count, "A").End(xlUp).Row).copy destination:=sheets("sheet2").range("a1")

unload userform1
 
End Sub

It assumes that the current form is Userform1, the output is going to sheet2, nothing is done with any existing output that may be in that area.


Tony
 
Upvote 0

Forum statistics

Threads
1,224,394
Messages
6,178,341
Members
452,841
Latest member
GenAkaman

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