Confirm Selection Before Running Macro

  • Hi,
    I have an excel form where users can input modifications by selecting a range and then pressing a command button to input a value in the selection.

    My goal is: to restrict the location where a user can select a range of cell and then execute the macro . The cells where the user can use the macro are merged and in my code, I used the address as show in the address bar in excel for their location. If the user select a cell that is not permitted, a message box is displays and exits the code.




    Code
    If (Selection.Address <> Range("AE7").Address) _
    Or (Selection.Address <> Range("BM7").Address) Then
    Msgbox ("Wrong location") Else


    For some reason it doesn't work since the message box appears even if I am within the permitted locations.


    Note: AE7 is the first cell of a merge, same as with BM7


    Thank you so much

  • Re: Cell Restriction


    Hey Dom you almost had the code tags. Your first one is fine [noparse]

    Code
    [/noparse], but the second one should be like this [noparse]

    [/noparse] but dont use the single hyphens, I included them so they wouldnt create the effect.

  • Re: Cell Restriction


    Hi, thanks for the reply


    I tried changing my "or" to "and" but it still doesn't work.


    Code
    If Selection.Address <> Range("AE7").Address _
    And Selection.Address <> Range("BM7").Address Then
    Msgbox ("Wrong location") Else
  • Re: Cell Restriction


    The code is correct if you intend to allow only selection of those two single-cell ranges; it is not appropriate for multi-cell ranges.


    As written, it could be simplified to,

    Code
    If Selection.Address <> "$AE$7" _ 
    And Selection.Address <> "$BM$7" Then 
        ...


    ... but better would be to use named ranges on the worksheet


    Code
    If Selection.Address <> Range("ThisRange").Address _ 
    And Selection.Address <> Range("ThatRange").Address Then 
        Msgbox ("Wrong location") Else


    ... which would work for however the range is defined on the worksheet.

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Cell Restriction


    Hi,


    This may help it will test if any of the cells are in the range the user has selected.


    I have used a inputbox for the user to select the cells but you dont have to.


  • Re: Cell Restriction


    Hi Reafidy,
    Your code works well but it is a bit complicated for me to understand. In your code the restricted cell are where nothing can be changed. I want the inverse, I want the specific cells (ae7 & bm7) to be the only locations where modifications CAN be made.[hr]*[/hr] Auto Merged Post;[dl]*[/dl]Hi, I was able to switch the restrictions but I don't know how to get rid of the inputbox. I'm thinking that it is around there that I should delete something.

    Code
    On Error Resume Next
        Set rSelect = Application.InputBox(Prompt:="Please Select A Range...", Type:=8)
        If Err.Number <> 0 Then
             MsgBox "You Have Not Selected Anything!", vbExclamation
            Exit Sub
        End If
        On Error GoTo 0


    But when I try to delete this portion it gives me a runtime error 5, invalid procedure call or argument.

    Thanks I really appreciate all the help.
    Note: This code is part of the code posted by reafidy

  • Re: Restrict Users Cell Selection


    Please dont PM me asking for specic help. If you post back in the thread your post will get answered as soon as possible.


    Also, it helps if you post all the code you are now working with for this routine so I can see what changes youve made.


    If you dont want the inputbox swap this:


    Code
    On Error Resume Next 
        Set rSelect = Application.InputBox(Prompt:="Please Select A Range...", Type:=8) 
        If Err.Number <> 0 Then 
            MsgBox "You Have Not Selected Anything!", vbExclamation 
            Exit Sub 
        End If 
        On Error Goto 0


    for this:


    Code
    set rSelect = selection
  • Re: Restrict Users Cell Selection


    Thank you very much for your help IT WORKS. I am sorry that I PM you. I thought that people didn't really check the forum so often. I'm kind of new and I didn't mean to be pushy.


    Thank you for your help it is appreciated

  • Re: Restrict Users Cell Selection


    Hi I found another issue in the code. The user can only run the macro in the cells that I specified but the user can select more then one column even if the other cells selected are not part of the "valid cells". This causes the macro to run in invalid cells. I want the user to be able to select only one column at a time. Here is my full code:



    On another worksheet instead of having specific cells where users can run the macro, I have a range (R9:R52). I tried to substitue the Array with a range but it is not working.


    Code
    aRanges = Array("AE7", "BM7", "bx6", "bz4", "bm21", "dd14", "db31", "db35", "dd39", "Am64", "ch64", "dd72")


    with


    I have the same issue on this worksheet, I want the user to be able to only select one column at a time.


    Thanks
    Dom01

  • Re: Restrict Users Cell Selection


    Dom,


    Have you thought about using Excels standard sheet protection and locking the cells. You would need to select the user cannot select locked cells option. Of course this would not allow the users to edit the locked cells not sure if that is a problem or not?

  • Re: Restrict Users Cell Selection


    Hi,
    The sheet protection will not work because the other cells must be edited to enter text and such.

  • Re: Restrict Users Cell Selection


    re:"I'm trying to force the user to only select one column"

    Code
    rselect.columns.count

    should return 1 then? so check for that too?
    (I'm coming in at this point not having read the whole thread, so sorry if this response is not appropriate.)
    p45cal

  • Re: Confirm Selection Before Running Macro


    Hi,
    Thanks for the columns.count tip. I have a small problem. The cells where the user is allowed to run the macro is a mix of merged cells and single cells. When the selection is a merged cell the macro run but when the selection is a single cell it is not recognized as a valid selection. I think the problem is here:


    Code
    Dim aRanges As Variant 
    aRanges =  Array("AE7", "BM7", "bx6", "bz4", "bm21", "dd14", "db31", "db35", "dd39", "Am64", "ch64", "dd72")


    Where bz4,db31 & db35 are single cells and the rest are merged cells.
    Thanks

  • Re: Confirm Selection Before Running Macro


    ahhhh, merged cells and vba, bad bad bad. :):)


    Instead of using merge cells can you use Format Cells -Tools - Aligment - Horizontal - Centre Accross Selection.


    It would be better if you can.

  • Re: Confirm Selection Before Running Macro


    Hi,
    Since it is a standard company wide form, I can't really play with the formatting of the cell this way. By the way what is the really problem when dealing with merged cells in vba.


    Thanks

  • Re: Confirm Selection Before Running Macro


    Quote

    By the way what is the really problem when dealing with merged cells in vba.

    For what you are wanting to do right now and a never ending stream of others issues. Search any form or newsgroup for "Merged Cells" and see.


    I cannot think of 1 valid reason where one MUST use Merged Cells.

  • Re: Confirm Selection Before Running Macro


    Hi,
    I can't really use the center across because the merged cells are vertical. And there is no vertical center across selection option. I don't understand why the code is not applicable when the cells are not merged i.e. when a single cell is selected.


    Is it because of the array? I am completely clueless

    Code
    Dim aRanges As Variant 
    aRanges =  Array("AE7", "BM7", "bx6", "bz4", "bm21", "dd14", "db31", "db35", "dd39", "Am64", "ch64", "dd72")

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!