Returning to the active cell

MattNewby

Board Regular
Joined
Dec 15, 2009
Messages
155
Hi,


I have wrote a very simple macro and it isn't quiet working. What I am trying to do is as follows:
  • Store the active cell as a variable to be used later
  • Past special values in a column on Saving
  • Return to the cell that was active before the macro was run
My code is as follows:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ScreenUpdating = False

Dim mycel As Integer
mycel = ActiveCell
Sheets(1).Range("C:C").Copy
Sheets(1).Range("C:C").PasteSpecial xlPasteValues
Range("mycel").Select

ScreenUpdating = True
End Sub

I think it's just the bit in red that's wrong. If any one can advise it will be appreciated.

Thanks

Matt :banghead::banghead::banghead:
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Not sure why you need that; you aren't changing the selection.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    With Worksheets(1).Range("C:C")
        .Value = .Value
    End With
End Sub
 
Upvote 0
This is just a bit of test code to illustrate what I am trying to do.

When people enter to a shared spreadsheet I have set it up, so the user ID and date is entered into any blank cells in next to a set of populated cells. What I want to do is pastespecial these formulas and return the user to the cell they last had selected.

Hope this clafifys my question

Matt
 
Upvote 0
Nope does not work. It buggs just like the first line of code.

I am using excel 2003 if that makes a difference.

Matt
 
Upvote 0
If you use the code that shg has given you then it does just that without leaving the cell that the user is in so there is no need to re-activate it.

However I have a feeling that using Range("C:C") could leave you with a lot of dirty cells.

Maybe it would be better to restrict it to the used range only.

Rich (BB code):
With Sheets(1).Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)
        .Value = .Value
End With

edit:-

you really don't need this but the correct way would be

Rich (BB code):
Dim mycell As Range
Set mycell = Activecell
 
' your code here
 
mycell.Select
 
Last edited:
Upvote 0
Appologies,

I was not understanding, didn't realise you could use a with loop that way.

Err, yes it works fine.

Thanks

Matt
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

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