我正在开发一个存档系统,其中用户使用数据验证列出有关机器的信息。这些信息都被传输到一个表中。表格如下所示:
|单位|机器|电脑|软件|谁 |为什么|
为了让程序正常运行,用户至少必须插入一台机器。如果用户未在 PC、软件、人员或原因字段中插入信息,我希望程序禁止用户记录信息。但我还希望有一个专门的 MsgBox 来告诉用户他们缺少什么。
我对此有两种不同的想法。我要么有一个程序遍历每个单元格,然后在它自己的 MsgBox 中单独命名它。
Sub InputCheck()
Dim wbk As Workbook
Dim shtForm As Worksheet
Set wbk = ThisWorkbook
Set shtForm = wbk.Worksheets("Interface")
Dim MachFinder As Range
Dim itm, tbl
Dim listCols As ListColumns
Dim unit_input As String
Dim machine_input As String
Dim pc_inp As String
Dim software_inp As String
Dim who_inp As String
Dim why_inp As String
unit_input = shtForm.Range("A2").Value
machine_input = shtForm.Range("B2").Value
pc_inp = shtForm.Range("C2").Value
software_inp = shtForm.Range("D2").Value
who_inp = shtForm.Range("E2").Value
why_inp = shtForm.Range("F2").Value
Set tbl = randomTable
Set MachFinder = tbl.ListColumns("Machine").DataBodyRange.Find(machine_input, lookat:=xlWhole) 'Find machine name within table
If Not MachFinder Is Nothing Then
'Check to see if a PC and software has been inputted
If IsEmpty(pc_inp) Then
pcRes = MsgBox("You did not insert the PC for this device." & vbNewLine & vbNewLine & "If you ignore this warning, the machine will no longer be associated with any information, and it will be still be logged. Do you wish to continue?", vbYesNo + vbExclamation + vbDefaultButton2, "Archive System")
If pcRes = vbNo Then
Exit Sub
End If
End If
If IsEmpty(software_inp) Then
swRes = MsgBox("You did not insert the software associated with this device." & vbNewLine & vbNewLine & "If you ignore this warning, the machine will no longer be associated with any information, and it will be still be logged. Do you wish to continue?", vbYesNo + vbExclamation + vbDefaultButton2, "Archive System")
If swRes = vbNo Then
Exit Sub
End If
End If
'Check to see if user has inserted "Who" and "Why"
If IsEmpty(who_inp) Then
whoRes = MsgBox("You must put WHO worked on the device. Please put all information needed and try again.", vbOKOnly + vbExclamation, "Archive System")
Exit Sub
End If
If IsEmpty(why_inp) Then
whyRes = MsgBox("There must be a reason for WHY there was work done on the machine. Please put all information needed and try again.", vbOKOnly + vbExclamation, "Archive System")
Exit Sub
End If
我的另一个想法是,我可以将它们全部合并为一个范围,也许可以让程序检查范围,找到空单元格,并将其打印在 MsgBox 中。但我需要以某种方式命名每个单元格,而且我不知道最好的方法。
Dim inputs As Range
Dim inpite
Set inputs = shtForm.Range("C2:F2")
For Each inpite In inputs
If IsEmpty(inpite.Value) Or inpite.Value = vbNullString Then
' I thought I properly named all of the cells within native Excel. It should reference the name of the cell. For instance, C2 is named "ComputerName"
MsgBox "You are missing an entry for" & inputs.Names.Name & vbNewLine & "Please insert all correct information.", vbOKOnly, "Archive System"
Exit Sub
End If
Next
'blah blah blah, rest of program...
End If
每当我尝试运行它时,我要么遇到对象分配问题,要么只是一般来说不接受程序的“IsEmpty”部分。我认为第二个程序肯定可以工作,我只是错过了一些我感觉的东西?我想让它尽可能干净。谢谢您的帮助!
主要
Sub YourProcedure()
If Not IsFormInputValid Then Exit Sub
MsgBox "Form input is valid. Continuing...", vbInformation
End Sub
帮助
Function IsFormInputValid() As Boolean
Const PROC_TITLE As String = "Form Input Validation"
Dim InputNames(): InputNames = VBA.Array("PC", "Software", "WHO", "WHY")
Dim irg As Range:
Set irg = ThisWorkbook.Worksheets("Interface").Range("C2:F2")
Dim cell As Range, c As Long, n As Long, MsgString As String
For Each cell In irg.Cells
If Len(CStr(cell.Value)) = 0 Then
n = n + 1
MsgString = MsgString & vbLf & n & ".) " & InputNames(c) _
& " in cell " & cell.Address(0, 0) & ","
End If
c = c + 1
Next cell
If n = 0 Then
IsFormInputValid = True
Else
MsgString = "You are missing the following entr" _
& IIf(n = 1, "y", "ies") & ": " & vbLf _
& Left(MsgString, Len(MsgString) - 1) & "." & vbLf & vbLf _
& "Please enter the required information."
MsgBox MsgString, vbCritical, PROC_TITLE
End If
End Function
使用
Len(Trim(inputs.Cells(i)))
检查空白单元格,然后将列标题连接为弹出消息。
Sub demo()
Dim inputs As Range
Dim inpite, sMissing As String
Set shtForm = ActiveSheet ' update sheet object as needed
Set inputs = shtForm.Range("A2:F2")
For i = 2 To 6
If Len(Trim(inputs.Cells(i))) = 0 Then
sMissing = sMissing & "," & _
inputs.Offset(-1, 0).Cells(i).Value
End If
Next
MsgBox "You are missing entries: " & Mid(sMissing, 2) & _
vbNewLine & "Please insert all correct information.", _
vbOKOnly, "Archive System"
Exit Sub
' your code
End Sub