-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel finder macro.vbs
62 lines (53 loc) · 2.32 KB
/
excel finder macro.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Sub finder()
' finder Macro
' v1.2.1
' if cell is currently yellow, change it to turqoise (8)
' only counts number of columns based on first row
' only searches columns over to ZZ -- if need to go farther, change cellRange
Dim word, numRows, cellRange, newColor, prevColor, firstAddress, c, NotBold, numFound, loc, locStr, FindNext
word = InputBox("Word to Find?", "")
If word = "" Then MsgBox ("Nothing to find"): Exit Sub
numFound = 0
numRows = ActiveSheet.Range("A1048576").End(xlUp).Row
cellRange = "a1:zz" & numRows
With ActiveSheet.Range(cellRange)
Set c = .Find(word)
If Not c Is Nothing Then
' found an occurrence
firstAddress = c.Address
Do
numFound = numFound + 1
newColor = 6
prevColor = Range(c.Address).Interior.ColorIndex
If prevColor = 6 Then newColor = 8
Range(c.Address).Interior.ColorIndex = newColor
If Range(c.Address).Font.Bold <> True Then
NotBold = False
Range(c.Address).Font.Bold = True
Else
NotBold = True
End If
Range(c.Address).Select
loc = Split(c.Address, "$")
locStr = ""
For Each x In loc
locStr = locStr + x
Next
locStr = " #" & numFound & ": " & locStr
If .FindNext(c).Address <> firstAddress Then
' if there's still another match to show
FindNext = MsgBox(locStr & vbCrLf & vbCrLf & " Find Next?", 4)
' Yes = 6, No = 7
Else
' this is the last match
MsgBox locStr, 0
End If
Range(c.Address).Interior.ColorIndex = prevColor
If Not NotBold Then Range(c.Address).Font.Bold = False
If FindNext = 7 Then Exit Sub
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
If numFound = 0 Then MsgBox ("""" & word & """ not found in this sheet.")
End Sub