所以,这基本上就是我要做的。我在一个由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
我认为我正在将方法与功能混合在一起,但我不确定将它放在哪里。我基本上想要将其添加为函数,以便更容易插入到列中。
答案 0 :(得分:7)
我看到有人为你提供了解决这个问题的方法,但是我会给你你想要的方法(以防万一)。 FYI当引用OLE对象时,智能感知在VBA中很糟糕(即,某些方法可能看起来不属于按钮对象,但它们确实属于这些方法)。
下面的脚本会自动为您创建按钮,并将用户发送到您单击时指定的站点。 **我附上了解释每行的内容的注释。
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
享受!
答案 1 :(得分:3)
您可以在没有VBA的情况下执行此操作。你可以使用公式。
=Hyperlink("http://www.someplace.com/employee.php?ID="&A1,A1)
A1
将拥有员工ID。
查看我关于从外部数据创建超链接的帖子:
http://www.spreadsheetsmadeeasy.com/creating-hyperlinks-with-external-data/
向下滚动到“添加超链接”部分以获取更多信息。