【发布时间】:2020-08-16 13:49:48
【问题描述】:
我在 excel VBA 上开发了一个小型票务系统 Save 按钮将从文本框和单选按钮中获取所有数据并将其添加到第 7 行(在这种情况下) 但是当我按下上传时,我无法将链接添加到附件标签
如何使用(上传文件)按钮将链接添加到附件标签 并保存上传文件按钮中存储的链接值,以便稍后在清除按钮和保存按钮中使用?
我对使用私有 sub 和私有 dim 变量感到困惑。 我是VBA新手,请帮忙
上传按钮
Public Sub btnAttachment_Click()
'To upload file link format is png, jpeg, PDF or All files'
Dim wks As Worksheet
Dim LinksList As Range
Dim lastRowLink As Long
Dim LinkAttached As Long
Set wks = ActiveSheet
Set LinksList = Range("N1")
'declare last row to insert link to
lastRowLink = WorksheetFunction.CountA(Sheets("Tickets").Range("A:A"))
Sheets("Tickets").Cells(lastRow + 1, 11).Value = LinkAttached
ChDrive "C:\"
ChDir "C:\"
Filt = "PNG Files(*.png),*.png ," & _
"Jpeg Files(*.jpeg),*.jpg ," & _
"PDF Files (*.pdf),*.pdf ," & _
"All Files (*.*),*.*"
FilterIndex = 1
Title = "Select a File to Hyperlink"
Filename = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)
If Filename <> False Then
wks.Hyperlinks.Add Anchor:=LinksList, _
Address:=Filename, _
TextToDisplay:=Filename
Else
MsgBox "No file was selected.", vbCritical, "Loading Error"
Exit Sub
End If
End Sub
保存按钮代码
Public Sub BtnSave_Click()
Dim Ws As Worksheet
Dim lastRow As Long
Dim openOn As Date
'Declare openBy to date Now function'
openOn = Now()
'set format function on time var'
openTimeAmPM = Format(openOn, "m.d.yy h:mm AM/PM")
'if no all information required inserted, show error'
If SieraForum.txtTicketName.Value = "" Or (SieraForum.ErrorOption = False And SieraForum.OrderOption = False) Or SieraForum.CombLocation.Value = "" Or SieraForum.CombOpenBy.Value = "" Or SieraForum.CombTicketStatus.Value = "" Then
MsgBox " Please fill All information required (Ticket Name, Severity, Location, OpenBy)"
Else
lastRow = WorksheetFunction.CountA(Sheets("Tickets").Range("A:A"))
Sheets("Tickets").Cells(lastRow + 1, 1).Value = lastRow
Sheets("Tickets").Cells(lastRow + 1, 2).Value = SieraForum.txtTicketName.Value
If SieraForum.ErrorOption = True Then
Sheets("Tickets").Cells(lastRow + 1, 3).Value = "Error"
Else
Sheets("Tickets").Cells(lastRow + 1, 3).Value = "Order"
End If
Sheets("Tickets").Cells(lastRow + 1, 4).Value = SieraForum.CombSeverity.Value
Sheets("Tickets").Cells(lastRow + 1, 5).Value = SieraForum.CombLocation.Value
Sheets("Tickets").Cells(lastRow + 1, 6).Value = SieraForum.txtTicketDetails.Value
Sheets("Tickets").Cells(lastRow + 1, 7).Value = SieraForum.CombOpenBy.Value
Sheets("Tickets").Cells(lastRow + 1, 8).Value = SieraForum.CombCloseBy.Value
Sheets("Tickets").Cells(lastRow + 1, 9).Value = SieraForum.CombTicketStatus.Value
Sheets("Tickets").Cells(lastRow + 1, 10).Value = openTimeAmPM
'Sheets("Tickets").Cells(lastRow + 1, 13).Value =
'Display new ticket number on the label'
lbTicketNumVal.Caption = lastRow
End If
'to clear data after pressing Save button'
'Clear the data from the form'
SieraForum.txtTicketName.Value = ""
SieraForum.ErrorOption = False
SieraForum.OrderOption = False
SieraForum.CombSeverity = ""
SieraForum.CombLocation = ""
SieraForum.txtTicketDetails = ""
SieraForum.CombOpenBy = ""
SieraForum.CombCloseBy = ""
SieraForum.CombTicketStatus = ""
lbTicketNumVal.Caption = lastRow + 1
'to save excel sheet editing'
ActiveWorkbook.Save
End Sub
【问题讨论】:
-
您要更新表单上的标签文本吗?
-
不,每次用户添加票证时,我都有票证,它将提交到新行中,上传密钥应在单元格 M 的同一行票证中添加附件的链接,并且每次将链接添加到票号的同一行
-
基本上,就链接值而言,我需要将保存按钮的相同子项放在上传按钮上
标签: excel vba desktop-application