Consulting

Results 1 to 8 of 8

Thread: Sleeper: MAcro Range Help

  1. #1
    VBAX Newbie
    Joined
    Nov 2004
    Posts
    2
    Location

    Sleeper: MAcro Range Help

    Private Sub CommandButton1_Click()
    Dim rngName As Range, intResponse
        Dim strName As String
        ActiveSheet.Unprotect
    strName = InputBox("Please Enter Guest Names", "Peverel Hall Hotel - Guest Confirmation")
    On Error Resume Next
        Set rngName = Range(strName)
        If Err.Number = 0 Then          ' no error, name already exists
        MsgBox "Name already exists." & vbCr & vbCr & _
               "Please enter a different name.", _
               vbExclamation, "Naming Conflict"
        Exit Sub
    End If
    On Error GoTo handler
        ActiveWorkbook.Names.Add name:=strName, RefersTo:=Selection
        Selection.Interior.ColorIndex = 4
        Selection.FormulaR1C1 = "C"
    handler:
            If Err.Number = 1004 Then
                MsgBox "You Have Entered No Data Or More Than 1 Word"
            End If
    ActiveSheet.Protect
    End Sub
    I have this macro and i only want it to work for cells B3:ad14 please help

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Seb,

    Are you trying to make this:

    ActiveWorkbook.Names.Add name:=strName, RefersTo:=Selection 
    Selection.Interior.ColorIndex = 4 
    Selection.FormulaR1C1 = "C"
    Refer to ALL cells in B3:AD14 (every time), or are you saying that you only want it to work on the selected range at any one time, provided it is outside of that listed?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Newbie
    Joined
    Nov 2004
    Posts
    2
    Location

    MAcor

    i only want the macro to work on cells b3:ad14

    cause i have to highlight the cells and click on the button and if i click out of that range it doesnt work

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Seb,

    Try changing this:
    ActiveSheet.Unprotect 
    strName = InputBox("Please Enter Guest Names", "Peverel Hall Hotel - Guest Confirmation")
    To this:
    ActiveSheet.Unprotect
    'Test if upper left cell is within range B3:AD14
        With Selection.Range("A1")
            If .Column < 2 Or .Row < 3 Then
                MsgBox "Sorry!  You selected cells " & Selection.Address & vbCrLf & _
                    "which is outside of the allowable range of $B$3 to $AD$14." & vbCrLf & _
                    "Please select a new range and try again!", vbOKOnly + vbCritical, _
                    "Invalid selection!"
                Exit Sub
            End If
        End With
    'Test if lower right cell is within range B3:AD14
        With Selection
            If .Range("A1").Column + .Columns.Count > 31 Or .Range("A1").Row + .Rows.Count > 15 Then
                MsgBox "Sorry!  You selected cells " & Selection.Address & vbCrLf & _
                    "which is outside of the allowable range of $B$3 to $AD$14." & vbCrLf & _
                    "Please select a new range and try again!", vbOKOnly + vbCritical, _
                    "Invalid selection!"
                Exit Sub
            End If
        End With
    strName = InputBox("Please Enter Guest Names", "Peverel Hall Hotel - Guest Confirmation")

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi,

    You could try the Intersect command. ..

    [vba]If Not Intersect(Selection, Range("B3:AD14")) Is Nothing Then
    'matching code here
    End If[/vba]

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey Zack,

    Thanks for that. I stumbled on the intersect method trying to test the top left, and then the bottom right cell. The bottom right one got me. Didn't realize that you could just do the whole range at once!

    Seb, use Zack's code. It's much more efficient, and will be way easier to maintain.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Seb,
    You might want to check out WorksheetChange macros, where the macro is run by a changed value in a range. The Insersect method is commonly used to limit the triggering to the required cells.
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Select Target Range B3:AD14 and Define name as ValidRng1
    You can re-define name in the future if your range changes and the macro would still work.

    Edit macro as follows:


    Private Sub CommandButton1_Click() 
    Dim rngName As Range, intResponse 
    Dim strName As String 
    ActiveSheet.Unprotect 
    MyRng = Application.ActiveCell.Address
    Set isect = Application.Intersect(Range("ValidRng1"), Range(MyRng))
    If Not isect Is Nothing Then
    strName = InputBox("Please Enter Guest Names", "Peverel Hall Hotel - Guest Confirmation") 
    On Error Resume Next 
    Set rngName = Range(strName) 
    If Err.Number = 0 Then ' no error, name already exists
    MsgBox "Name already exists." & vbCr & vbCr & _ 
    "Please enter a different name.", _ 
    vbExclamation, "Naming Conflict" 
    Exit Sub 
    End If 
    On Error Goto handler 
    ActiveWorkbook.Names.Add name:=strName, RefersTo:=Selection 
    Selection.Interior.ColorIndex = 4 
    Selection.FormulaR1C1 = "C" 
    handler: 
    If Err.Number = 1004 Then 
    MsgBox "You Have Entered No Data Or More Than 1 Word" 
    End If 
    End If  
    ActiveSheet.Protect 
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •