Searching for a string of text in a specific worksheet - vba

Im trying to make excel search for a text string in a specific column in a specific worksheet that is not the active worksheet. VBA gives me an error that says i cannot use this method of selection. so my question is, do you have a suggestion to do it in another way?
Worksheets("Parts for renovation").Columns("Q:Q").Select
Set cell = Selection.Find(What:="Total transfer price", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
Exit Sub
Else
Worksheets("Internal").Cells(29, 4) = Worksheets("Parts for Renovation").ActiveCell.Offset(0, 4)
End If

There is no need to select anything there:
With Worksheets("Parts for renovation").Columns("Q:Q")
Set cell = .Find(What:="Total transfer price", After:=.Cells(1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
Exit Sub
Else
Worksheets("Internal").Cells(29, 4) = Cell.Offset(0, 4)
End If
End With

Your error comes, because you select a range on a non-active worksheet. This is one of the reasons, why you should be avoiding select in general.
However, if you want to make your code working (which is strongly not advisable), you may consider selecting the worksheet before selecting the range:
Worksheets("Parts for renovation").Select
Columns("Q:Q").Select
For the advisable part, try to avoid the usage of "Select" -
How to avoid using Select in Excel VBA

Related

Need explanation: vba find method troubleshooting

I am using Find method to bring the cell value from another workbook.
The code below brings the value. But I wanted to erase Activate methods, so just using Block Statements with Find method to bring values from another workbook.
'Windows(wb_name).Activate
'Sheets("SheetA").Select
'Set rg =Worksheets("SheetA").Range("C:C")
'With rg
'value1 = Cells.Find(What:="11693", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False,
SearchFormat:=False).Value
'End With
For clarifying, what exactly I want; in values1 = Cells.Find ...I changed Cells to rg but it doesnt work. I want to know why? Also I see it unnecessary to use activate . I want to write a code where I will get rid of Activate another workbook. So, just by giving source wb and ws names and range to look for the value
Try the next way, please:
Sub FindInOtherSheet()
Dim Value1 As String, rg As Range
Set rg = Workbooks("W1.xlsx").Worksheets("SheetA").Range("C:C")
With rg
Value1 = .cells.Find(What:="11693", After:=.cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).value
End With
End Sub
It starts searching after the first cell of the range. ActiveCell does not have sense in a not activated sheet...
Edited:
As an example to clarify your question about the Find "problem" of not returning any error in case of no any match, I would state that this should be considered an advantage.
You can simple check if the function returned a range in this simple way (I will use the above code to exemplify):
Sub FindInOtherSheet()
Dim Value1 As String, rg As Range, fndCell as Range
Set rg = Workbooks("W1.xlsx").Worksheets("SheetA").Range("C:C")
With rg
set fndCell = .cells.Find(What:="11693", After:=.cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
'check if Find returned a range:
If not fndCell is Nothing Then 'if a match has been found
Value1 = fndCell.value
Else
MsgBox "No match has been found...": Exit Sub
End If
End With
End Sub

Trying to select a cell with a variable as a column number

I'm trying to set up a code that will find a cell using the find function and then select a different cell in that column. I store the column number as a variable, and then try to move to a cell in the same column using the variable, but its not working.
I've tried changing it so that the column is stored as a string instead of an integer and tried using the .Cells method instead, neither have worked.
Dim numCol As String
Cells.Find(what:="e", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=
_
False, SearchFormat:=False).Activate
numCol = ActiveCell.Column
Range(numCol & "4").Select
Getting a 1004 "method range of object global failed" error from that last line of code.
Here's some modified code from your query that might be useful. You want to try to use the properties of the range. In the below example I've defined a range that is the found cell fcell. A couple examples of what you can do ar there.
Dim numCol As Long
Dim fcell As Range
'this will find the cell and set it as a variable of fcell
Set fcell = Cells.Find(what:="e", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'you can select it
fcell.Select
'you can select the whole column
fcell.EntireColumn.Select
'you can select the column number (if one column)
numCol = fcell.Column
'you can return the address
MsgBox "the address is " & fcell.Address
Good luck.

VBA find, how to capture when there is nothing to find

I just wrote a seven page macro with two UserForms that is a thing of beauty.
There are two data bases and the one on the left is longer than the one on the right.
I use a variable DDataa1 and the VBA function:
Sub SSearchh()
... code ...
Cells.Find(What:=DDataa1, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
... code ...
End Sub
It works perfectly EXCEPT when there is no DDataa1 to find. (That is, there is no variable in the right hand list that is being searched for in the left hand list.)
In that case the Macro just stops. I want to capture this "data not found" event and write more code specific to this failure but this function does not seem to generate a True/False condition.
Specific assistance would be appreciated
You could try
Dim FindRange As Range
Set FindRange = Cells.Find(What:=DDataa1, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If FindRange Is Nothing Then
' do error handling here
Else
FindRange.Activate
End If

find a cell with specific text, multiple occurences, and past active cell in all cells containing specific text

I am currently working on a report, originating in Infoview (SAP business Objects)
This is a report that provides valuable information on a weekly basis, to enhance awareness of current shop performance.
Like the tile of the post might show i want to find a cell with specific text. It has multiple occurrences, and I want to past a previously selected cell in all of those instances.
I can reach the same result by Ctrl-F, "Search all" ( for the "specific text") and than Paste (the previously selected cell)
( http://www.extendoffice.com/documents/excel/816-excel-select-cells-with-specific-text.html)
But i would like to automate this.
I want to use:
Cells.Find(What:="[ö]", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
and
Cells.FindNext(After:=ActiveCell).Activate
But I can't incorporate the two in one macro that gives me the result i describes above.
The previously selected cell contains a formula, containing index(match) and a reference to a cell on the same row as the "specific text".
In my opinion this way of doing stuff saves me a lot of trouble with dynamic cell references ect.
I hope you can help
Your request is a little vague, but I believe this will get you started
Dim PasteValue as string 'this is what you're pasting in
Dim WS as Worksheet
Dim FirstCell as string
Dim rng as range
PasteValue = 'do something here to get your value
set rng = Cells.Find(What:="[ö]", LookIn:=xlFormulas, LookAt:= xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
while not rng is nothing 'make sure you found something
if len(FirstCell) = 0 then
firstcell = rng.address 'save this spot off so we don't keep looping
end if
rng.value = PasteValue
'now find the next one
set rng = Cells.Find(What:="[ö]", LookIn:=xlFormulas, LookAt:= xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
if rng.address = FirstCell then 'back at the first cell
set rng = nothing 'so get out of the loop
endif
end while
Thanks for the code above, it helped a lot.
This is the final code, in case anyone needs it. The
If rng Is Nothing Then Exit Do
Loop While rng.Address <> strFirstAddress
is particulary usefull.
Sub Fill_VCNC()
Dim formula_ö As String
Dim rng As Range
Dim strFirstAddress As String
With Range("S:S")
Set rng = Cells.Find(What:="[ö]", LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
If Not rng Is Nothing Then
strFirstAddress = rng.Address
Do
rng.formula = formula_ö
.NumberFormat = "0.00"
Set rng = .FindNext(rng)
If rng Is Nothing Then Exit Do
Loop While rng.Address <> strFirstAddress
End If
End With
End Sub
( source : https://social.msdn.microsoft.com/Forums/en-US/958fca4e-b19d-4b50-8235-e05adc7f25d5/loop-through-a-range-until-value-not-found?forum=exceldev)

VBA error: Object variable or with variable not set [duplicate]

This question already has answers here:
how to detect whether VBA excel found something?
(3 answers)
Closed 7 years ago.
I receiving a strange error when running this subroutine in VBA:
Sub NameColumns()
' name key columns for later reference in formulas
Dim startdatecol As Integer
' name start date column
startdatecol = ActiveSheet.Cells.Find(What:="Start Date", after:=[a1], LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
End Sub
Run time error '91': Object variable or With variable not set
Any ideas on how I can fix this subroutine error? And why it is occurring?
Thanks,
AME
The problem is that Find is not finding the cell.
You will find (pun intended) that the following is true:
MsgBox ActiveSheet.Cells.Find(What:="Start Date", after:=[a1], LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) Is Nothing
The first thing you should do is fix your search so that it finds the cell you're looking for.
Edit:
Maybe a change that would better illustrate the problem is this:
Dim found as Range
Dim startDateCol as Integer
Set found = ActiveSheet.Cells.Find(What:="Start Date", after:=[a1], LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not found Is Nothing Then startDateCol = found.Column
MsgBox startDateCol 'This will be zero if the "Start Date" cell wasn't found.
Edit to respond to comment:
'This should find the exact text "Start Date" (case sensitive) in the header row.
'A cell containing, for example "The Start Date" will not be matched.
Set found = ActiveSheet.Range("1:1").Find("Start Date", LookIn:=xlValues, _
LookAt:=xlWhole, MatchCase:=True)