.Range(Cells(n,m),Cells(x,y)).address produces error?

pjmorris

Well-known Member
Joined
Aug 2, 2012
Messages
2,172
Office Version
  1. 365
Platform
  1. Windows
I'm sure this is simple, but the following code produces an error "Application Defined or object-defined error". Grateful for your suggestions to correct.

Code:
Function AvailCol(nAvail) As String
    Dim cCol As Integer 'column in which availability should be found
    Dim nRows As Integer 'number of rows to be included in range
    
    cCol = Worksheets("CoE Manager Data").Range("$C$2:$U$2").Find(Left(nAvail, 3)).Column
    nRows = WorksheetFunction.CountIf(Worksheets("CoE Manager Data").Range("$B$3:$B$4000"), "<>0")
    
    AvailCol = Worksheets("CoE Manager Data").Range(Cells(3, cCol), Cells(nRows, cCol)).Address
End Function


I had hoped to replace this rather ungainly formula with 'AvailCol':

Code:
   = OFFSET('CoE Manager Data'!$C$3,0,MATCH(LEFT('ECP Summary'!$B10,3),'CoE Manager Data'!$C$2:$U$2,0)-1,COUNTIF('CoE Manager Data'!$B$3:$B$4000,"<>0"))

Regards
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi ,

This does away with the error ; I have not checked whether it replicates the behaviour of your formula.
Code:
Function AvailCol(nAvail) As String
    Dim cCol As Integer 'column in which availability should be found
    Dim nRows As Integer 'number of rows to be included in range
    
    With ThisWorkbook.Worksheets("CoE Manager Data")
         cCol = .Range("$C$2:$U$2").Find(Left(nAvail, 3)).Column
         nRows = WorksheetFunction.CountIf(.Range("$B$3:$B$4000"), "<>0")
    
         AvailCol = .Range(.Cells(3, cCol), .Cells(nRows, cCol)).Address
    End With
End Function
 
Upvote 0
brilliant, worked a charm!

Obviously it was the unqualified Cells functions that were failing!

Thanks again for prompt reply, much appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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