VBA值错误

时间:2017-06-02 15:58:53

标签: excel vba excel-vba

我对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

1 个答案:

答案 0 :(得分:2)

  1. .Range("B2:B650" & lastrow)更改为.Range("B2:B" & lastrow)
  2. .Range("C" & c.Row).Text = maxclass更改为.Range("C" & c.Row).Value = maxclass,因为.text是一个只读属性。
  3. 您收到#Value错误,因为您正在尝试写入函数中的范围。

    使用Sub代替Function或解释您想要实现的目标,我们将从那里开始:)

    Public Sub luclass(NAPS As Double)