我正在尝试写一些类似的东西:如果值" O"在此范围内找到(M3:Q3)然后填充范围(M3:Q3)中不包含" O"的所有单元格。用" X"。 这是我到目前为止所拥有的。我在使函数填充除调用函数的单元格以外的任何值时遇到了很多麻烦。
Function positive(range_data As range)
Dim display As String
display = ""
Dim positiveValue As Boolean
For Each Item In range_data
If Item.Value = "O" Then
positiveValue = True
End If
Next
If positiveValue = True Then
For Each Item In range_data
If Item.Value = "" Then
Worksheets("Sheet1").Cells(Item.row, Item.Column).Value = "X"
End If
Next
End If
'positive = range_data
'positive = display
End Function
感谢您的帮助!
答案 0 :(得分:0)
如果从Sub
:
Function positive(range_data As Range)
Dim display As String
display = ""
Dim positiveValue As Boolean
For Each Item In range_data
If Item.Value = "O" Then
positiveValue = True
End If
Next
If positiveValue = True Then
For Each Item In range_data
If Item.Value = "" Then
Worksheets("Sheet1").Cells(Item.Row, Item.Column).Value = "X"
End If
Next
End If
positive = "whatever"
End Function
Sub MAIN()
x = positive(Range("M3:Q3"))
End Sub
但没有理由使用函数,因为函数的输出没有被使用。
答案 1 :(得分:0)
您的代码似乎工作正常,我只是把它变成了Sub
Sub positive(range_data As Range)
Dim display As String
display = ""
Dim positiveValue As Boolean
For Each Item In range_data
If Item.Value = "O" Then
positiveValue = True
End If
Next
If positiveValue = True Then
For Each Item In range_data
If Item.Value <> "O" Then
Worksheets("Sheet1").Cells(Item.Row, Item.Column).Value = "X"
End If
Next
End If
'positive = range_data
'positive = display
End Sub
Sub MAIN()
positive Range("A1:C4")
End Sub