Consulting

Results 1 to 4 of 4

Thread: Why Does Command Button Select and Highlight Whole Page

  1. #1
    VBAX Expert
    Joined
    May 2006
    Location
    Oklahoma City, OK
    Posts
    532
    Location

    Why Does Command Button Select and Highlight Whole Page

    When CommandButton1 is clicked it performs its function, but when it selects worksheet "Dashboard" it actually selects it and highlights the whole page like when you click the top left corner of the worksheet. When CommandButton2 is clicked it does what it's suppose to do, but it actually selects worksheet "Dashboard" without hightlighting the whole page. Is there a reason this is taking place???

    Best regards,

    Charlie


    [VBA]Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    With frmRequest.ListBox1
    'Check for selected item
    If (.Value <> vbNullString) Then
    Range(.RowSource)(.ListIndex + 1, 1).Value = UserForm4.TextBox1.Value
    Range(.RowSource)(.ListIndex + 1, 2).Value = UserForm4.TextBox2.Value
    Range(.RowSource)(.ListIndex + 1, 3).Value = UserForm4.TextBox3.Value
    Range(.RowSource)(.ListIndex + 1, 4).Value = UserForm4.TextBox4.Value
    Range(.RowSource)(.ListIndex + 1, 5).Value = UserForm4.TextBox5.Value

    Else
    MsgBox "Please Enter Data"

    End If
    End With
    'End With
    Unload Me
    Cells.Select
    Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, _
    Key1:=Range("B2"), Order1:=xlAscending, _
    Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Sheets("Dashboard").Select
    Application.ScreenUpdating = True
    End Sub


    Private Sub CommandButton2_Click()
    Application.ScreenUpdating = False
    Unload Me
    Sheets("Dashboard").Select
    Application.ScreenUpdating = True
    End Sub[/VBA]

  2. #2
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    This is what's selecting the entire sheet:
    [VBA]Cells.Select
    [/VBA]

    If you don't want all of the cells selected after CommandButton1's code is complete, then add this after the sort command:
    [VBA]Range("A1").select[/VBA]

  3. #3
    VBAX Expert
    Joined
    May 2006
    Location
    Oklahoma City, OK
    Posts
    532
    Location
    Thanks MikeO I'll give it a shot.

    Best regards,

    Charlie

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    No need to select to sort
    [VBA]
    Cells.Sort Key1:=Range("D2"), Order1:=xlAscending, _
    Key1:=Range("B2"), Order1:=xlAscending, _
    Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    [/VBA]
    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'

Posting Permissions

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