"Cells" function for sheet 1 not working

QuiksilverHg

New Member
Joined
Jul 2, 2011
Messages
4
I'm trying to see if a group of cells (on another sheet) is blank as a condition before continuing. I figured out how to check it correctly when I list the cells as a range, but it will not work with the cells command unless I have the sheet selected. Any ideas?

This is what I would like to do:

If Application.CountBlank(Sheets(1).Range(Cells(55, scopy), Cells(59, scopy))) < 5 Then

Where scopy is a variable which will change depending on which column I need to copy from. I am trying to do this entire program without switching back and forth between sheets. Sheet 5 is selected the entire time.

I've tried breaking it down to make sure I had the formatting right for the cells, but these 'test lines' do not work either:
Sheets(1).Cells(55, scopy).Select
Sheets(1).Range(Cells(55, scopy), Cells(59, scopy)).Select

Again it will work if I have sheet 1 selected, but not while sheet 5 is selected. Anyone know what I'm doing wrong or if there's another way to do this?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Two parts to your question.

First, qualify the Cells reference with the parent sheet, example, instead of this you are using which does not work:

If Application.CountBlank(Sheets(1).Range(Cells(55, scopy), Cells(59, scopy))) < 5 Then

Instead use:

With Sheets(1)
If WorksheetFunction.CountBlank(Sheets(1).Range(.Cells(55, scopy), .Cells(59, scopy))) < 5 Then
MsgBox "Yes, less than 5 cells are blank"
Else
MsgBox "No, not less than 5 are blank"
End If
End With

Note that I am using WorksheetFunction instead of Application which will make your code more efficient when dealing with VBA functions.

The second item is how to go to a specific range on a sheet when executing the code line to do so from another sheet.

Instead of this which is a common mistake and does not work:

Sheets(1).Cells(55, scopy).Select
or
Sheets(1).Range(Cells(55, scopy), Cells(59, scopy)).Select

Utilize GoTo, again, qualifying the Cells object like so:

With Sheets(1)
Application.Goto Range(.Cells(55, scopy), .Cells(59, scopy))
End With
 
Upvote 0
Tom,

Thank-you for that reply, I will give it a shot.

Could you give me a short explanation on what the "application." portion of the command actually means, and tell me how that contrasts with the "WorksheetFunction." command.

I do much better if I can figure out why I am doing something vice just copying the code.
 
Upvote 0
I'm concerned with having an open-ended with for the entire if statement.

I am going to insert many lines of code dealing mostly with the main sheet (5) in the if statement. Wouldn't putting the "with sheets(1)" line in there confuse the program when I try to interact with sheet 5?
 
Upvote 0
Application resfers to the highest level object in Excel's object model, that being the Application of Excel itself. There is nothing higher than Application, meaning its collection of methods and properties, and all of their methods and properties, is the largest and hence most extensive for VBA to compile. What you *could* do is to write
Application.WorksheetFunction.CountBlank...
but as long as you are doing that, you could chop off the Application qualifier because VBA would know you are working in Excel unless you specified otherwise such as if you are working with the Word or PowerPoint app, which in this case you are not.

To demonstarte further, go into the VBE, and from the menu bar click Tools > Options > Edit tab and select Auto List Members. Click OK, and then in the Immediate window or in some module, type Application and the dot character and you will see the Intellisense drop-down list for the Application object. Now try doing that with
WorksheetFunction.
and the VBA worksheet functions will appear in the drop-down list, quite a few but not as many for VBA to sift through as it would by using just Application.

You can also look at the Object Browser by hitting F2 in the VBE and seeing the Application object's members, vs other objects' members.

I built a project some years ago using Application for worksheet functions and then changed the code to directly specify WorksheetFunction, and tested the run time difference, which was measurable.
 
Upvote 0
I'm concerned with having an open-ended with for the entire if statement.

I am going to insert many lines of code dealing mostly with the main sheet (5) in the if statement. Wouldn't putting the "with sheets(1)" line in there confuse the program when I try to interact with sheet 5?
Well I don't know because you have not talked about the extent of your macro. If Sheet5 is active when that part of the code is executing then you don't ahve to worry about qualifying it. If neither Sheets(1) nor Sheet5 are active then you can still use a With structure for Sheet5, just don't include Sheet5 in the With structure of Sheets(1) by not placing a dot in front of Sheet5, example, you would not want to do this:

With .Sheet5
yada yada


And you don't need to have an open-ended With in the first place, you could terminate it as I did, and maybe have Boolean variables for True or False thereafter to determine what you will be doing. Hard to answer your concern beyond that because you have not given details about the scope of your code.
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,611
Members
452,931
Latest member
The Monk

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