繁体   English   中英

使用ActiveWorkbook.FollowHyperlink在Excel中打开网站

[英]Open Website in excel using ActiveWorkbook.FollowHyperlink

所以,这基本上就是我想要做的。 我在一个由MSSQL生成的文件中有一列员工#。 我想在URL所在的单元格中创建一个函数, http://www.someplace.com/employee.php?ID = Employee#FromCell

到目前为止,我发现的所有示例都不够详细,无法弄清楚如何处理它。 我知道这不正确,但这是我到目前为止所得到的

Function openurl(strSKU As String)
    ActiveWorkbook.FollowHyperlink Address:="http://www.someplace.com/employee.php?ID=?strSKU=" & strSKU, NewWindow:=True
End Function

我想我正在将方法与功能混合在一起,但我不知道该把它放在哪里。 我基本上想要将其添加为函数,以便更容易插入到列中。

我看到有人为你提供了解决这个问题的方法,但是我会给你你想要的方法(以防万一)。 FYI当引用OLE对象时,智能感知在VBA中很糟糕(即,某些方法可能看起来不属于按钮对象,但它们确实如此)。

下面的脚本将自动为您创建按钮,并将用户发送到您单击时指定的站点。 **我附上了解释每行的内容的注释。

这将在B列中创建按钮,并从A列获取URL参数:

Sub CreateButtons()


Dim btn As Button 'Create a variable for our button
Application.ScreenUpdating = False 'Speed up the process by disabling ScreenUpdating
ActiveSheet.Buttons.Delete 'Delete existing buttons.
Dim Report As Worksheet 'Create our worksheet variable.
Set Report = Excel.ActiveSheet 'Set our worksheet to the worksheet variable.
Dim t As Range 'Create a variable for the cells we will reference.

For i = 1 To Report.UsedRange.Rows.Count 'This will loop through each row in the used range of our worksheet.
    If Report.Cells(i, 1).Value <> "" Then 'If the value of the first cell is not empty, then do the following...
        Set t = Report.Range(Cells(i, 2), Cells(i, 2)) 'Assign the cell in the second column of the current row to the cell variable.
        Set btn = Report.Buttons.Add(t.Left, t.Top, t.Width, t.Height) 'Create a button and place it in the cell in the second column.

        With btn
          .OnAction = "openurl" 'Set the button to trigger the openurl sub-routine when it is clicked.
          .Caption = Report.Cells(i, 1).Value 'Set the caption of the button to equal the value of the cell in the first column.
          .Name = i 'Set the name of the button to equal the row on which it resides. This name will be used in the openurl sub; So don't change it.
        End With
   End If
Next i


End Sub

这是用户单击按钮时执行的宏:

Sub openurl()

Dim Report As Worksheet 'Create a variable for the worksheet
Set Report = Excel.ActiveSheet 'Assign the worksheet to our variable
Dim i As Integer 'Create a variable for our row number
i = Application.Caller 'Assign name of the button to our row number.

Dim address As String 'Create a variable for our address
address = "http://www.someplace.com/employee.php?ID=?strSKU=" & Report.Cells(i, 1).Value 'Assign the URL to our address variable.

ActiveWorkbook.FollowHyperlink address:=address, NewWindow:=True 'Send the user to the URL you specified (with the URL parameter at the end).

End Sub

奖金信息:

按照下一步,自动完成整个过程:

当您说从MSSQL数据库填充当前数据时,您可能意味着您正在使用另一个VBA子或函数将数据拉入Excel。 如果是这样,那么如果你在提取数据的脚本之后放置一个脚本来调用“CreateButtons()”子程序,那么整个过程将自动完成。 例:

Sub getEmployeeData() 'This represents your sub that pulls your data from MSSQL
'================================================================
'This represents your script to get your information into Excel.
'================================================================

Call CreateButtons 'This runs the CreateButtons() subroutine.

End Sub

请享用!

你可以不用VBA就可以做到这一点。 您可以使用公式。

=Hyperlink("http://www.someplace.com/employee.php?ID="&A1,A1)

A1将拥有员工ID。

看看我发表的关于从外部数据创建超链接的帖子:

http://www.spreadsheetsmadeeasy.com/creating-hyperlinks-with-external-data/

向下滚动到“添加超链接”部分以获取更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM