Assign code to button

N

Nic

H

I found the following code whilst surfing the net and thought it would be very useful to me as i am currently compiling a list of some 24 columns in Excel. It does do exactly what it is meant to do i.e. upon right clicking within a row it shows me the contents of all columns within that row, which is great. However, there are times when i really could do with being able to turn this function off so that i can use the shortcut menus

Could anybody tell me a step by step way of assigning this code to a button so that i can turn this feature on and off as and when i require it? Just so you know, i am totally unfamiliar with VBA so please be gentle with me

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean
Dim intCounter As Intege
Dim txt As Strin
If Target.Column > 24 Then Exit Su
Cancel = Tru
For intCounter = 1 To 2
txt = txt & Cells(Target.Row, intCounter) & vbL
Next intCounte
MsgBox tx
End Su

Many thanks
 
H

Harald Staff

Hi

Insert a button from the Control toolbox and make its code (rightclick it > view code)
look like this:

Private Sub CommandButton1_Click()
Dim intCounter As Integer
Dim txt As String
If ActiveCell.Column > 24 Then Exit Sub
For intCounter = 1 To 24
txt = txt & Cells(ActiveCell.Row, intCounter) & vbLf
Next intCounter
MsgBox txt
End Sub

--
HTH. Best wishes Harald
Followup to newsgroup only please.

Nic said:
Hi

I found the following code whilst surfing the net and thought it would be very useful to
me as i am currently compiling a list of some 24 columns in Excel. It does do exactly
what it is meant to do i.e. upon right clicking within a row it shows me the contents of
all columns within that row, which is great. However, there are times when i really could
do with being able to turn this function off so that i can use the shortcut menus.
Could anybody tell me a step by step way of assigning this code to a button so that i
can turn this feature on and off as and when i require it? Just so you know, i am totally
unfamiliar with VBA so please be gentle with me.
 
N

Nic

Hi Harol

Thank you so much for that, it has worked a treat. You would not believe the hours i have spent trying to work out how to do that!!!

Many, Many thanks again

Nic
 
H

Harald Staff

Glad it worked Nic. Thank you for the feedback.

Best wishes Harald
Followup to newsgroup only please

Nic said:
Hi Harold

Thank you so much for that, it has worked a treat. You would not believe
the hours i have spent trying to work out how to do that!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top