2017-07-19 99 views
0

我已經爲公司創建了用戶表單來輸入首選產品,其中包括產品網站。目前,當用戶點擊提交時,所有信息都輸入正確,但包含網站的單元格不是超鏈接到網站。如果我雙擊單元格(就像添加文本一樣),然後退出單元格,它會將鏈接變成超鏈接。通過用戶表單提交的數據不會變爲活動超鏈接

需要什麼樣的VBA代碼才能將數據輸入到文本框中,並通過用戶表單提交到工作表單元格中的超鏈接上?

這裏是我有的代碼的基本版本(刪除了其他部分);

Private Sub ComboBoxDivision_Change() 

Me.ComboBoxSpecsNumber = "" 
Me.ComboBoxSpecsName = "" 
Select Case Me.ComboBoxDivision 
    Case "DIVISION 02 - EXISTING CONDITIONS" 
    Me.ComboBoxSpecsNumber.RowSource = "D02_Number" 
    Me.ComboBoxSpecsName.RowSource = "D02_Name" 

    Case "DIVISION 03 - CONCRETE" 
    Me.ComboBoxSpecsNumber.RowSource = "D03_Number" 
    Me.ComboBoxSpecsName.RowSource = "D03_Name" 

    Case "DIVISION 04 - MASONRY" 
    Me.ComboBoxSpecsNumber.RowSource = "D04_Number" 
    Me.ComboBoxSpecsName.RowSource = "D04_Name"  
End Select 
End Sub 

Private Sub ComboBoxSpecsNumber_Change() 
Application.EnableEvents = False 
    With ComboBoxSpecsNumber 
    ComboBoxSpecsName.ListIndex = .ListIndex 
    End With 
    Application.EnableEvents = True 
End Sub 

Private Sub ComboBoxSpecsName_Change() 
Application.EnableEvents = False 
    With ComboBoxSpecsName 
    ComboBoxSpecsNumber.ListIndex = .ListIndex 
    End With 
    Application.EnableEvents = True 
End Sub 

Private Sub SubmitButton_Click() 
If Me.ComboBoxDivision.Value = "" Then 
MsgBox "Please select a Division.", vbExclamation, "Product_Information_Form" 
Me.ComboBoxDivision.SetFocus 
Exit Sub 
End If 
If Me.ComboBoxSpecsNumber.Value = "" Then 
MsgBox "Please select a Specs Number or Name.", vbExclamation, "Product_Information_Form" 
Me.ComboBoxSpecsNumber.SetFocus 
Exit Sub 
End If 
If Me.ComboBoxSpecsName.Value = "" Then 
MsgBox "Please select a Specs Name or Name.", vbExclamation, "Product_Information_Form" 
Me.ComboBoxSpecsName.SetFocus 
Exit Sub 
End If 

Dim RowCount As Long 
RowCount = Worksheets("FormData").Range("A1").CurrentRegion.Rows.Count 
With Worksheets("FormData").Range("A1") 
.Offset(RowCount, 0).Value = Me.ComboBoxDivision.Value 
.Offset(RowCount, 1).Value = Me.ComboBoxSpecsNumber.Value 
.Offset(RowCount, 2).Value = Me.ComboBoxSpecsName.Value 
.Offset(RowCount, 3).Value = Me.TextBox_Website_Link.Value 
.Offset(RowCount, 4).Value = Format(Now, "yyyy.mm.dd hh:mm:ss") 
End With 

Select Case Me.ComboBoxDivision 
Case "DIVISION 02 - EXISTING CONDITIONS" 
Dim LastRow As Long, ws As Worksheet 
Set ws = Sheets("Div-02") 
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 
ws.Range("a" & LastRow).Value = Me.ComboBoxSpecsNumber.Value 
ws.Range("b" & LastRow).Value = Me.ComboBoxSpecsName.Value 
ws.Range("c" & LastRow).Value = Me.TextBox_Website_Link.Value 

Case "DIVISION 03 - CONCRETE" 
Set ws = Sheets("Div-03") 
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 
ws.Range("a" & LastRow).Value = Me.ComboBoxSpecsNumber.Value 
ws.Range("b" & LastRow).Value = Me.ComboBoxSpecsName.Value 
ws.Range("c" & LastRow).Value = Me.TextBox_Website_Link.Value 

Case "DIVISION 04 - MASONRY" 
Set ws = Sheets("Div-04") 
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 
ws.Range("a" & LastRow).Value = Me.ComboBoxSpecsNumber.Value 
ws.Range("b" & LastRow).Value = Me.ComboBoxSpecsName.Value 
ws.Range("c" & LastRow).Value = Me.TextBox_Website_Link.Value 


End Select 

Unload Product_Information_Form 
Start_Form.Show 

End Sub 

P.S.我試過多次搜索解決方案,但總是得到關於如何向用戶表單添加超鏈接的信息,這不是我所需要的。

回答

0

編輯:更新我的回答,因爲我注意到你有多個地方的鏈接可能會被寫出來......

首先添加此實用程序子:

Sub AddLink(c As Range, txt As String) 
    If Len(txt) > 0 Then 
     c.Formula = "=HYPERLINK(""" & txt & """)" 
    Else 
     c.Value = "" 
    End If 
End Sub 

那麼無論你可能會寫一個鏈接你可以使用該子:

eg變線:

.Offset(RowCount, 3).Value = Me.TextBox_Website_Link.Value 

ws.Range("c" & LastRow).Value = Me.TextBox_Website_Link.Value 

AddLink .Offset(RowCount, 3), Me.TextBox_Website_Link.Value 

AddLink ws.Range("c" & LastRow), Me.TextBox_Website_Link.Value 

分別

+0

謝謝你的反應速度快,但你所提供的代碼,目前導致一個編譯錯誤:無效的或不合格的參考。調試器突出顯示您提供的第一個.Offset代碼。我在Dim RowCount As Long之後插入代碼。是否有一個特定區域,我應該插入代碼? – user8333623

+0

更改了我的答案,使其更具可重用性...... –

+0

再次感謝您的快速響應,但發生了不同的錯誤;運行時錯誤「424」對象需要。調試器突出顯示了AddLink。偏移量(RowCount,3).Value,Me.TextBox_Website_Link.Value部分代碼。我在代碼的Private Sub SubmitButton_Click()部分上方添加了您的代碼。我應該注意到,我對VBA很陌生,在過去的幾周裏只是編寫它。 – user8333623