Excel 如何创建具有下拉值并单击按钮的VB框
我有以下代码来自动登录网站:Excel 如何创建具有下拉值并单击按钮的VB框,excel,vba,Excel,Vba,我有以下代码来自动登录网站: 和工作手册如下: 现在 我想在vb中创建一个宏。当宏按以下窗口运行时: 应打开一个小窗口,该窗口具有“网站”下拉菜单,其中包含“网站”列中的所有值 当用户选择特定网站时:该url、用户名、密码应在marco中使用 听起来你想创建一个用户表单。为此,请从VBA窗口中选择“插入和用户表单”。从那里,添加一个组合框和一个命令按钮。您可以使用此代码在组合框中填充选项: Private Sub UserForm_Initialize() 'We want the box
和工作手册如下:
现在 我想在vb中创建一个宏。当宏按以下窗口运行时:
听起来你想创建一个用户表单。为此,请从VBA窗口中选择“插入和用户表单”。从那里,添加一个组合框和一个命令按钮。您可以使用此代码在组合框中填充选项:
Private Sub UserForm_Initialize() 'We want the box to fill as soon as the form appears
Dim sites As Range
For Each sites In Range("B2:B3") 'Change this to suit your actual range
Me.ComboBox1.AddItem sites.Value
Next sites
End Sub
对于执行代码的命令按钮,请使用以下命令:
Private Sub CommandButton1_Click()
Dim siteName As String
Dim URL As String
Dim user As String
Dim password As String
Dim site As Range
Dim IntExpl As Object
Set IntExpl = CreateObject("InternetExplorer.Application")
Dim dd As Object
siteName = UserForm1.ComboBox1.Value
Set site = Range("B2:B3").Find(siteName, ,xlValues,xlWhole)
'These variables will let you log in using arbitrary credentials instead of ones called explicitly
URL = site.Offset(0,1)
user = site.Offset(0,2)
password = site.Offset(0,3)
With IntExpl
.navigate URL
.Visible = True
Do Until IntExpl.ReadyState = 4
Loop
Set dd = .Document.getElementById("LoginUsername")
dd.Value = user
dd.Click
Set dd = .Document.getElementById("LoginPassword")
dd1.Value = password
dd1.Click
Set dd = .Document.getElementById("loginBtn")
dd.Click
End With
End Sub
我希望这有帮助。干杯 你研究过UserForms的概念吗?我现在在手机上,所以我现在无法测试它,但是看一看,我认为如果你将Me.ComboBox1.Value改为UserForm1.ComboBox1.Value(或者你给你的用户表单起的任何名字),它应该可以工作。今天晚些时候,我将在excel中试用它,并在确认它正常工作后发布一个编辑。是的,在做了更改后,我能够让它正常工作。我已经编辑了答案以显示工作代码。
Private Sub UserForm_Initialize() 'We want the box to fill as soon as the form appears
Dim sites As Range
For Each sites In Range("B2:B3") 'Change this to suit your actual range
Me.ComboBox1.AddItem sites.Value
Next sites
End Sub
Private Sub CommandButton1_Click()
Dim siteName As String
Dim URL As String
Dim user As String
Dim password As String
Dim site As Range
Dim IntExpl As Object
Set IntExpl = CreateObject("InternetExplorer.Application")
Dim dd As Object
siteName = UserForm1.ComboBox1.Value
Set site = Range("B2:B3").Find(siteName, ,xlValues,xlWhole)
'These variables will let you log in using arbitrary credentials instead of ones called explicitly
URL = site.Offset(0,1)
user = site.Offset(0,2)
password = site.Offset(0,3)
With IntExpl
.navigate URL
.Visible = True
Do Until IntExpl.ReadyState = 4
Loop
Set dd = .Document.getElementById("LoginUsername")
dd.Value = user
dd.Click
Set dd = .Document.getElementById("LoginPassword")
dd1.Value = password
dd1.Click
Set dd = .Document.getElementById("loginBtn")
dd.Click
End With
End Sub