標題:
在範圍內尋找指定內容的位置
[打印本頁]
作者:
enoch
時間:
2012-9-28 16:29
標題:
在範圍內尋找指定內容的位置
請問如何可以在指定儲存格範圍(例如在A1:D5),
找到指定內容(如 "TARGET" )的儲存格位置
請指教
作者:
mark15jill
時間:
2012-9-28 16:39
回復
1#
enoch
Sub sh()
For ghps = 1 To 5
For gg = 1 To 4
If Cells(ghps, gg) = "TARGET" Then
Cells(ghps, 11) = "Y=" & ghps & "X=" & gg
End If
Next gg
Next ghps
End Sub
複製代碼
作者:
enoch
時間:
2012-9-28 23:32
多謝指教, 始終要每行去找尋
作者:
Hsieh
時間:
2012-9-29 00:52
回復
3#
enoch
Sub ex()
If Application.CountIf(Range("A1:D5"), "TARGET") > 0 Then
Range("A1:D5").Replace "TARGET", "=1/0", xlWhole
Set A = Range("A1:D5").SpecialCells(xlCellTypeFormulas, 16)
MsgBox A.Address
A.Value = "TARGET"
End If
End Sub
複製代碼
作者:
stillfish00
時間:
2012-9-29 22:43
本帖最後由 stillfish00 於 2012-9-29 22:44 編輯
Dim A As Range
With Range("A1:D5")
Set A = .Find(what:="TARGET", After:=.Cells(1, 1))
End With
MsgBox A.Address
複製代碼
作者:
c_c_lai
時間:
2012-9-30 06:27
回復
1#
enoch
試試這個,這是我常使用的功能函式。
Function getTarget(sh As String, rng As String, fnd As Variant) As String
Dim c As Range
Set c = Sheets(sh).Range(rng).Find(fnd, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
getTarget = c.Address
End If
End Function
複製代碼
作者:
c_c_lai
時間:
2012-9-30 06:43
回復
1#
enoch
我忘了附上範例。
Sub Test()
Dim adr As String
adr = getTarget("工作表1", "A1:D5", "TARGET")
MsgBox adr
End Sub
複製代碼
作者:
c_c_lai
時間:
2012-9-30 08:14
回復
1#
enoch
實務上,極有可能會需要去取得(或找到) "標的" 右邊的值,
你也可以使用此功能涵式達到目的。
Sub Test()
Dim adr As String
adr = getTarget("工作表1", "A1:D5", "TARGET")
MsgBox adr
adr = getTarget("工作表1", "A1:D5", "TARGET", False)
MsgBox adr
[A6] = Range(adr).Value
End Sub
Function getTarget(sh As String, rng As String, fnd As Variant, Optional curr As Boolean = True) As String
Dim c As Range
Set c = Sheets(sh).Range(rng).Find(fnd, LookIn:=xlValues, LookAt:=xlWhole)
If Not c Is Nothing Then
getTarget = IIf(curr, c.Address, c.Offset(, 1).Address)
End If
End Function
複製代碼
[attach]12637[/attach]
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)