[英]VBA loop through columns - if inside for
此代碼有什么問題? 應該返回找到“ 1”的列的標題(第一行)。 我傳遞行號(nr),並且應該在M和T列(包括)之間查找
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
我得到的錯誤是
應用程序定義或對象定義的錯誤
和標記線
If ws.Cells(i, nr).Value = 1 Then
我真的不喜歡VB。
如果將nr
用作數值,為什么要以String
發送它。 嘗試將其更改為Integer
,至少應該更進一步。
編輯:我忘記了我認為您可能也混淆了行/列。 我想也許您希望它是:
If ws.Cells(nr, i).Value = 1 Then
這對我有用
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.