|
-
Nov 22nd, 2011, 05:04 AM
#1
Thread Starter
Junior Member
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.
-
Nov 22nd, 2011, 06:08 AM
#2
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
-
Nov 22nd, 2011, 06:33 AM
#3
Thread Starter
Junior Member
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
-
Nov 22nd, 2011, 03:09 PM
#4
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
-
Nov 22nd, 2011, 04:26 PM
#5
Thread Starter
Junior Member
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 (:
-
Nov 23rd, 2011, 03:13 AM
#6
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:
Sub HighlightYellow() activecell.entirerow.interior.colorindex = 6 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
-
Nov 23rd, 2011, 04:23 AM
#7
Thread Starter
Junior Member
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
-
Nov 23rd, 2011, 04:29 AM
#8
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|