Function looks at Sheet1 instead of active

Reset

Board Regular
Joined
Apr 16, 2010
Messages
227
You are going to say "please use code tags". I don't know how, please show me. And on to the question!

I am trying to run through several sheets and pull up data from them. However, even though it shows (via msgBox to pause execution) I am on the sheet of choice, all the data calls are going to Sheet1. Here's the code:
Code:
Private Sub CommandButton1_Click()
c = Sheets.Count
For Z = 1 To c
If Sheets(Z).Name = "Sheet1" Then GoTo line1
If Sheets(Z).Name = "dat" Then GoTo line1
Sheets(Z).Activate
Sheets(Z).Select
    a = Application.WorksheetFunction.CountA(Columns(1))
    MsgBox a
    b = Application.WorksheetFunction.CountA(Columns(9))
    
    If a - b < 1 Then GoTo line1
    ReDim q(a - b) As Integer
    inc = 0
    For x = 2 To a
        If Cells(x, 9) = "" Then q(inc) = Cells(x, 1): inc = inc + 1
    Next x
    Sheets("dat").Select
        v = Application.WorksheetFunction.CountA(Columns(1))
        For y = 1 To inc
            Cells(v + y, 1) = q(y - 1)
            Cells(v + y, 2) = Sheets(Z).Name
            Cells(v + y, 3) = Cells(v + y, 2) & ", " & Cells(v + y, 1)
        Next y
        
line1: 'jump out
Next Z

The MsgBox a gives 0, which is the expected result in Sheet1 (I put garbage in column 1 to verify and it comes out with expected result). So why is this going to Sheet1?
 
Last edited by a moderator:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Code tags:
[code]code goes here[/code]
 
Upvote 0
Shouldn't that be:
Code:
a = Application.WorksheetFunction.CountA(Sheets(Z).Columns(1))
etc etc?
 
Upvote 0
Hello,

What is the REDIM statement doing? Should q be equal to a-b?

While creating a post in the editing window that we type into, you will see a pound symbol (#), this will insert the code tags around highlighted text or insert the tags with cursor in between.

Code:
Private Sub CommandButton1_Click()
c = Sheets.Count
    For Z = 1 To c
        If Sheets(Z).Name = "Sheet1" Or Sheets(Z).Name = "dat" Then GoTo line1
            Sheets(Z).Activate
            a = Application.WorksheetFunction.CountA(Columns(1))
                MsgBox a
            b = Application.WorksheetFunction.CountA(Columns(9))
        If a - b < 1 Then GoTo line1
 
        'ReDim q(a - b) As Integer
        q = a - b
 
            inc = 0
        For x = 2 To a
            If Cells(x, 9) = "" Then q(inc) = Cells(x, 1): inc = inc + 1
        Next x
 
        Sheets("dat").Select
            v = Application.WorksheetFunction.CountA(Columns(1))
                For y = 1 To inc
                    Cells(v + y, 1) = q(y - 1)
                    Cells(v + y, 2) = Sheets(Z).Name
                    Cells(v + y, 3) = Cells(v + y, 2) & ", " & Cells(v + y, 1)
                Next y
line1: 'jump out
    Next Z
End Sub
 
Upvote 0
Glenn,

Yes that's true, but I am wondering why. I have done it before without having to call out the sheet name every time. Is it perhpas because it is coming from "Private Sub CommandButton1_Click()"? I've actually left out a bit and will be putting in more code and it get tedious. Thanks.
 
Upvote 0
If you refer to Range in code located in a worksheet code module, it refers to a range on that same sheet, not the active sheet. You must specify (as you always should if possible) which sheet the range belongs to.
 
Upvote 0
Still going nuts here. I put in sheet references everywhere, but there is still one issue. I need to clear all the contents of the sheet named "dat" before repopulating it with this data. At the very beginning I've tried everything but can't get it to work. I have tried:

Code:
Sheets("dat").Select
Range(Columns(1), Columns(3)) = ""
' clears range on Sheet1
 
Sheets("dat").Select
Columns("A:C").Select
Selection.ClearContents
' this is from the record macro function and when placed in my code give
'run-time error 1004: application-defined or object-defined error
 
Sheets("dat").Range(Columns(1), Columns(3)).Delete
' run-time error 1004 again

And a couple of other variants. Why can't this work?
 
Upvote 0
You're not qualifying the range or columns statements. You can just use:
Code:
Sheets("dat").Range("A:C").ClearContents
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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