我有以下代码。我的用户表单上的 listquotes 中选择的数据来自表格上的一列数据,我需要我选择的这个值用作参考(quotenumber)以将数据从同一行传输到另一张表。
我不断收到消息“请从列表报价组合框中选择报价#。”即使选择了报价编号。
Sub TransferDataBasedOnQuote()
Dim wsTaskCodes As Worksheet
Dim wsInvoiceForm As Worksheet
Dim quoteNumber As String
Dim quoteRow As Range
Dim projectNameCell As Range
Dim pridCell As Range
Dim billCodeCell As Range
' Set references to the worksheets
Set wsTaskCodes = ThisWorkbook.Sheets("Task Codes")
Set wsInvoiceForm = ThisWorkbook.Sheets("Invoice Log and Form")
' Show the FormQuotes UserForm to select the quote number
FormQuotes.Show
' Get the selected quote number from the form's Tag property after closing
quoteNumber = FormQuotes.ListQuotes.Tag
' Ensure that a valid quote number was selected
If quoteNumber = "" Then
MsgBox "Please select a Quote # from the ListQuotes ComboBox.", vbExclamation
Exit Sub
End If
' Find the Quote # in column A of Task Codes sheet
Set quoteRow = wsTaskCodes.Columns("A").Find(quoteNumber, LookIn:=xlValues, LookAt:=xlWhole)
' If Quote # is not found, show a message and exit
If quoteRow Is Nothing Then
MsgBox "Quote # not found. Please try again.", vbExclamation
Exit Sub
End If
' Find "Project Name:" and transfer data
Set projectNameCell = wsInvoiceForm.Cells.Find(What:="Project Name:", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not projectNameCell Is Nothing Then
projectNameCell.Offset(0, 1).Value = quoteRow.Offset(0, 1).Value ' Assuming "Project Name" is in the second column (B)
Else
MsgBox "'Project Name:' not found in Invoice Form", vbExclamation
Exit Sub
End If
' Find "Project ID:" and transfer data
Set pridCell = wsInvoiceForm.Cells.Find(What:="Project ID:", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not pridCell Is Nothing Then
pridCell.Offset(0, 1).Value = quoteRow.Offset(0, 8).Value ' Assuming "Project ID" is in the 9th column (I)
Else
MsgBox "'Project ID:' not found in Invoice Form", vbExclamation
Exit Sub
End If
' Find "Billing Code" and transfer data
Set billCodeCell = wsInvoiceForm.Cells.Find(What:="Bill to Task Code:", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not billCodeCell Is Nothing Then
billCodeCell.Offset(0, 1).Value = quoteRow.Offset(0, 9).Value ' Assuming "Billing Code" is in the 10th column (J)
Else
MsgBox "'Billing Code:' not found in Invoice Form", vbExclamation
Exit Sub
End If
' Add more transfers as needed based on your structure
' Notify the user of the successful transfer
MsgBox "Data successfully transferred for Quote # " & quoteNumber, vbInformation
End Sub
FormQuotes.ListQuotes.Tag
只能检索预定义的Tag(在用户表单设计模式下设置)。一旦用户表单被卸载,通过代码对其所做的任何更改都将丢失。大多数情况下,ListQuotes.Value
用于获取用户在ListBox中选择的项目。
我建议使用标准模块中声明的公共变量来存储用户选择。在
Change
事件期间从列表框中检索所选项目。
' Standard Module
Public pQuote As String
Sub TransferDataBasedOnQuote()
'.. your code
' Show the FormQuotes UserForm to select the quote number
FormQuotes.Show
' Get the selected quote number from the form's Tag property after closing
quoteNumber = pQuote
' Ensure that a valid quote number was selected
If quoteNumber = "" Then
MsgBox "Please select a Quote # from the ListQuotes ComboBox.", vbExclamation
Exit Sub
End If
' .. your code
End Sub
' FormQuotes Module
Private Sub FormQuotes_Change()
pQuote = Me.FormQuotes.Value
End Sub