Sub 按鈕1_Click()
For Each c In Worksheets(1).Range("B:B").Cells
If c.Value < 4 Then
Worksheets(1).Range("C1") = c.Offset(columnOffset:=-1).Value
Exit For
End If
Next
End Sub
再請教一下大大, 如果要以迴圈執行, 即有多個B欄. 又該如何修改呢?
目前的寫法看來只能用在A欄有序號? 讓每筆數據都跟A欄對應嗎?
謝謝
還是給我一個範例比較快
5 9 0/NA
1 11 12 13
2 33 34 35
3 22 23 24
4 11 12 13
5 9 10 11
6 16 17 18
7 25 26 27
8 66 67 68
9 8 9 10
10 24 25 26
第一列表示檢查到第幾個<10
謝謝
Sub 按鈕1_Click()
Dim Target As Variant
Target = Array("B", "C", "D")
Dim Index As Long
For Each Item In Target
Index = 1
Worksheets(1).Range(Item & "1") = "0"
For Each c In Worksheets(1).Range(Item & ":" & Item).Cells
If Index > 1 And Len(c.Value) > 0 And c.Value < 10 Then
Worksheets(1).Range(Item & "1") = Worksheets(1).Range("A" & c.Row).Value
Exit For
End If
Index = Index + 1
Next c
Next Item
End Sub
需要哪些直接加 Target 就好
如果不想要序號跟A走,把以下程式
Worksheets(1).Range(Item & "1") = Worksheets(1).Range("A" & c.Row).Value
改成
Worksheets(1).Range(Item & "1") = c.Row - 1
可用, 謝謝koro_michael
抱歉, 再請教一下koro_michael 大, 如果要繼續延伸多欄
Array 只能一直編欄位下去, 如果用Range, 又會偵錯 Worksheets(1).Range(Item & "1") = "0"
小弟新手我還沒想到有沒有更好的辦法?
Thanks a lot.
Sub 按鈕1_Click()
Dim Index As Long
Dim Item As String
Dim i As Integer
For i = 2 To 100
Index = 1
Item = change(i)
Worksheets(1).Range(Item & "1") = "0"
For Each c In Worksheets(1).Range(Item & ":" & Item).Cells
If Index > 1 Then
If Len(c.Value) = 0 Then ' 有個欄位是空就結束迴圈
Exit For
ElseIf c.Value < 10 Then
Worksheets(1).Range(Item & "1") = c.Row - 1
Exit For
End If
End If
Index = Index + 1
Next c
Next i
End Sub
Function change(ByVal Num As Integer)
If Num > 26 Then
Dim Param1 As Integer
Dim Param2 As Integer
Param1 = Fix(Num / 26)
Param2 = Num Mod 26
If Param2 = 0 Then
Param1 = Param1 - 1
Param2 = 26
End If
change = change(Param1) + change(Param2)
Else
change = Chr(Num + 64)
End If
End Function
太感謝大大了~
抱歉, 如果要再推到每行同時檢出第一個與第二個小於10的位置
需要將這段改成迴圈, 對嗎?
ElseIf c.Value < 10 Then
Worksheets(1).Range(Item & "1") = c.Row - 1
Sub 按鈕1_Click()
Dim Index As Long
Dim Item As String
Dim i As Integer
Dim First As Long
Dim Second As Long
For i = 2 To 100
Index = 1
First = 0
Second = 0
Item = change(i)
For Each c In Worksheets(1).Range(Item & ":" & Item).Cells
If Index > 1 Then
If Len(c.Value) = 0 Then ' 有個欄位是空就結束迴圈
Worksheets(1).Range(Item & "1") = CStr(First)
Exit For
ElseIf c.Value < 10 Then
If First = 0 Then
First = c.Row - 1
Else
Second = c.Row - 1
Worksheets(1).Range(Item & "1") = CStr(First) & "-" CStr(Second)
Exit For
End If
End If
End If
Index = Index + 1
Next c
Next i
End Sub
嘗試想將First 放第一列 & Second放第二列..... 失敗
Oh ya~ 我再想了一下, 試成功了
再次感謝koro_michael
抱歉再請教, 如果繼續延伸同欄找第三個&第四個. 又該如何寫?
再度卡關中.....
Sub 按鈕1_Click()
Dim Index As Long
Dim Item As String
Dim i As Integer
Const Target As Integer = 3 ' 要找的數量
For i = 2 To 100
ReDim TargetArr(Target - 1) As Integer
Dim Counter As Integer
Counter = 0
Index = 1
Item = Change(i)
For Each c In Worksheets(1).Range(Item & ":" & Item).Cells
If Index > 1 Then
If Len(c.Value) = 0 Then ' 有個欄位是空就結束迴圈
Worksheets(1).Range(Item & "1") = MakeResult(TargetArr)
Exit For
ElseIf c.Value < 10 Then
TargetArr(Counter) = c.Row - 1
Counter = Counter + 1
If Counter = Target Then
Worksheets(1).Range(Item & "1") = MakeResult(TargetArr)
Exit For
End If
End If
End If
Index = Index + 1
Next c
Next i
End Sub
Function MakeResult(Arr() As Integer)
Dim Result As String
Result = "0"
For Each Item In Arr
If Item = 0 Then
Exit For
End If
If Result = "0" Then
Result = CStr(Item)
Else
Result = Result & "_" & CStr(Item)
End If
Next Item
MakeResult = Result
End Function
Function Change(ByVal Num As Integer)
If Num > 26 Then
Dim Param1 As Integer
Dim Param2 As Integer
Param1 = Fix(Num / 26)
Param2 = Num Mod 26
If Param2 = 0 Then
Param1 = Param1 - 1
Param2 = 26
End If
Change = Change(Param1) & Change(Param2)
Else
Change = Chr(Num + 64)
End If
End Function
如果需求項是這樣呢?
Sub 按鈕1_Click()
Dim Index As Long
Dim Item As String
Dim i As Integer
Const Target As Integer = 4 ' 要找的數量
For i = 2 To 2
ReDim TargetArr(Target - 1) As Integer
Dim Counter As Integer
Counter = 0
Index = 1
Item = Change(i)
For Each c In Worksheets(1).Range(Item & ":" & Item).Cells
If Index > 4 Then
If Len(c.Value) = 0 Then ' 有個欄位是空就結束迴圈
Call MakeResult(TargetArr, Item)
Exit For
ElseIf c.Value < 10 Then
TargetArr(Counter) = c.Row - 4
Counter = Counter + 1
If Counter = Target Then
Call MakeResult(TargetArr, Item)
Exit For
End If
End If
End If
Index = Index + 1
Next c
Next i
End Sub
Sub MakeResult(Arr() As Integer, Column As String)
Dim i As Integer
i = 1
For Each Item In Arr
If Item = 0 Then
Exit For
End If
Worksheets(1).Range(Column & CStr(i)) = CStr(Item)
i = i + 1
Next Item
End Sub
Function Change(ByVal Num As Integer)
If Num > 26 Then
Dim Param1 As Integer
Dim Param2 As Integer
Param1 = Fix(Num / 26)
Param2 = Num Mod 26
If Param2 = 0 Then
Param1 = Param1 - 1
Param2 = 26
End If
Change = Change(Param1) & Change(Param2)
Else
Change = Chr(Num + 64)
End If
End Function
已跪~~
我想IF/Elseif 用好久還是只出來兩列
感謝~ 應該沒再有問題了@@