gpt4 book ai didi

excel - 可以保存有效性检查结果的范围吗?

转载 作者:行者123 更新时间:2023-12-04 07:22:44 25 4
gpt4 key购买 nike

我正在编写几个 UDF,它们都采用 Range作为输入。因为我需要检查用户输入是否有效,所以我构建了另一个 Function检查诸如 IsNumeric 之类的内容或 IsEmpty等等。每次检查完成时,都会有一个 MsgBox如果检测到一些问题。我想保留这个 MsgBox但是因为有不同的 UDF 将用于同一个 Range我想问我是否可以减少弹出窗口的数量?如果有办法“保存”另一个 Function已完成此Range 的有效性检查?没有办法提前知道任何函数将被调用多少次。
我的代码太长并且还有其他问题,但这是基本思想:

Public Const maxn As Long = 10

Function Check(myRange As Range, p As Long, n As Long) As Boolean
Dim i As Long, k As Long, x As Long
Dim emptyCell As String, noNum As String, emptyRow As String
Dim empty(1 To maxn) As Long

For i = 1 To p
For k = 1 To n
If IsEmpty(myRange.Cells(i, k).Value) Then
x = x + 1
empty(k) = 1
If x = n Then
emptyRow = emptyRow & vbLf & CStr(myRange.Row + i - 1)
End If
ElseIf Not IsNumeric(myRange.Cells(i, k).Value) Then
noNum = noNum & vbLf & ColNo2ColLet(myRange.Column + k - 1) & CStr(myRange.Row + i - 1)
End If
If k = n And x > 0 And x <> n Then
For x = 1 To n
If empty(x) = 1 Then emptyCell = emptyCell & vbLf & ColNo2ColLet(myRange.Column + x - 1) & CStr(myRange.Row + i - 1)
Next x
End If
Next k
Next i

If emptyRow <> "" Then
emptyRow = "Following rows are empty and will not be considered:" & emptyRow
MsgBox (emptyRow)
End If
If emptyCell <> "" Then
emptyCell = "Following cells are empty and will not be considered:" & emptyCell
MsgBox (emptyCell)
End If
If noNum <> "" Then
noNum = "Following cells contain nonnumeric values:" & noNum
MsgBox (noNum)
Check = CVErr(xlErrValue)
Exit Function
End If
Check = True
End Function

Function ColNo2ColLet(x as Long) as String
'returns the Letters corresponding to the Column number provided by myRange.Column
End Function

Function sr(myRange As Range) as Double
' p, n get defined and checked
Call Check(myRange, p, n)
' other calculations
End Function

Function Q(myRange As Range) as Double
' p, n get defined and checked
Call Check(myRange, p, n)
' other calculations
End Function

最佳答案

这就是我所做的。我声明一个变量来存储消息,然后在其中“收集”消息。最后,我只在最后显示 1 个消息框,其中包含所有消息。

Option Explicit

Dim msg As String

Sub Sample()
Dim Ret As Variant
msg = ""

Ret = SomeFunctionA(1)
Ret = SomeFunctionB(1)

If msg <> "" Then MsgBox msg
End Sub

Function SomeFunctionA(x As Long) As String
'
'~~> Some code
'
If msg = "" Then msg = "Error A" Else msg = msg & vbNewLine & "Error A"
End Function

Function SomeFunctionB(x As Long) As String
'
'~~> Some code
'
If msg = "" Then msg = "Error B" Else msg = msg & vbNewLine & "Error B"
End Function
如果您不想键入 If msg = "" Then msg = ... 的替代方法一次又一次
Option Explicit

Dim msg As String

Sub Sample()
Dim Ret As Variant
msg = ""

Ret = SomeFunctionA(1)
Ret = SomeFunctionB(1)

If msg <> "" Then MsgBox msg
End Sub

Function SomeFunctionA(x As Long) As String
'
'~~> Some code
'
StoreMessage "Error A"
End Function

Function SomeFunctionB(x As Long) As String
'
'~~> Some code
'
StoreMessage "Error B"
End Function

Private Sub StoreMessage(s As String)
If msg = "" Then msg = s Else msg = msg & vbNewLine & s
End Sub

关于excel - 可以保存有效性检查结果的范围吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68395422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com