[英]How to do dynamic entry data validation in Excel
[一張表的例子][2]
我有一個 Excel 項目的結構,其中包含 5 個定期更新的表。 我目前正在尋找一種方法來驗證輸入的數據並為某些列設置框架和特定數據類型。 如果輸入錯誤,它應該返回一條消息。 我嘗試過使用 vba,但由於我完全沒有經驗,所以我很難實現一個工作代碼。 到目前為止,excel 中的數據檢查選項也不起作用,因為還應該可以選擇一次在表中復制和粘貼多個數據集。 一旦我關閉項目,它也會不斷重置。
繼續嘗試使用 vba 是個好主意嗎?
我的想法是循環遍歷每一列,並檢查每個條目是否有分配給該列的某些要求。 如果輸入的數據具有錯誤的數據類型或超出所選范圍,則應該有一個消息框引發錯誤。
Sub CheckColumns()
Dim rng As Range
Dim lCol As Long, lRow As Long
lCol = Range("C2").End(xlToRight).Column
lRow = Range("C2").End(xlDown).Row
For Each rng In Range("C2", Cells(lRow, lCol))
If IsNumeric(rng) = False Then
MsgBox ("A number has to be entered " & "Row " & rng.Row &
" Column " & rng.Column)
rng.Font.ColorIndex = 3
End If
Next rng
End Sub
把它放在你的工作表模塊中:
Private Sub Worksheet_Change(ByVal Target As Range)
'Declarations.
Dim RngNameColumn As Range
Dim RngLenghtColumn As Range
Dim RngWidthColumn As Range
Dim RngHeightColumn As Range
Dim RngIntersection As Range
Dim RngCell As Range
Dim StrMessage As String
Dim StrMessageReason As String
Dim BytCounter As Byte
Dim DblLenghtMax As Double
Dim DblWidthMax As Double
Dim DblHeightMax As Double
Dim DblLenghtMin As Double
Dim DblWidthMin As Double
Dim DblHeightMin As Double
'Settings.
Set RngNameColumn = Me.Range("B:B")
Set RngLenghtColumn = Me.Range("C:C")
Set RngWidthColumn = Me.Range("D:D")
Set RngHeightColumn = Me.Range("E:E")
DblLenghtMax = 5000
DblWidthMax = 243
DblHeightMax = 4354
DblLenghtMin = 2354
DblWidthMin = 24
DblHeightMin = 333
'Setting the first part of StrMessage.
StrMessage = "Invalid input:"
'___________________________
'RngNameColumn Block - Start
'---------------------------
'In this block RngNameColumn is checked for invalid input.
'Setting RngIntersection.
Set RngIntersection = Intersect(Target, RngNameColumn)
'Setting the StrMessageReason accordingly to the block need.
StrMessageReason = ": no digits allowed"
'Checking if RngIntersection is something.
If Not RngIntersection Is Nothing Then
'Covering each cell of RngIntersection.
For Each RngCell In RngIntersection
'Covering each digit.
For BytCounter = 0 To 9
'Checking if RngCell contains any digit.
If Len(RngCell.Value) <> Len(Excel.WorksheetFunction.Substitute(RngCell.Value, BytCounter, "")) And _
RngCell.Value <> "" Then
'Setting StrMessage.
StrMessage = StrMessage & vbCrLf & "-range " & RngIntersection.EntireColumn.Address(0, 0) & StrMessageReason
'When the first invalid input of the block is found, the block is left.
GoTo CP_RngNameColumnEnd
End If
Next
Next
End If
CP_RngNameColumnEnd:
'___________________________
'RngNameColumn Block - End
'---------------------------
'_____________________________
'RngLenghtColumn Block - Start
'-----------------------------
'In this block RngLenghtColumn is checked for invalid input.
'Setting RngIntersection.
Set RngIntersection = Intersect(Target, RngLenghtColumn)
'Setting the StrMessageReason accordingly to the block need.
StrMessageReason = ": has be be a number between " & DblLenghtMax & " and " & DblLenghtMin
'Checking if RngIntersection is something.
If Not RngIntersection Is Nothing Then
'Covering each cell of RngIntersection.
For Each RngCell In RngIntersection
'Checking if RngCell does not contain a number within the specified limits.
If (IsNumeric(RngCell.Value) = False Or _
RngCell.Value < DblLenghtMin Or _
RngCell.Value > DblLenghtMax _
) And _
RngCell.Value <> "" Then
'Setting StrMessage.
StrMessage = StrMessage & vbCrLf & "-range " & RngIntersection.EntireColumn.Address(0, 0) & StrMessageReason
'When the first invalid input of the block is found, the block is left.
GoTo CP_RngLenghtColumnEnd
End If
Next
End If
CP_RngLenghtColumnEnd:
'_____________________________
'RngLenghtColumn Block - End
'-----------------------------
'_____________________________
'RngWidthColumn Block - Start
'-----------------------------
'In this block RngWidthColumn is checked for invalid input.
'Setting RngIntersection.
Set RngIntersection = Intersect(Target, RngWidthColumn)
'Setting the StrMessageReason accordingly to the block need.
StrMessageReason = ": has be be a number between " & DblWidthMax & " and " & DblWidthMin
'Checking if RngIntersection is something.
If Not RngIntersection Is Nothing Then
'Covering each cell of RngIntersection.
For Each RngCell In RngIntersection
'Checking if RngCell does not contain a number within the specified limits.
If (IsNumeric(RngCell.Value) = False Or _
RngCell.Value < DblWidthMin Or _
RngCell.Value > DblWidthMax _
) And _
RngCell.Value <> "" Then
'Setting StrMessage.
StrMessage = StrMessage & vbCrLf & "-range " & RngIntersection.EntireColumn.Address(0, 0) & StrMessageReason
'When the first invalid input of the block is found, the block is left.
GoTo CP_RngWidthColumnEnd
End If
Next
End If
CP_RngWidthColumnEnd:
'_____________________________
'RngWidthColumn Block - End
'-----------------------------
'_____________________________
'RngHeightColumn Block - Start
'-----------------------------
'In this block RngHeightColumn is checked for invalid input.
'Setting RngIntersection.
Set RngIntersection = Intersect(Target, RngHeightColumn)
'Setting the StrMessageReason accordingly to the block need.
StrMessageReason = ": has be be a number between " & DblHeightMax & " and " & DblHeightMin
'Checking if RngIntersection is something.
If Not RngIntersection Is Nothing Then
'Covering each cell of RngIntersection.
For Each RngCell In RngIntersection
'Checking if RngCell does not contain a number within the specified limits.
If (IsNumeric(RngCell.Value) = False Or _
RngCell.Value < DblHeightMin Or _
RngCell.Value > DblHeightMax _
) And _
RngCell.Value <> "" Then
'Setting StrMessage.
StrMessage = StrMessage & vbCrLf & "-range " & RngIntersection.EntireColumn.Address(0, 0) & StrMessageReason
'When the first invalid input of the block is found, the block is left.
GoTo CP_RngHeightColumnEnd
End If
Next
End If
CP_RngHeightColumnEnd:
'_____________________________
'RngHeightColumn Block - End
'-----------------------------
'If StrMessage has changed since its initial value, it is reported.
If StrMessage <> "Invalid input:" Then
MsgBox StrMessage, vbCritical + vbOKOnly, "Invalid input"
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.