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

所以,这基本上是我想要做的。 我从MSSQL生成的文件中有一列雇员#。 我想创build一个函数在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 

我想我正在将函数与函数混合在一起,但我不确定该怎么去做。 我基本上想添加它作为一个function,使其更容易插入列。

你可以做到这一点没有VBA。 你可以使用一个公式。

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

A1将有员工ID。

看看这篇文章,我做了关于从外部数据创build超链接:

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

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

我看到有人向你提供了解决方法,但是我会给你所要求的方法(以防万一)。 FYI intellisense在引用OLE对象时吸引VBA(也就是说,某些方法可能不属于button对象,但它们的确如此)。

下面的脚本将自动为您创buildbutton,并将用户发送到您单击指定的站点。 **我包括解释每一行是什么的笔记。

这会在列B中创buildbutton,并从列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 

这是用户单击button时执行的macros:

 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 

请享用!

这对我来说是有效的:

 ActiveWorkbook.FollowHyperlink Address:=URL 

但是我必须拿www。 在它工作之前closuresURL。 如果我没有,那么我会得到一个服务器没有发现exception。 所以,而不是:

 "http://www.myaddress.com" 

我用了:

 "http://myaddress.com"