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

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.

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

Excel-VBA Range.Find method error handling

I'm using the range.find method to locate a cell with specific value in the first row. When there's a match, the code works fine. When there's no match, the code throws error as shown in screenshot below.
Is it normal that we need error handling for this method? I thought it'd just return Null or Nothing. Thank you for your help!
Use this. if you directly wanted to return column number of an empty range, it will show error.
Sub findtest()
dim c as long
dim rng as range
Set rng = ActiveSheet.Rows("1:1").Find(What:="John Smith", _
After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then c = rng.column : Debug.Print c
end sub

Searching for a string of text in a specific worksheet

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

Naming Dynamic Range and turning it into a variable

I am trying to name a range based on a .Find then define that range as my variable so I can enter the variable into a different function. When I run the code I get a type mismatch error.
Sub Faked()
Dim r As Range
Cells.Find(What:="EE status", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Name = "Win"
Set r = ("Win")
End Sub
Because Scott says so. Try Set r = range("Win")
To test if r picked up the range correctly, one could do
for each c in r
debug.print c
next
Edit: or if you're cool like Dirk,
Set r = [Win]
Replace the code with this:
Dim r As Range
Cells.Find(What:="EE status", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Set r = Range(Selection, Selection.End(xlDown))
r.Name = "Win"
Explanation:
You cannot use the SET command with the name property of the range object. The object needs to be populated with the range selection first, then you can just use the property of the object to assign a value to it, in this case the Name property of it.
Hope that helps!

vba code for Find the date

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