【发布时间】:2017-02-17 22:28:31
【问题描述】:
我正在努力寻找解决问题的方法:
我有一个工作簿上的项目列表和一个为不同电子表格上的每个项目创建工作表的宏。
A 列中的每个代码都将产品类型作为第一个字母,每个产品类型都有自己的工作簿。
所有代码都可以正常工作,除了超链接。
我需要在创建工作表时将每个代码超链接到工作表。
运行时,它会将我的单元格超链接到“C:\Users\Reception\Documents\Shared\Item Master Data\Stock\”,而不是打开我的工作表。
我错过了什么?我的完整代码如下。
Sub StockSheets()
Sheets("Component List").Select
Range("A2").Select 'Start with first item code'
Do Until ActiveCell = " "
GoTo Openwb 'check if wbStock is already open'
NewType: 'if wbStock is not open'
Dim StType As String, wbStock As Workbook, wsTEMP As Worksheet
If Left(ActiveCell, 1) = "B" Then
StType = "Bulk Stock.xlsx"
Else
If Left(ActiveCell, 1) = "F" Then
StType = "Finished Goods Stock.xlsx"
Else
If Left(ActiveCell, 1) = "P" Then
StType = "Packaging Stock.xlsx"
Else
If Left(ActiveCell, 1) = "R" Then
StType = "Raw Mat Stock.xlsx"
End If
End If
End If
End If
Set wbStock = Workbooks.Open("C:\Users\Reception\Documents\Shared\Item Master Data\Stock\" & StType)
Resume Cont1 'skip Openwb part'
Openwb:
On Error GoTo NewType 'Open wbStock'
wbStock.Activate
Cont1:
Set wsTEMP = Sheets("Stock Template")
wsTEMP.Copy After:=Sheets(Sheets.Count) 'Copies the Stock template to a new sheet'
Sheets(Sheets.Count).Activate
Application.Workbooks("Item Master Data.xlsm").Activate
Worksheets("Component List").Select
On Error GoTo Exist 'if Sheetname exists'
wbStock.Worksheets("Stock Template (2)").Name = ActiveCell.Value 'Name the new sheet as per the active cell on Component List'
wbStock.Activate
Range("A1:B1").Copy
Range("A1:B1").PasteSpecial Paste:=xlPasteValues 'Paste the formulas as values to speed up computer'
Range("A:J").Select
Columns.AutoFit 'neaten the sheet'
ThisWorkbook.Activate 'Go back to Item Master Data workbook with Component list'
Dim FPath As String
FPath = "C:\Users\Reception\Documents\Shared\Item Master Data\Stock\" & StType
Sheets("Component List").Hyperlinks.Add Anchor:=Excel.Selection, _
Address:="C:\Users\Reception\Documents\Shared\Item Master Data\Stock\" & StType & "#" & ActiveCell.Value & "!A1" 'Hyperlink item code to newly created sheet on wbStock'
Cont2:
If Left(ActiveCell.Offset(1, 0), 1) = Left(ActiveCell, 1) Then
Resume Cont3 'Check if next stType is the same as the Active Cell'
Else
wbStock.Close True 'Save and close wbStock'
End If
Cont3:
ActiveCell.Offset(1, 0).Select 'Select next item'
Loop
Exist: 'If the sheet already exists'
Sheets("Componet List").Hyperlinks.Add Anchor:=Selection, _
Address:=wbStock.Worksheets(ActiveCell).Range("A1")
Application.DisplayAlerts = False
Worksheets("Stock Template (2)").Delete
Application.DisplayAlerts = True 'Delete the newly created sheet before looping with the next item'
Resume Cont2
ActiveSheet.Cells.Font.Size = 10 'Neaten Sheet'
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Borders
.LineStyle = xlContinuous
.Color = 0
.Weight = xlThin
End With
With Columns("A:ZZ").AutoFit
Range("A1").Select
End With
End Sub
【问题讨论】:
-
到底是什么问题?您正在向单元格添加超链接。如果您打算打开工作簿,请执行 workbooks.open(fpath)。 编辑:如果您的意图是自定义超链接范围内的每个单元格,然后发布我假设您正在使用的整个循环。
-
如果它只链接到 C:\Users\Reception\Documents\Shared\Item Master Data\Stock\ 那么听起来像设置变量 StType 的标准没有得到满足,所以变量是空的“”。您的代码正在检查 ActiveCell,因此您需要确保在运行宏时选择的单元格包含您用于设置超链接的代码。