Solved

Move from one cell to another

Posted on 2014-02-28
5
341 Views
Last Modified: 2014-02-28
Folks,
In my worksheet I need to disable cell O2 when the workbook is activated.
In cell M3 I need to:
1. Check that the value entered is between 1 and 12
2. If true then I need to enable cell O2 and move to cell O2 to enter my formula.
0
Comment
Question by:Frank Freese
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
Harry Lee earned 500 total points
ID: 39895777
fh_freese,

You can put these VBA into the workbook.

Alt-11 to go to VBA Editor. Ctrl-R to open Project Explorer.

In the project explorer, find the workbook you are working on, and double click on ThisWorkbook.

Copy and paste the following vba codes in there.

This will protect the all the sheets in the workbook except M3
Private Sub Workbook_Open()
Dim WS As Worksheet
For Each WS In Worksheets
With WS.Range("m3")
    .Locked = False
    .FormulaHidden = False
End With
    WS.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Next
End Sub

Open in new window

Upon change of M3, and if the entered value is between 1 and 12, unprotect sheet and jump to O2.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address = "$M$3" Then
    If Target.Value >= 1 And Target.Value <= 12 Then
        ActiveSheet.Unprotect
        Range("O2").Select
    End If
End If
End Sub

Open in new window

0
 

Author Comment

by:Frank Freese
ID: 39895874
question:
Can this be modified for just a single worksheet (:OffsetFunction") rather than the workbook?
0
 

Author Closing Comment

by:Frank Freese
ID: 39895963
I selected your answer by modifying you Workbook_SheetChange sub as follows:

If Target.Address = "$M$3" Then
    If Target.Value >= 1 And Target.Value <= 12 Then
        Range("O2").Select
    Else
    MsgBox "A number between 1 and 12 is required", vbInformation, "Incorrect Value"
    Worksheets("OffsetFunction").Range("O2,M3").ClearContents
    Range("M3").Select
    Exit Sub
   
    End If

Thanks for pointing me in the right direction!
0
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39896170
fh_freese,

Yes, I can change it to protect only 1 sheet. The only catch is the sheet name has to stay the same for all the related workbooks.

If you want it to protect only 1 worksheet,

you have to modify it this way.

Private Sub Workbook_Open()
Dim WS As Worksheet
For Each WS In Worksheets
    If WS.Name = "OffsetFunction" Then
        Set WS = Sheets("OffsetFunction")
        With WS.Range("M3")
            .Locked = False
            .FormulaHidden = False
        End With
        WS.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End If
Next
End Sub

Open in new window


Also, watch out! I didn't know the sheet name with I wrote these codes but now I know.
You have to change the other code as well. This way, it won't react to other sheets.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim WS As Worksheet
For Each WS In Worksheets
    If WS.Name Like "OffsetFunction" Then
        Set WS = Sheets("OffsetFunction")
        On Error GoTo EndNow
        If ActiveSheet.Name = WS.Name Then
            If Target.Address = "$M$3" Then
                If Target.Value >= 1 And Target.Value <= 12 Then
                    ActiveSheet.Unprotect
                    Range("O2").Select
                End If
            End If
        End If
    End If
Next
EndNow:
End Sub

Open in new window

0
 

Author Comment

by:Frank Freese
ID: 39896212
Thanks Harry,
I'll pick up your recommendation.
Appreciate your help
Frank
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Indirect formula 9 30
TT Copy Formula 3 16
Excel Array formula issues 4 12
+1 to an even value 4 13
A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now