Skip to content

Instantly share code, notes, and snippets.

@quyleanh
Created March 25, 2021 02:19
Show Gist options
  • Save quyleanh/9b5daafaf158e2c54b3dcbcd2f3647d8 to your computer and use it in GitHub Desktop.
Save quyleanh/9b5daafaf158e2c54b3dcbcd2f3647d8 to your computer and use it in GitHub Desktop.
Kien_nho_2.vb
Dim searchStr As String
Dim searchFlag As Boolean
Dim shapeList() As Shape
Dim resultID As Integer
Private Sub GetData()
If searchStr <> SearchShapeUF.TextBox1.Value Then
searchStr = SearchShapeUF.TextBox1.Value
searchFlag = False
End If
End Sub
Private Sub StartSearch()
ReDim shapeList(0)
If searchStr = "" Then
Label1.Caption = "Result: 0/0"
MsgBox "You entered nothing!"
Else
Dim shapeTmp As Shape
ReDim shapeList(0)
resultID = 0
For Each shapeTmp In ActiveSheet.Shapes
If shapeTmp.Type = msoGroup Then
Dim sTmp As Shape
For Each sTmp In shapeTmp.GroupItems
If sTmp.TextFrame2.HasText = msoTrue Then
If InStr(1, sTmp.TextFrame.Characters.Text, searchStr, vbBinaryCompare) <> 0 Then
resultID = resultID + 1
ReDim Preserve shapeList(resultID - 1)
Set shapeList(resultID - 1) = sTmp
End If
End If
Next
Else
If shapeTmp.TextFrame2.HasText = msoTrue Then
If InStr(1, shapeTmp.TextFrame.Characters.Text, searchStr, vbBinaryCompare) <> 0 Then
resultID = resultID + 1
ReDim Preserve shapeList(resultID - 1)
Set shapeList(resultID - 1) = shapeTmp
End If
End If
End If
Next
If resultID = 0 Then
MsgBox "No matching result!"
Label1.Caption = "Result: 0/0"
Else
resultID = 0
Application.Goto reference:=ActiveSheet.Range(shapeList(resultID).TopLeftCell.Address), Scroll:=True
ActiveWindow.SmallScroll 0, 5, 0, 5
' shapeList(resultID).Application.Goto
shapeList(resultID).Select
Label1.Caption = "Result: " & CStr(resultID + 1) & "/" & CStr(UBound(shapeList) + 1)
searchFlag = True
End If
End If
End Sub
Private Sub NextResult()
If searchFlag And ((Not Not shapeList) <> 0) Then
If (resultID < UBound(shapeList)) And (resultID >= 0) Then
resultID = resultID + 1
ElseIf resultID = UBound(shapeList) Then
resultID = 0
End If
Application.Goto reference:=ActiveSheet.Range(shapeList(resultID).TopLeftCell.Address), Scroll:=True
ActiveWindow.SmallScroll 0, 5, 0, 5
shapeList(resultID).Select
Label1.Caption = "Result: " & CStr(resultID + 1) & "/" & CStr(UBound(shapeList) + 1)
End If
If searchFlag = False Or ((Not Not shapeList) = 0) Then
StartSearch
End If
End Sub
Private Sub PreviousResult()
If searchFlag And ((Not Not shapeList) <> 0) Then
If (resultID <= UBound(shapeList)) And (resultID > 0) Then
resultID = resultID - 1
ElseIf resultID = 0 Then
resultID = UBound(shapeList)
End If
Application.Goto reference:=ActiveSheet.Range(shapeList(resultID).TopLeftCell.Address), Scroll:=True
ActiveWindow.SmallScroll 0, 5, 0, 5
shapeList(resultID).Select
Label1.Caption = "Result: " & CStr(resultID + 1) & "/" & CStr(UBound(shapeList) + 1)
End If
If searchFlag = False Or ((Not Not shapeList) = 0) Then
StartSearch
End If
End Sub
Private Sub CommandButton3_Click()
PreviousResult
End Sub
Private Sub CommandButton4_Click()
NextResult
End Sub
Private Sub Label1_Click()
End Sub
Private Sub TextBox1_Change()
GetData
End Sub
Private Sub UserForm_Initialize()
Label1.Caption = "Result: 0/0"
TextBox1.SetFocus
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment