板大 可以幫我看看我註解的對嗎? 沒註明的是不太理解的部分
Option Explicit '強制宣告 防止錯字 造成錯字誤以為變數
Sub Ex()
Dim arr(), str As String, brr() As String, I As Integer
Dim S As Variant, E As Integer
str = Sheets("工作表1").[c2].Value '讀取禁忌
'Sheets("工作表1").[E8] = "" '將儲存格清空
arr = Sheets("data").[a1].CurrentRegion.Rows(4).Value '讀取資料變數-列
For I = 2 To UBound(arr, 2) Step 2 '資料變數 以2欄做為一個數據
S = Split(str, ",") '將禁忌:以,區隔多條件
ReDim brr(0 To UBound(S)) '數組內從0開始計算至最後一位 = 如 1-10 等同UBound 0-9
For E = 0 To UBound(S)
If InStr(arr(1, I), S(E)) Then brr(E) = "禁忌"
Next
S = Join(brr, "")
If S = "" Then
Sheets("工作表1").[E8] = arr(1, I - 1)
Exit For
End If
Next
With Sheets("工作表1").[E8]
If .Value = "" Then
MsgBox "查不到結果"
Else
MsgBox "最後有 " & .Value & "為:" & arr(1, I)
End If
End With
End Sub作者: GBKEE 時間: 2015-7-1 15:49
版大~請問FOR 和NEXT 是要在該怎麼放呢
我將原本的列,和要輸入的位置都改用變數,但我的 FOR A 只會跑第一次,之後的NEXT都不會再經過A,所以變成固定在第1列
A=列 Q=輸入位置
Sub 跳禁忌()
Dim arr(), str As String, brr() As String, I As Integer, A As Integer, Q As Integer
Dim S As Variant, E As Integer
str = Sheets("禁忌").[B3].Value
For A = 1 To 100
arr = Sheets("Data").[c1].CurrentRegion.Rows(A).Value
For Q = 1 To 100
For I = 2 To UBound(arr, 2) Step 2
S = Split(str, ",")
ReDim brr(0 To UBound(S))
For E = 0 To UBound(S)
If InStr(arr(1, I), S(E)) Then brr(E) = "禁忌"
Next
S = Join(brr, "")
If S = "" Then
Sheets("菜單").[C6] = arr(1, I - 1)
Exit For
End If
Next
With Sheets("菜單").Range("c" & Q)
If .Value = "" Then
'MsgBox "查不到結果"
Else
'MsgBox "最後有 " & .Value & "為:" & arr(1, I)
End If
End With
Next
Next
End Sub作者: starry1314 時間: 2015-7-17 13:32