VBA Create buttons and call different program based on the value of their left cell

Trevian3969

New Member
Joined
Jul 2, 2009
Messages
3
Hi,
I have 3 columns in excel, Server, Type of connection, and IP Address

The type of connection could be WEB or RD (Remote Desktop)

I need to create a macro that could put a button in the fourth column and based on the type of connection the button will call a web browser or mstsc.exe (remote desktop connection).
It would be great if the button could be created as soon as I open the excel worksheet.

I have this code for create a button, but dont know how to pass parameters to other function
I think that the logic is fine, but couldnt test it becuase is not working
:eeek:

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub Create_button()
Dim btn As Button
Application
.ScreenUpdating = False
ActiveSheet
.Buttons.Delete
Dim t As Range
For i = 2 To 6 Step 1
Set t = ActiveSheet.Range(Cells(i, 4), Cells(i, 4))
Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
' .OnAction = "conect" (Im stuck Here, How to pass the Type of connection and IP values to the conect sub()
' .Caption = "Conect "
.Name = "Button" & i
End With
Next i
Application
.ScreenUpdating = True
End Sub



<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub Conect(Type_connection, IP as string)
Dim retVal As Variant

Select Case Type_Connection

Case "RD"
prg
= "c:\windows\system32\mstsc.exe /v:" & IP
Case "WEB"
prg
= "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" & IP
Case Else
GoTo 999
End Select
retVal
= Shell(prg, 1)
999

End Sub</code>
</code></pre>
Thanks in advance





 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top