.FindNext in a loop doesnt' work - excel

I am using this loop to look for values. .Find works but findNext does not, ommiting many values. Here I drop my code, do you have any advice? thank you very much!!
For Each ws In SourceWb.Worksheets
If IsNumeric(Left(ws.Name, 3)) Then
Set gCell = ws.Columns(6).Find(what:=numdoc, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Do
repetidos = repetidos + 1
finalcell = gCell.Address
'merged cells code here not displayed
oldaddress = gCell.Address
'>Having trouble here> **
Set gCell = ws.Columns(6).FindNext(after:=gCell)
'**
Loop Until gCell.Address = oldaddress
End If
End If
Next ws

This was the best I could derive from your clues:
Option Explicit
Sub Test()
Dim WS As Worksheet
Dim SourceWB As Workbook
Dim numdoc As Long
Dim gCell As Range
Dim firstAddress As String
Dim oldaddress As String
Dim finalcell As String
Dim repetidos As Long
Set SourceWB = ThisWorkbook 'added for clarity and safety
numdoc = 456
For Each WS In SourceWB.Worksheets
If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
Set gCell = WS.Columns(6).Find(what:=numdoc, _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Set gCell = WS.Columns(6).FindNext(after:=gCell)
Do
repetidos = repetidos + 1
finalcell = gCell.Address
'merged cells code here not displayed
oldaddress = gCell.Address
Loop Until gCell.Address = oldaddress
End If
End If
Next WS
End Sub
Not sure if it answers the question but, it does demonstrate indentation.
There's probably room for a With...End With in there but I'm too tired to look for it.

This seems to work at this point.
For Each WS In SourceWB.Worksheets
With ws.Range("F:F")
If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
Set gCell = .Find(what:=numdoc, _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Set gCell = .FindNext(after:=gCell)
Do
'merged cells code here not displayed
Loop While Not gCell Is Nothing And gCell.Address <> firstAddress
End If
End If
end with
Next WS

Related

How can I change my VBA code not to use select?

The code I have, works, but it is slow and I want to avoid using select.
I have tried something in the line of the following:
Sub PopulateBlastEvents()
Dim wsfr As Worksheet
Dim wsl As Worksheet
Dim BlNumber As String
Dim BSStep As Long
Dim SI As String
Dim Srng As Range
Dim Nrng As Range
Dim Arng As Range
Dim NotF As String
Dim Found As Range
Application.ScreenUpdating = False
NotF = "NO INFO"
BSStep = 1
Set Rrng = Sheets("Blast List").Range("A2:A45")
Set Srng = Sheets("Blast List").Range("E1:R1")
For Each cell In Rrng
If cell <> "" Then
For Each cell2 In Srng
If cell2 <> "" Then
On Error Resume Next
SI = cell.Value
BlNumber = CStr("Blasted " & BSStep)
Set wsfr = Sheets(CStr(BlNumber))
Set wsl = Sheets("Blast List")
With wsfr.Range("A:A")
Set Found = Cells.Find(What:=SI, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Found Is Nothing Then
With wsl.Range("A:A")
Set Found1 = Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1)
Found1.Value = NotF
End With
Else
With wsl.Range("A:A")
Set Found1 = Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1)
Found1.Value = Found.Value
End With
End If
End With
End If
Next cell2
BSStep = BSStep + 1
End If
Next cell
Set Arng = ThisWorkbook.Worksheets("Blast List").Range("E3:X3").End(xlDown).Select
Application.ScreenUpdating = True
Columns("A:S").EntireColumn.AutoFit
End Sub
The code does run, but returns no value as the range value "rng" remains at NOTHING even though it is in the sheet where it is looking for the value.
Below is the current code I am using that needs to change:
Sub PopulateBlastEvents()
Dim wsfr As Worksheet
Dim wsl As Worksheet
Dim BlNumber As String
Dim BSStep As Long
Dim SI As String
Dim Srng As Range
Dim Nrng As Range
Dim Rrng As Range
Dim Brng As Range
Dim Arng As Range
Dim NotF As String
Application.ScreenUpdating = False
NotF = "NO INFO"
BSStep = 1
Set Rrng = Sheets("Blast List").Range("A2:A45")
Set Srng = ThisWorkbook.Worksheets("Blast List").Range("E1:R1")
For Each Brng In Rrng.Cells
If Brng <> "" Then
For Each Nrng In Srng.Cells
If Nrng <> "" Then
On Error Resume Next
SI = Nrng.Value
BlNumber = CStr("Blasted " & BSStep)
Set wsfr = ThisWorkbook.Worksheets(CStr(BlNumber))
Set wsl = ThisWorkbook.Worksheets("Blast List")
wsfr.Select
Range("A1").Select
Cells.Find(What:=SI, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Selection.Copy
If Err.Description <> "" Then
Sheets("Blast List").Select
Range("A1").Select
Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1).Select
Selection.Value = NotF
Else
Sheets("Blast List").Select
Range("A1").Select
Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End If
Next Nrng
BSStep = BSStep + 1
End If
Next Brng
Set Arng = ThisWorkbook.Worksheets("Blast List").Range("E3:X3").End(xlDown).Select
Application.ScreenUpdating = True
Columns("A:X").EntireColumn.AutoFit
End Sub
I really want to speed up the code and all previous questions I have posted, I was informed not to or avoid using Select.
Please could someone help.

Copying values from one workbook into another using VBA

so I am trying to copy a value from one workbook into another, and keep getting syntax compilation errors. If anyone knows why it would be very helpful
Sub findsomething()
Dim rng As Range
Dim account As String
Dim rownumber As Long
Dim dehyp As Long
dehyp = Replace(Range("A5").Value, "-", "")
account = Sheet.Cells(dehyp)
Set rng = sheet1.List-of-substances-in-the-third-phase-of-CMP-(2016-
2021).xlsx.Columns("A:A").Find(What:=account,
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
rownumber = rng.Row
Sheet1.Cells(2, 2).Value = Sheet1.List-of-substances-in-the-third-
phase-of-CMP-(2016-2021).xlsx.Cells(rownumber,
3).Value
End Sub
Cell A5 contains
numbers with hypens such as 279-01-2.
but to be searchable in the other document needs to be in the form of 279012
Some of your code is unclear, but it would be something more like:
Sub findsomething()
Dim rng As Range
Dim account As String
Dim rownumber As Long
Dim dehyp As Long
Dim wb As Workbook
dehyp = Replace(Range("A5").Value, "-", "") '<< be more specific here about workbook/sheet
account = Sheet.Cells(dehyp) '<< and here
Set wb = Workbooks.Open( _
"L:\PRS\CEPA\Chemicals Management Plan\!Overviews and Summaries\" & _
"List-of-substances-in-the-third-phase-of-CMP-(2016-2021).xlsx")
Set rng = wb.Sheets("sheet1").Columns(1).Find(What:=account, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If not rng is nothing then
thisworkbook.sheets("Sheet1").Cells(2, 2).Value = _
wb.Sheets("sheet1").Cells(rng.Row, 3).Value
End If
End Sub
This would be tidier as a Vlookup though.

Search Multiple strings and assign a string in previous cell in Excel VBA Macro

I have each set of strings which required to search in column 2, if it finds the string, Offset(0, -1) and place given text there, and repeat the process for each set of strings and for each set of text. i tried below query but getting 91 error. please some one help me out.
Sub Sample()
Dim MyAr(1 To 3) As String
Dim MyAr1(1 To 3) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim cCell As Range, dCell As Range
Dim i As Long
Dim x As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "grant"
MyAr(2) = "grant2"
MyAr(3) = "grant3"
MyAr1(1) = "cancel"
MyAr1(2) = "expired"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(2).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'aCell.Interior.ColorIndex = 3
aCell.Offset(0, -1).Value = "g\"
Do
Set aCell = .Columns(2).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
'aCell.Interior.ColorIndex = 3
Else
Exit Do
End If
Loop
End If
Next
For x = LBound(MyAr1) To UBound(MyAr1)
Set cCell = .Columns(2).Find(What:=MyAr1(x), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set dCell = cCell
cCell.Offset(0, -1).Value = "c\"
Do
Set cCell = .Columns(2).FindNext(After:=cCell)
If Not cCell Is Nothing Then
If cCell.Address = dCell.Address Then Exit Do
Else
Exit Do
End If
Loop
End If
Next
End With
End Sub
Sample image
It seems to be bellow.
Sub test()
Dim aCell As Range, bCell As Range
Dim cCell As Range, dCell As Range
Dim i As Long
Dim x As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "grant"
MyAr(2) = "grant2"
MyAr(3) = "grant3"
MyAr1(1) = "cancel"
MyAr1(2) = "expired"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(2).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
'aCell.Interior.ColorIndex = 3
Do
aCell.Offset(0, -1).Value = "g\"
Set aCell = .Columns(2).FindNext(After:=aCell)
Loop Until aCell.Address = bCell.Address Or aCell Is Nothing
End If
Next
For x = LBound(MyAr1) To UBound(MyAr1)
Set cCell = .Columns(2).Find(What:=MyAr1(x), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set dCell = cCell
Do
cCell.Offset(0, -1).Value = "c\"
Set cCell = .Columns(2).FindNext(After:=cCell)
Loop Until aCell.Address = bCell.Address Or aCell Is Nothing
End If
Next
End With
End Sub
I can't get properly what you want, but the following reduced code seems to work....
Sub Sample()
Dim MyAr(1 To 3) As String
Dim MyAr1(1 To 2) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim cCell As Range, dCell As Range
Dim i As Long
Dim x As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
MyAr(1) = "grant"
MyAr(2) = "grant2"
MyAr(3) = "grant3"
MyAr1(1) = "cancel"
MyAr1(2) = "expired"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = .Columns(2).Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -1).Value = "g\"
End If
Next
For x = LBound(MyAr1) To UBound(MyAr1)
Set cCell = .Columns(2).Find(What:=MyAr1(x), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cCell Is Nothing Then
cCell.Offset(0, -1).Value = "c\"
End If
Next
End With
End Sub

Excel VBA to repeat founding

Goal:
Search Sheet1 for keywords in column 18 (keywords:case,etc.)
Once keyword is found, offset (0,-11)
enter the given value C1008 in respective cell.
and repeat the same till it completes full column (about 1500 rows)
I am not able to perform 4th step.
And I need multiple keywords to search and perform the same steps.
Program:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Columns(18).Find(What:="case", LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
End With
End Sub
If you want just to repeat your code use a loop statement like this:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
DIM isTimetoExit As Boolean
Dim currentRow As Long 'Counter of Rows
Dim KeyWord as String
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
isTimetoExit = False
currentRow = 1 'If you start from row 1
KeyWord = "case"
DO
'
Set aCell = .Columns(18).Find(What:=KeyWord, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
'
' Whit a condition go out of loop
isTimetoExit = NOT (Trim(.Cells(currentRow, 1).Value & "") = "")
If (isTimetoExit) THEN EXIT DO
'Go for next row
currentRow = currentRow + 1
' And set the keyword to your new value;
KeyWord = "next case" 'But I don't know where are those KeyWords stroes !?
LOOP
End With
End Sub

I want to search/find value in textbox2 in an active sheet using command button

I want to search/find value in textbox2 in an active sheet using command button
Here is my code:
Dim ws As Worksheet
Set ws = Worksheets("FSS-TEM-00025")
Dim FindString As String
Dim Rng As Range
FindString = Me.TextBox2.Value
If Trim(FindString) <> "" Then
With ws.Range("A1:Z1048576")
'Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)'
Set Rng = ws.Cells.Find(What:=FindString, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
Unload Me
Try this. This works for me
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim FindString As String
Dim Rng As Range
Set ws = ThisWorkbook.Worksheets("FSS-TEM-00025")
FindString = TextBox2.Value
If Trim(FindString) <> "" Then
Set Rng = ws.Cells.Find( _
What:=FindString, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End If
End Sub
Change LookAt:=xlPart to LookAt:=xlWhole if you are trying to find a complete match.
More on .Find here.

Resources