Trying to find all instances for a certain Text - excel

I am trying to find all instances of a certain text within a range. I tried to creating a "Do Loop", but my loop keeps running, and doesn't time out.
Dim TextRange As Range
Dim LastRow As Long
Dim Cables As Range
LastRow = Report.Range("A" & Rows.Count).End(xlUp).Row
Set TextRange = rReport.Range("A20:AE" & RangeLastRow)
Set Cables = Report.Range("G20:G" & LastRow)
Dim cel As Range
Set cel = Cables.Cells.Find(what:=CableNumber)
If Not FindNumber Is Nothing Then 'If the number is found
Debug.Print CableNumber
Debug.Print FindCableNumber.Address
Do
Set FindNumber = Cables.FindNext(FindNumber)
Loop While Not FindNumber Is Nothing
Else 'If the cable number is not found
End If

The code you show is not complete and I am trying to deduce what you try accomplishing. I commented the strange lines, and changed the variables in a way to make it working. But I cannot be sure that what I suppose you really want accomplishing. Anyhow, please try understanding the next adapted code, test the suggested way and send some feedback:
Sub FandAllOccurrencesInRange()
Dim TextRange As Range 'not used
Dim LastRow As Long
Dim Cables As Range, Report As Worksheet, CableNumber
Set Report = Worksheets("Report") 'in case that Report is not the sheet code name
LastRow = Report.Range("A" & rows.Count).End(xlUp).row
'Set TextRange = rReport.Range("A20:AE" & RangeLastRow) 'not used
'is rReport a typo, or another undeclared, un set sheet?
Set Cables = Report.Range("G20:G" & LastRow)
CableNumber = "something" 'you must give a value to the variable to be searched!!!
Dim FindNumber As Range, firstAddress As String
Set FindNumber = Cables.Find(what:=CableNumber)
If Not FindNumber Is Nothing Then 'If the number is found
firstAddress = FindNumber.Address
Debug.Print CableNumber
Debug.Print FindCableNumber.Address
Do
Debug.Print FindNumber.Address
Set FindNumber = Cables.FindNext(FindNumber)
Loop While FindNumber.Address <> firstAddress
Else 'If the cable number is not found
End If
End Sub
It is good to know that FindNext returns in a loop, restarting iteration from the first occurrences. That's why firstAddress is necessary.

Related

SpecialCells Type Visible cannot find last row in vba

All, I am working part of my code where I want to update filtered noncontiguous cells with index / match. Core of my code is working in proper manner in another case but here seems wrong and do not know what is the reason behind. Working turn endless and could cause that no last row find in section here: Set rngI = usedR.Resize(usedR.Rows.Count - 1).Offset(5).Columns("I:I").SpecialCells(xlCellTypeVisible). Checked with debug.print the result which shows me the end as wrong...$I$174:$I$1046999...proper has to be $I$174:$I$197...what could be the reason behind?
Another question using lastrow calculation..on this way this doesnt work, Dim lastrow As Long, lastrow = rngD(Rows.Count, 1).End(xlUp).row I have to correct like this to count...lastrow = rngD(rngD.Rows.Count, 1).End(xlUp).row. What's the reason behind that once working on first way, once only if I double type range. This code is in Personal folder if it counts anyway
Sub Macro2()
Dim wbD As Workbook: Set wbD = Workbooks("dashboard-advanced.xls")
Dim wsD As Worksheet: Set wsD = wbD.Sheets("Sheet1")
Dim rngD As Range: Set rngD = wsD.Range("A:C")
Dim wbCallLCL As Workbook: Set wbCallLCL = Workbooks("CALL_REPORT.xlsx")
Dim wsCallLCL As Worksheet: Set wsCallLCL = wbCallLCL.Sheets("LCL")
Dim rngCallLCL As Range: Set rngCallLCL = wsCallLCL.Range("A:V")
rngCallLCL.autofilter Field:=10, Criteria1:=Blanks
Dim lastrow As Long
lastrow = rngD(rngD.Rows.Count, 1).End(xlUp).row
Dim usedR As Range, rngI As Range, A As Range, C As Range
Set usedR = wsCallLCL.UsedRange
Set rngI = usedR.Resize(usedR.Rows.Count - 5).Offset(1).Columns("I:I").SpecialCells(xlCellTypeVisible)
For Each A In rngI.Areas
For Each C In A.Cells
res = Application.Match(C.Value, wsD.Range("A2:" & "A" & lastrow), 0)
If IsError(res) Then
C.Offset(, 1).Value = ""
Else
C.Offset(, 1).Value = Application.WorksheetFunction.Index(wsD.Range("B2:" & "B" & lastrow), res, 0)
End If
Next C
Next A
End Sub

search words in cells in columns if not found error

i am trying to search for list of words in different combos and can include commas(i.e john, sue, [john,sue]) also is not case sensitive when looking for the words. code is below how would search multiple words and also not make it case sensitive and if there are words other then those listed error message
Sub Search_Range_For_Text()
Dim c As Range
Dim alastrow As Long
Dim ContainWord As String
ContainWord = "Claim"
alastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For Each c In Range("A2:B2" & alastrow)
If c.Find(ContainWord) Is Nothing And c.Value <> "" Then
MsgBox "Word found " & c.Address
End If
Next c
End Sub
Couple issues with the code in your comment:
You should declare a worksheet variable to avoid relying on the ActiveSheet
Your range "A2:B2" & alastrow is not correct. When alastrow = 50 you will end up with A2:B250
The right syntax of InStr for your purposes is InStr( Cell to search, value to search for, comparison type)
Use comparison type vbTextCompare which is not case sensitive i.e. A = a
Sub substring()
Dim Target As String, C As Range, lr As Long, ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Target = "Claim"
For Each C In Range("A2:B" & lr)
If InStr(C, Target, Compare:=vbTextCompare) Then
MsgBox C.Address
End If
Next C
End Sub
You don't necessary need to check if the test value is larger than 0. In solution, the test string will be evaluated such that 0 = FALSE and ELSE = TRUE

VBA: type mismatch error on finding elements from another list

i am working on the exercise below but i need some help. It is giving runtime error 13. I am a beginner, can you please help me solve it? Of course other ways of solving the problem are more than welcomed. There are 2 lists (1: Sheet2.Range("E5:E1324") and 2:SearchRange = Sheet1.Range("F2:F4178"))
, of long text format and not exact match, list 1 contains a phrase of the information in list2. I need to know how many times data in list 2 is mentioned in list 1 (as i know there are times it goes up to 7)
Thank you a lot,
Ana
Sub countvalues()
' count how many times data in Description Range is listed in SearchRange
Dim i As Integer 'for looping in Description
Dim j As Integer 'for looping in SearchRange
Dim Counter As Integer
Dim FoundData As Range
Dim Description As Range
Dim SearchRange As Range
Set Description = Sheet2.Range("E5:E1324")
Set SearchRange = Sheet1.Range("F2:F4178")
Application.ScreenUpdating = False
Counter = 0
For i = 5 To 1324 'trying with a narrower range for testing purpose
For j = 2 To 4178
Set FoundData = SearchRange.Find(Sheet2.Range("E" & i))
'On Error Resume Next
Counter = FoundData.Count + 1
Next j
Sheet2.Range("F" & i) = Counter
Next i
Application.ScreenUpdating = True
End Sub
Try the adapted code, please. It will work only if your string used for search (What) is contained in the searched range cells... I mean "test searched" will be found in "test searched today". But "test searched today" will not be found in "test searched"
Sub testFindSimilar()
Dim d As Long, strFirstAddress As String
Dim Counter As Long, lastR1 As Long, lastR2 As Long
Dim shD As Worksheet, shS As Worksheet, cel As Range
Set shD = sheet1
Set shS = sheet2
lastR1 = shS.Range("F" & Rows.Count).End(xlUp).Row
lastR2 = shD.Range("E" & Rows.Count).End(xlUp).Row
For d = 5 To lastR2
Set cel = shS.Range("F2:F" & lastR1).Find(What:=shD.Range("E" & d).value, _
After:=shS.Range("F2"), LookIn:=xlValues, SearchOrder:=xlByRows, LookAt:=xlPart)
If Not cel Is Nothing Then
strFirstAddress = cel.Address
Do
Set cel = shS.Range("F2:F" & lastR1).FindNext(cel)
If Not cel Is Nothing Then
Counter = Counter + 1
End If
Loop Until cel.Address = strFirstAddress
End If
shD.Range("F" & d).value = Counter
Counter = 0: strFirstAddress = ""
Next d
End Sub
If you wont to test less rows, you can replace last rows variable with your testing numbers.

VBA - how to choose a column containing a specific cell and name it output column?

There is a cell in another worksheet whose value ranges from 1 to 100, I have defined its name as "SpanLength" in the name manager. In the worksheet I am now concerned with, I want to find the cell which contains the same value as "SpanLength", that is, a value from 1 to 100, within the range I have defined as "FindSpanLength". Then I want to call the column that this cell is within "outputcolumn" so that I can use this column further in the script. How can I do this?
The line of code before End Sub causes the error message 'Wrong number of arguments or invalid property assignment'
I am a VBA novice and no doubt my code is fraught with errors, so would appreciate any help I can get. I have already scoured google for answers but haven't found any specific enough for me to understand.
Sub OutputMaximums()
Dim spanlengthcell As Range
Set spanlengthcell = Range("FindSpanLength").Find("SpanLength")
Range("spanlengthcell").Column(1) = outputcolumn
End Sub
New Code, with error message 1004 (Method 'Range' of object '_Worksheet' failed):
Sub OutputMaximums()
Dim spanlengthcell As Range, outputcolumn As Long
Set spanlengthcell = OUTPUT.Range("FindSpanLength").Find(Range("SpanLength").Value)
If Not spanlengthcell Is Nothing Then
outputcolumn = spanlengthcell.Column
End If
End Sub
When you assign a value to a variable, the variable is on the left hand side of the equals sign.
When using Find always check first that your search term is found before trying to access its properties to avoid subsequent errors.
It is also advisable to specify a number of Find parameters as they may have unexpected settings from when last used on a worksheet.
Sub OutputMaximums()
Dim spanlengthcell As Range, outputcolumn As Long
Set spanlengthcell = Range("FindSpanLength").Find(Range("SpanLength").Value)
If Not spanlengthcell Is Nothing Then
outputcolumn = spanlengthcell.Column
End If
End Sub
Option Explicit
Sub test()
Dim rngSearch As Range, rngFound As Range
Dim strSearch As String
Dim ColumnNo As Long
strSearch = "Bingo"
With ThisWorkbook.Worksheets("Sheet1")
Set rngSearch = .UsedRange
Set rngFound = rngSearch.Find(strSearch)
If rngFound Is Nothing Then
MsgBox strSearch & " does not excist in range."
Else
ColumnNo = rngFound.Column
MsgBox "Text appears in column " & ColumnNo & "."
End If
End With
End Sub
Sub ShowNamedRange()
'The named Range "SpanLenght" must be on the Sheet "SheetWithNamedRange"!
Debug.Print Sheet("SheetWithNamedRange").Range("SpanLength").Address
Debug.Print Sheet("SheetWithNamedRange").Range("SpanLength").Column
End Sub
and after reading the question again:
Sub SearchRange()
Dim rngSearch As Range
Dim rngFound As Range
Dim strSearch As String
strSearch = Sheets("SheetWithNamedRange").Range("SpanLength").Value
Set rngSearch = Sheets("SheetWithFINDRange").Range("FindSpanLength")
Set rngFound = rngSearch.Find(strSearch)
If Not (rngFound Is Nothing) Then _
MsgBox "Found in column " & rngFound.Column & _
" on the sheet """ & rngFound.Parent.Name & """!"
End Sub

Referencing a cells address and storing it in a Range object

I am iterating through a column and wanting to catch the cells that meet the string of text that I am searching for. My problem occurs when I try and set the address of the cell that has met the criteria of the search into a Range object. Line:
Set testRng = i.Range.Adress
Gives me error " Wrong number of arguments or invalid property assignment" and I'm not sure what the issue is here?
This is the entire code I am working with:
Sub Tester()
Dim rng As Range
Dim testRng As Range
Dim i As Variant
Dim cmpr As String
Dim usrInputA As String
usrInputA = InputBox("Col 1 Criteria: ")
Set rng = Range("A2:A10")
For Each i In rng
cmpr = i.Value
If InStr(cmpr, usrInputA) Then
If testRng Is Nothing Then
Set testRng = i.Range.Address
Else
Set testRng = testRng & "," & i.Range.Address
End If
Else
MsgBox "No hit"
End If
Next
End Sub
You should declare i as a Range (not Variant)
Use Union to group together a collection of cells instead of trying to manually build the string yourself
Switch the order of your range set. You only need Set rngTest = i once so I would put this at the bottom so you don't have to keep spamming it.
Option Explicit
Sub Tester()
Dim testRng As Range, i As Range
Dim usrInputA As String
Dim LR as Long
usrInputA = InputBox("Col 1 Criteria: ")
LR = Range("A" & Rows.Count).End(xlUp).Row
For Each i In Range("A2:A" & LR)
If InStr(i, usrInputA) Then
If Not testRng Is Nothing Then
Set testRng = Union(testRng, i)
Else
Set testRng = i
End If
End If
Next i
If Not testRng is Nothing Then
'Do what with testRng?
End If
End Sub

Resources