VBA全新,所以只是试图了解使用方法的一般意义。
如果找到值<>,则以下代码定义数组和条件集。 0然后'失败'。我想生成一个满足这个条件的列列表,而不是停止。
任何提示或参考都会有所帮助。谢谢,
Sub validate()
Dim Arr1() As Variant
Dim num_rows As Long, num_columns As Long
Dim column_num As Long, row_num As Long
With ThisWorkbook.Worksheets("Tie Out")
ThisWorkbook.Worksheets("Tie Out").Select
Arr1 = ThisWorkbook.Worksheets("Tie Out").Range("B15:CG66").Value
num_columns = UBound(Arr1, 2)
num_rows = UBound(Arr1, 1)
For column_num = 1 To num_columns
For row_num = 1 To num_rows
If Abs(Arr1(row_num, column_num)) > 0.001 Then MsgBox "Failure"
Next row_num
Next column_num
End With
End Sub
答案 0 :(得分:1)
我稍微调整了您的代码(因为您使用的是With
,因此您不需要继续拼写ThisWorkbook.Worksheets("Tie Out")
)。这将生成立即窗口的列/行列表
你已经接近了。您可能想尝试添加一个函数,该函数将从您拥有的列号返回Column Letter。
Sub validate()
Dim Arr1() As Variant
Dim num_rows As Long, num_columns As Long
Dim column_num As Long, row_num As Long
With ThisWorkbook.Worksheets("Tie Out")
Arr1 = .Range("B15:CG66").Value
num_columns = UBound(Arr1, 2)
num_rows = UBound(Arr1, 1)
For column_num = 1 To num_columns
For row_num = 1 To num_rows
If Arr1(row_num, column_num) <> 0 Then Debug.Print "Column " & column_num + 1, "Row " & row_num + 14
Next row_num
Next column_num
End With
End Sub
下面提供了另一种方法,即我将如何做到这一点:
Sub validate2()
Dim c As Range
For Each c In Worksheets("Tie Out").Range("B15:CG66")
If c.Value <> 0 Then
'add your code here
Debug.Print "Value not = 0 at cell address " & c.Address
End If
Next
End Sub