VBA Code Cell Select?

Javi

Active Member
Joined
May 26, 2011
Messages
440
Hi All,

Could someone please help me I'm stuck. I would like the code to bring me back to the cell I was in when I ran it. The code " Range("ActiveCell").Select" errors out on me.


Code:
Sub Finished_Edit()
'
' Finished_Edit Macro
'
    Columns("J:O").Select
    Selection.EntireColumn.Hidden = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    'ActiveSheet.Unprotect
    Range("ActiveCell").Select
            
'
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Code:
[COLOR=blue]Dim rngStart as Range[/COLOR]
[COLOR=blue]rngStart=Activecell[/COLOR]
    Columns("J:O").Select
    Selection.EntireColumn.Hidden = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    'ActiveSheet.Unprotect
    [COLOR=blue]rngStart.Select[/COLOR]
 
Upvote 0
If you don't select the range to hide, your selection will not change therefore no need to reselect it.

Code:
Sub Finished_Edit()
    Columns("J:O").EntireColumn.Hidden = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub

Note my signature ;)
 
Upvote 0
Thanks code worked great!

However I get an error if the button is clicked twice and the columns are hidden is there a way to prevent this.


Code:
Sub Finished_Edit()
'
' Finished_Edit Macro
'
  
    Columns("J:O").EntireColumn.Hidden = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
'
End Sub
 
Upvote 0
Sure thing:

Code:
Sub Finished_Edit()
    If not Columns("J:O").EntireColumn.Hidden then Columns("J:O").EntireColumn.Hidden = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
 
Upvote 0
Thank you very much!!! Worked just as you said.

Did you see my question about adding borders?
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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