Auto select next cell

meangreen

Board Regular
Joined
Jan 29, 2007
Messages
169
I have a macro that unhides the next row when I enter someting into the row above. I need help with my code to get it to automatically select the cell in column C of the newly unhidden row. I will need this to happen each time a new row is unhidden (up to 50 times). Here's my code:

Private Sub Worksheet_Calculate()
Dim rng As Range
Set rng = Range("A24")

If rng.Value <> "" Then
Application.EnableEvents = False
Rows("25:75").EntireRow.Hidden = True
Rows("25:" & rng.Value + 24).Hidden = False
Application.EnableEvents = True
End If
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi,

so you want to select column C, row "rng.Value + 24" ...
try this
Code:
Cells(rng.Value + 24, "C").Select
or
Code:
Range("C" & rng.Value + 24).Select

kind regards,
Erik
 
Upvote 0
Thank you - that works great. Now I have run into another problem though. I need to repeat this action three times at three different locations in the workbook. I can still get the rows to automatically unhide, but I can't get it to select the cell I want. The select cell function for rng no longer works after I added the code for rng2. Any time ows are unhidden in 25:75, the cell selected is in the 88:138 range. Here's my code:

Code:
Private Sub Worksheet_Calculate()
Dim rng As Range
        Set rng = Range("A24")
        If rng.Value <> "" Then
            Application.EnableEvents = False
                Rows("25:75").EntireRow.Hidden = True
                Rows("25:" & rng.Value + 24).Hidden = False
                Cells(24 + rng.Value, "C").Select
            Application.EnableEvents = True
        End If
        
        Set rng2 = Range("A87")
        If rng2.Value <> "" Then
            Application.EnableEvents = False
                Rows("88:138").EntireRow.Hidden = True
                Rows("88:" & rng2.Value + 87).Hidden = False
                Cells(87 + rng2.Value, "C").Select
            Application.EnableEvents = True
        End If
End Sub
 
Upvote 0
perhaps I'm missing something ?
it is normal that only the second "selection" can be seen

if you have code like
Code:
Range("A1).Select
Range("A3).Select
you will only see A3 selected

can you clarify what you want to do ?what would be the expected result ?
(perhaps two cells selected ?)
generally you do not need to select cells, but I can imagine you want this macro to "go" somewhere to make scrolling easier
 
Upvote 0
Ok, I'll try my best here. In 25:75, everytime a new row is unhidden I need the cell in coulmn C of that row to be selected. Then I have some other stuff, and in 83:138 I want to do the same thing from above. Everytime a row in 83:138 is unhidden, the cell in column C of the newly undden row will be selected. What's happening is, when I unhide a row in 25:75,
Code:
 Cells(24 + rng.Value, "C").Select
is no longer recognized and
Code:
 Cells(87 + rng2.Value, "C").Select
is. I need both to be reconized depending on which group (25:75 or 83:138) is being edited.

I think you actually answered my question in your previous post, but I was hoping there is a way around this.
 
Upvote 0
I just noticed something else too. I have a spinner button at the top to change the date. Because of my current code, each time I click it, C88 is selected - this is very annoying. Is there not a way to say: if the spinner button is clicked then no specific cell is selected, if a row is unhidden in 25:75 then select C in the newly unhidden row from that range, and if a row in 83:138 is unhidden then select C in the newly unhidden row from that range? It seems like something that should be pretty basic...
 
Upvote 0
you lost me a little bit, perhaps I'm tired, but I think you are complicating things ...
before going further, a question:

does Range("A24") house a formula or simply a value ?
and Range("A87") ?
 
Upvote 0
A formula. A24 counts the number of nonblank cells from C25:C75 and A87 counts the nonblank cells from C88:C138.

Maybe it would help if I explain why I have to tell it which cell to select. After I enter something into C25 and hit enter, the next row unhides - based on the result of my formula in A24. That part works perfectly. The problem is that after I hit enter, for some reason C36 becomes the selected cell (which is still hidden) rather than C26. I don't know why this is happening, so I am trying to force it to select the next cell (in this case C26).
 
Upvote 0
I'm still not sure you're understanding what I need, so I'll try to explain one more time. I'll walk you through each step of the process. The value in A24 is correct, it is actually the number of nonblank cells from C25:C75 +1. I added the +1 because I really want A24 to represent the number of visible rows from 25:75. Row 25 will always be visible. Once I add data into C25 and hit enter, A24 will change to 2 (to represent the 2 visible rows from 25:75). Here was my original code:
Code:
 Private Sub Worksheet_Calculate() 
Dim rng As Range 
        Set rng = Range("A24") 
        If rng.Value <> "" Then 
            Application.EnableEvents = False 
                Rows("25:75").EntireRow.Hidden = True 
                Rows("25:" & rng.Value + 24).Hidden = False 
            Application.EnableEvents = True 
        End If 
End Sub

This works perfectly for unhiding the correct row - I have no problems with unhiding the rows. My problem is that after entering data into C25 and hitting Enter, for some reason C26 (which should be default) is not selected but C36 is. I didn't like this so, I changed the code to this:
Code:
 Private Sub Worksheet_Calculate() 
Dim rng As Range 
        Set rng = Range("A24") 
        If rng.Value <> "" Then 
            Application.EnableEvents = False 
                Rows("25:75").EntireRow.Hidden = True 
                Rows("25:" & rng.Value + 24).Hidden = False 
                Cells(24 + rng.Value, "C").Select
            Application.EnableEvents = True 
        End If 
End Sub

That worked perfectly. This is where it get's tricky. I want to do the exact same thing as above in rows 88:138. So I editied the code to this:
Code:
 Private Sub Worksheet_Calculate() 
Dim rng As Range 
        Set rng = Range("A24") 
        If rng.Value <> "" Then 
            Application.EnableEvents = False 
                Rows("25:75").EntireRow.Hidden = True 
                Rows("25:" & rng.Value + 24).Hidden = False 
                Cells(24 + rng.Value, "C").Select 
            Application.EnableEvents = True 
        End If 
        
        Set rng2 = Range("A87") 
        If rng2.Value <> "" Then 
            Application.EnableEvents = False 
                Rows("88:138").EntireRow.Hidden = True 
                Rows("88:" & rng2.Value + 87).Hidden = False 
                Cells(87 + rng2.Value, "C").Select 
            Application.EnableEvents = True 
        End If 
End Sub

At first I thought this worked perfectly as well. I tested everything in the range of 88:138. The rows unhid as they should have, and the correct cells were being selected. However when I went back up to the 25:75 ranges and added data, this part of the code was no longer recognized:
Code:
 Cells(24 + rng.Value, "C").Select
and this was
Code:
 Cells(87 + rng2.Value, "C").Select

In other words rather than C26 being selected (after entering data into C25), C88 was being selected based on
Code:
 Cells(87 + rng2.Value, "C").Select

Then to complicate things further, each time I click my spinner button the same piece of code is being activated
Code:
 Cells(87 + rng2.Value, "C").Select
In this case, I don't want any specific cell to be selected after clicking the spinner button.


Basically it comes down to this, I need a way to be able to use
Code:
 Cells(24 + rng.Value, "C").Select
AND
Code:
 Cells(87 + rng2.Value, "C").Select
on the same sheet depending on which part of the sheet I am entering data. Then also, have the spinner button not recognize either if them.

I'm afraid I already know the answer to this (it can't be done), but if you can figure this out, I will be forever grateful and to show my appreciation I will become a member of DRAFT. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,215,719
Messages
6,126,432
Members
449,314
Latest member
MrSabo83

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