我对VBA很新。基本上,我的代码尝试根据区域列中的最大值输出分类,该列具有多个类别。逻辑似乎是正确的,但我继续获得#VALUE!错误。任何帮助将不胜感激!
Public Function luclass(NAPS As Double) As String
Dim lastrow As Long
Dim c As Range, rng As Range
Dim maxclass As String
Dim maxshape As Double
With ThisWorkbook.Worksheets("LandUseClass2")
lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
maxclass = "Blank"
maxshape = 0
For Each c In .Range("B2:B650" & lastrow)
If c.Value = NAPS Then
If .Range("F" & c.Row).Value > maxshape Then
.Range("C" & c.Row).Text = maxclass
End If
End If
Next c
End With
luclass = maxclass
End Function
答案 0 :(得分:2)
.Range("B2:B650" & lastrow)
更改为.Range("B2:B" & lastrow)
.Range("C" & c.Row).Text = maxclass
更改为.Range("C" & c.Row).Value = maxclass
,因为.text
是一个只读属性。您收到#Value
错误,因为您正在尝试写入函数中的范围。
使用Sub
代替Function
或解释您想要实现的目标,我们将从那里开始:)
Public Sub luclass(NAPS As Double)