I am not experienced in VBA coding.
My VBA code:
Search = InStr(ActiveCell.NumberFormat, Chr(128))
Selection.Find(What:=Search, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
It searches and activates the cells which include the € symbol. It works only if I manually define my selection range.
When I try inserting ActiveSheet.Range("H:H").Select to make column H my selection (which is my goal), the code stops working.
The problem is in the ActiveCell, which is changing depending on what you are selecting. Try like this, you should get lucky:
Option Explicit
Sub TestMe()
Dim Search As String
ActiveSheet.Range("H:H").Select
Search = CStr(Chr(128))
Selection.Find(What:=Search, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select 'or .Activate
End Sub
Once you feel a bit better with recording macros, you may try to avoid ActiveSheet, Selection and ActiveCell:
How to avoid using Select in Excel VBA
This code found the cell with the Greek Euro format in the range A1:A6 on Sheet1 in the workbook containing the code (ThisWorkbook).
The cell must hold a value (to find blanks change "*" to "").
Sub Test()
Dim rRangeToSearch As Range
Dim rFoundRange As Range
Set rRangeToSearch = ThisWorkbook.Worksheets("Sheet1").Range("A1:A6")
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = "#,##0.00 [$€-408]"
Set rFoundRange = rRangeToSearch.Find(What:="*", SearchFormat:=True)
If Not rFoundRange Is Nothing Then
MsgBox "Greek Euro format found in cell " & rFoundRange.Address
End If
End Sub
No idea why [$€-408] denotes Greek.
Related
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
I am not experienced in VBA coding at all and I have a seemingly simple question. I would like to create a click button macro which will find and select every currency formated cell which includes the euro symbol € or every currency formatted cell that has a sum above 0 within a specific column. At the moment this code :
Cells.Find(What:="€", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
is close to what I want to achieve (finds the € symbol) but it does not work on currency formatted cells which by default-automatically add the euro symbol to the sum..and is not defined to a specific column.
If for example in my worksheet only some currency formatted cells have a € symbol (which I am looking for), then macro gives me a Run-time error 91..The same exactly search parameters work fine using the native excel search function.
See pictures attached
Please kindly help it will help me a lot with my project =)
This doesnt work :
https://i.stack.imgur.com/UQqJp.jpg
This finds the euro symbol exactly as i would like my macro do.
https://i.stack.imgur.com/fM1lY.jpg
Part of your need. This routine will Select all cells containing the euro symbol. It will handle euros displayed by formatting as well as euros included in the cell text.
Sub test()
Dim rng As Range, r As Range, rFound As Range
Set rFound = Nothing
For Each r In ActiveSheet.UsedRange
If InStr(1, r.Text, "€") > 0 Then
If rFound Is Nothing Then
Set rFound = r
Else
Set rFound = Union(rFound, r)
End If
End If
Next r
If Not rFound Is Nothing Then rFound.Select
End Sub
Test the following-
Sub test()
Dim rng As Range
Set rng = Cells.Find(What:="€", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then
rng.Activate
End If
End Sub
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
In my workbook I frequently need to activate some sheets by using their CodeName then search for some texts within that sheet and use the row or column number of the cell that contains the text I'm looking for.
In that situations, I am using below kind of codes:
Sheet16.Select '(Using codename)
Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
FirstRow= ActiveCell.Row
Cells.Find(What:="SECOND TEXT I'M LOOKING FOR", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
SecondRow = ActiveCell.Row
Rows(FirstRow & ":" & SecondRow + 1).EntireRow.Hidden = False
Eveything works well, but nowadays I am trying to improve my codes and I'd like to run my codes faster.
Now,
1- How can I reference my WorkSheets' CodeName easily?
(I'm looking for answer like ThisWorkbook.Worksheets("Sheet1") - not a function
Dim wb as Workbook, ws as Worksheet
set wb = ThisWorkbook
set ws = wb.CodeName(Sheet16) or wb.Sheet16 or sheet16
'then
ws.Cells.Find(What .......... rest of the code ...... )
none of them working for CodeName property.
Fully reference a worksheet by codename or Referencing sheets in another workbook by codename using VBA didn't answer my question.
2- How can I avoid using .Activate to use the result cell of Cells.Find() formula.
Again in that example I firstly search for specific text, which is :="FIRST TEXT I'M LOOKING FOR" in the first part of my code, and then I need to use that cell to get it's row number or use offset or anything , and because of that I feel myself obligated to use .Activate because,
FirstRow = Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
kind of codes do not work as well. How to avoid using Select in Excel VBA macros here in that answer there are couple of suggestions but none of them helps me in this case. I tried to get an answer from the owner of this answer to avoid any duplicate question but he suggested me to ask a new question. (And as long as both of my questions belong to my example code and I'll connect them, I asked them together in one question.)
While setting a sheet variable to the codename, the limitation is you can use the codename in ThisWorkbook only i.e. the workbook which contains the code.
Consider this code...
Dim ws As Worksheet
Set ws = wsData 'where wsData is the CodeName of a sheet.
Now in your code you can manipulate or perform actions on ws sheet without activating or selecting it.
Actually in case of CodeNames, you don't need to declare a sheet variable, you can directly refer to the sheet by using it's codename irrespective of which sheet is currently active.
like...
wsData.Cells.Clear
Set Rng = wsData.Range("A1").CurrentRegion
e.g. with your another example code
Dim ws As Worksheet
Set ws = wsData 'where wsData is the CodeName of a sheet.
FirstRow = ws.Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
'Or just (without declaring the ws sheet variable where wsData is the sheet code name)
FirstRow = wsData.Cells.Find(What:="FIRST TEXT I'M LOOKING FOR", After:= _
ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
Sheet 1:- I mentioned the date in Range A1 with dd-mmm format (i.e., 01-Sep)
Sheet 2:- I updated all the date (from Jan to Dec) with same format (dd-mmm)
But I'm unable to find (select the cell) the date.
I wrote as below:
Sub UpdateToday()
Dim x As String
Dim Cell As Range
x = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
Sheets("Sheet2").Activate
Range("C10:NC10").Select'All 360 days
Application.FindFormat.NumberFormat = "dd-mmm"
Set Cell = Selection.Find(What:=x, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=True).Activate
Sheets("Sheet2").Activate
End Sub
There are a few issues:
Unless you think it's absolutely necessary, I would remove the notion of searching on the format. Adding the format into the search is only complicating it. I would remove the formatting criteria and simply change the LookIn parameter to the Find method from xlValues to be xlFormulas so that the formatting does not matter at all.
You need to define x as Date to hold the true value as it is stored in all of the cells.
Your code will only find the first cell with the same date and there could be many. Not sure if this was your intention.
You can't Activate at the same time as assigning the Cell variable. You need to do this in two steps.
I would update your code to look like this:-
Sub UpdateToday()
Dim x As Date
Dim Cell As Range
x = ActiveWorkbook.Sheets("Sheet1").Range("A1").Value
Sheets("Sheet2").Activate
Range("N10:NC10").Select 'All 360 days
Set Cell = Selection.Find(What:=x, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Cell Is Nothing Then
Cell.Activate
End If
Sheets("Sheet2").Activate
End Sub