Variables for Ranges

mt

Board Regular
Joined
Feb 24, 2006
Messages
134
I am having trouble getting variable to work in this procedure. When I hardcode the ranges, it runs fine. The two variables I am having trouble with are 1.) defining the last row of the filter outSh Range, and 2.) defining the last row of the filter range.

Any help will be greatly appreciated of if you see any other issues that could improve the code.

Thanks
Mike
Code:
'Set Filter in Place
 
Set outsh = Worksheets("FilterCriteria")
For Each ce In outsh.Range("BN3", outsh.Cells(Rows.Count), 66).End(xlUp))   'BN58
For i = 1 To 10             ' Set i counter to the number of Data Sheets in Workbook
    ToRow = Worksheets("Report1").Range("A65536").End(xlUp).Row + 1 ' ToRow resets to new row after each loop
    With Worksheets("Data" & i)
        
        On Error Resume Next
        LastRow = .Range("A65536").End(xlUp).Row
         
        .Range("AO1").Value = "Resource"            'Criteria Range copied into Data sheets
        .Range("AO2").Value = ce.Value
        .Range("AP1").Value = "Date"
        .Range("AQ1").Value = "Date"
        .Range("AP2").Value = Worksheets("FilterCriteria").Cells(3, 3)
        .Range("AQ2").Value = Worksheets("FilterCriteria").Cells(3, 4)
        .Range("A4:AN" & LastRow).AdvancedFilter Action:=xlFilterInPlace, criteriarange:=.Range("AO1:AQ2") ', copytorange:=outsh.Cells(Rows.Count, ce.Column).End(xlUp).Offset(1, 0)
        .Range("AO1:AQ2").ClearContents
  End With
    
    Next i
    Copy_Rng        'Procedure below
    'Format_Print    'Procedure below
    Next ce
  End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi Mike

Is the following a typo?

Code:
For Each ce In outsh.Range("BN3", outsh.Cells(Rows.Count), 66).End(xlUp))   'BN58

looks like the .Cells(Rows.Count) is incorrect (shouldnt have the last parenthesis).

I always tend to define my ranges actually outside of the loops and then refer to them by a reference eg:

Code:
Set rng = Range("A1:A" & cells(65536,"N").End(xlUp).Row)

....

For Each cell in rng
....

Best regards

Richard
 

mt

Board Regular
Joined
Feb 24, 2006
Messages
134
Richard,
Thanks for the quick response.
I keep getting a "wrong number of arguements" error using rows.count . I have tried a number of different combinations, but can't seem to make it work.

Also, any idea why the LastRow variable is not working correctly? If I set the LastRow definition outside of the For loop, I was not sure how to define the range.

Thanks,
Mike
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Does this work any better?:

Code:
For Each ce In outsh.Range("BN3", outsh.Cells(Application.Rows.Count, 66).End(xlUp))   'BN58

There doesn't look to be anything wrong with your LastRow definition - is it Dimensioned as a Long (ie in case the value is greater than 32768)?

Richard
 

mt

Board Regular
Joined
Feb 24, 2006
Messages
134
Richard,
No luck with it. I have it hard coded to run for now. Maybe I'll discover what is wrong later. Thanks for your effort.

Mike
 

Forum statistics

Threads
1,141,734
Messages
5,708,168
Members
421,549
Latest member
Dtcfire

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