HELP! method 'Range' of object '_Worksheet' failure

Sebastian K.

New Member
Joined
Jul 19, 2011
Messages
16
Hi

I'm trying to writ a small code to determin which column has the most filled cells.

I've got the following code:

Code:
    Dim LastRow As Long
    Dim ColCount As Integer
 
        LastRow = 0
 
        For ColCount = 19 To 31 Step 4
            If Range(Cells(Rows.Count, ColCount)).End(xlUp).Row > LastRow Then _
                LastRow = Range(Cells(Rows.Count, ColCount)).End(xlUp).Row
        Next ColCount

I've tried adding Worksheets("xxxx") and Sheets("xxxx") in front of the Range but it does'nt seem to help.

Any sugestions?

Best regards
Sebastian
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Perhaps

Code:
Dim LastRow As Long
Dim ColCount As Integer
 
LastRow = 0
With Sheets("xxx")
        For ColCount = 19 To 31 Step 4
            If .Range(.Cells(Rows.Count, ColCount)).End(xlUp).Row > LastRow Then _
                LastRow = .Range(Cells(Rows.Count, ColCount)).End(xlUp).Row
        Next ColCount
End With
 
Upvote 0
You only need to use Range(Cells... if you're referring to more than once cell. And you can combine the comparison between the row and lastrow variable into one statement...

Code:
Dim LastRow As Long
Dim ColCount As Integer
 
        For ColCount = 19 To 31 Step 4
            LastRow = WorksheetFunction.Max(LastRow, Cells(Rows.Count, ColCount).End(xlUp).Row)
        Next ColCount
 
Upvote 0
See change in red
Hi

I'm trying to writ a small code to determin which column has the most filled cells.

I've got the following code:

Code:
    Dim LastRow As Long
    Dim ColCount As Integer
 
        LastRow = 0
 
        For ColCount = 19 To 31 Step 4
            [COLOR="Red"]If Range("A" & Rows.Count).Offset(0, ColCount - 1).End(xlUp).Row > LastRow Then _
        LastRow = Range("A" & Rows.Count).Offset(0, ColCount - 1).End(xlUp).Row[/COLOR]
        Next ColCount

I've tried adding Worksheets("xxxx") and Sheets("xxxx") in front of the Range but it does'nt seem to help.

Any sugestions?

Best regards
Sebastian
 
Upvote 0
You only need to use Range(Cells... if you're referring to more than once cell. And you can combine the comparison between the row and lastrow variable into one statement...

Code:
Dim LastRow As Long
Dim ColCount As Integer
 
        For ColCount = 19 To 31 Step 4
            LastRow = WorksheetFunction.Max(LastRow, Cells(Rows.Count, ColCount).End(xlUp).Row)
        Next ColCount

Thanks ..... works nicely
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,487
Members
452,917
Latest member
MrsMSalt

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