答案1
在 2016 版本中,用 VBA 编写的 UDF 函数可能效果最好。
正如我之前所写,您需要区分单元格已格式化但不使用该格式的情况,因为它是空的或不包含数字。因此,当您比较格式代码时,您可能会得到与比较单元格显示内容不同的结果。
要比较格式代码,您可以使用以下函数:
Function CompNFs(x As Range) As String
' compares NumberFomats in columns of an x Range
Dim c As Range, cc As Range, cel As Range
Dim CountCR As Long, CountDr As Long
For Each c In x.Rows(1).Cells
CountDr = 0: CountCR = 0
Set cc = Intersect(c.EntireColumn, ActiveSheet.UsedRange)
For Each cel In cc
If InStr(cel.NumberFormat, "Dr") > 0 Then
CountDr = CountDr + 1
ElseIf InStr(cel.NumberFormat, "Cr") > 0 Then
CountCR = CountCR + 1
End If
Next cel
If CountDr * CountCR > 0 Then CompNFs = CompNFs & _
Split(c.EntireColumn.Address(0, 0), ":")(0) & ", "
Next c
If Len(CompNFs) > 0 Then
CompNFs = "Columns with mixed formats: " & Left(CompNFs, Len(CompNFs) - 2)
Else
CompNFs = "No mixed formats."
End If
End Function
比较显示文本的函数看起来完全相似(区别仅在于所使用的属性):
Function CompText(x As Range) As String
' compares formatted Output in columns of an x Range
Dim c As Range, cc As Range, cel As Range
Dim CountCR As Long, CountDr As Long
For Each c In x.Rows(1).Cells
CountDr = 0: CountCR = 0
Set cc = Intersect(c.EntireColumn, ActiveSheet.UsedRange)
For Each cel In cc
If InStr(cel.Text, "Dr") > 0 Then
CountDr = CountDr + 1
ElseIf InStr(cel.Text, "Cr") > 0 Then
CountCR = CountCR + 1
End If
Next cel
If CountDr * CountCR > 0 Then CompText = CompText & _
Split(c.EntireColumn.Address(0, 0), ":")(0) & ", "
Next c
If Len(CompText) > 0 Then
CompText = "Columns with mixed output: " & Left(CompText, Len(CompText) - 2)
Else
CompText = "No mixed ouput."
End If
End Function
在工作表中调用该函数时,您指定单行单元格范围。作为响应,该函数将返回使用两个代码的列的列表。将检查指定列中所有使用的单元格。