What is wrong with this code? It is supposed to return header (1st row) of a column in which it finds "1". I pass the row number (nr) and 开发者_运维百科it is supposed to look between columns M and T (inclusive)
Function who(ByVal rowNr As Integer) As String
Dim temp As String
Dim ws As Worksheet
With ActiveSheet
Set ws = ActiveWorkbook.Sheets(.Name)
End With
For i = 13 To 20 Step 1
If ws.Cells(i, rowNr).Value = 1 Then
temp = temp & " " & ws.Cells(i,1).Value
End If
Next i
who = temp
End Function
The error I get is
Application-Defined or Object-Defined error
And marks line
If ws.Cells(i, nr).Value = 1 Then
I truly dislike VB.
If nr
is used as a numerical value, why are you sending it in as a String
. Try changing that to an Integer
and you should be a bit further along at least.
Edit: I forgot that I think you might have mixed up rows/columns as well. I think maybe you want it to be:
If ws.Cells(nr, i).Value = 1 Then
This worked for me
Function who(ByVal rowNr As Long) As String
Dim temp As String
Dim ws As Worksheet
Dim i As Long
Set ws = ActiveSheet
For i = 13 To 20 Step 1
If ws.Cells(rowNr, i).Value = 1 Then
temp = temp & " " & ws.Cells(1, i).Value
End If
Next i
who = Trim(temp)
End Function
精彩评论