Results 1 to 8 of 8

Thread: Excel Macro to Highlight Row

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Excel Macro to Highlight Row

    Hi,

    I would like to set up a right click option to highlight the row i have right clicked on (if that makes sense). I have already set up the right click option and the highlight option but it doesn't work and presents with the error: User-Defined type not defined. Here is the code i am using:

    Option Explicit

    Private Sub BuildCustomMenu2()

    Dim ctrl As CommandBarControl
    Dim btn As CommandBarControl
    Dim i As Integer


    'add a 'popup' control to the cell commandbar (menu)
    Set ctrl = Application.CommandBars("Cell").Controls.Add _
    (Type:=msoControlPopup, Before:=1)
    ctrl.Caption = "Highlight"
    For i = 50 To 50 'add a few menu items
    'add the submenus
    Set btn = ctrl.Controls.Add
    btn.Caption = "Highlight Yellow" 'give them a name
    btn.OnAction = "HighlightYellow" 'the routine called by the control



    Next

    End Sub

    Private Sub DeleteCustomMenu2()

    Dim ctrl As CommandBarControl

    'go thru all the cell commandbar controls and delete our menu item
    For Each ctrl In Application.CommandBars("Cell").Controls
    If ctrl.Caption = "Highlight" Then ctrl.Delete

    Next

    End Sub
    Sub Highlight(ByVal Target As Value)

    Dim RngRow As Range
    Dim RngFinal As Range
    Dim Row As Long
    Dim Target As Value

    'Cells.Interior.ColorIndex = xlNone

    Row = Target.Row


    Set RngRow = Range("A" & Row, Target)
    Set RngFinal = RngRow

    RngFinal.Interior.ColorIndex = 6

    End Sub

    If anyone could please tell me why i am getting this error and how i can make this work i would be very appreciative.

    Thanks.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Macro to Highlight Row

    btn.OnAction = "HighlightYellow" 'the routine called by the control
    i do not see any procedure named highlightyellow in the posted code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Re: Excel Macro to Highlight Row

    Hi i fixed the error but when i try to run the macro I still get the same error: user-defined type not defined do you know why ? this has me very lost :L

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Macro to Highlight Row

    if you are calling the highlight procedure, you need to pass a range for the target parameter, but as i can not see your fixed code, i can not tell
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Re: Excel Macro to Highlight Row

    Hi, sorry the fixed code is:

    Option Explicit

    Private Sub BuildCustomMenu2()

    Dim ctrl As CommandBarControl
    Dim btn As CommandBarControl
    Dim i As Integer


    'add a 'popup' control to the cell commandbar (menu)
    Set ctrl = Application.CommandBars("Cell").Controls.Add _
    (Type:=msoControlPopup, Before:=1)
    ctrl.Caption = "Highlight"
    For i = 50 To 50 'add a few menu items
    'add the submenus
    Set btn = ctrl.Controls.Add
    btn.Caption = "Highlight Yellow" 'give them a name
    btn.OnAction = "HighlightYellow" 'the routine called by the control



    Next

    End Sub

    Public Sub DeleteCustomMenu2()

    Dim ctrl As CommandBarControl

    'go thru all the cell commandbar controls and delete our menu item
    For Each ctrl In Application.CommandBars("Cell").Controls
    If ctrl.Caption = "Highlight" Then ctrl.Delete

    Next

    End Sub
    Sub HighlightYellow(ByVal Target As Value)

    Dim RngRow As Range
    Dim RngFinal As Range
    Dim Row As Long
    Dim Target As Value

    'Cells.Interior.ColorIndex = xlNone

    Row = Target.Row


    Set RngRow = Range("A" & Row, Target)
    Set RngFinal = RngRow

    RngFinal.Interior.ColorIndex = 6

    End Sub

    still getting that same error :/
    thanks (:

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Macro to Highlight Row

    Sub HighlightYellow(ByVal Target As Value)
    as i noted above, as this procedure requires a parameter (target) passed to it, it can not be run from a button click, which can not pass a parameter

    what is target in this instance?
    are you trying to set the row containing the activecell to highlight if so try like
    vb Code:
    1. Sub HighlightYellow()
    2. activecell.entirerow.interior.colorindex = 6
    3. end sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    27

    Re: Excel Macro to Highlight Row

    haha talk about going about things the hard way. Thank you soo much that is perfect. A quick question is it possible to only highlight the row to column D and not all the way along?

    once again thanks heaps and thanks for putting up with my ignorance.

    andrew

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel Macro to Highlight Row

    A quick question is it possible to only highlight the row to column D and not all the way along?
    vb Code:
    1. range(cells(activecell.row, 1), cells(activecell.row, 4)).interior.colorindex = 6
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width