Find if date is present in Range of cells (Excel VBA) - vba

I have a cell F2 that contains a Date, and is formatted as Custom Date field displaying only day and month.
Then i have a Range of Cells C3;C60 that contains also Dates and is formatted as European Date field displaying dd/mm/yyyy
I am writing a VBA that makes a check on those fields, but it is not working.
in the sample below the variable c can be any cell between F5 and F20.
I get RuntimeError 91.
If Worksheets(1).Range("C3", "C60").Find(Worksheets(1).Cells(2, c.Column).Value) = True Then
c.Value = "Whatever"
Else

.Find() returns a range, your if statement is checking to see if it's TRUE. That will fail.
Dim rng as Range
Set rng = Worksheets(1).Range("C3:C60").Find(Worksheets(1).Cells(2, c.Column).Value)
if not rng is Nothing then
c.Value = "Whatever"
Else
Note the inclusion of Gary's answer

Substitute:
Range("C3:C60")
for
Range("C3", "C60")
There may be other problems.

Related

Excel VBA Make Cell Active in a Specified Range

I'm not sure why the following code gives a "Not found" Message when the value in the initial active cell (-15.0) is also a value within the specified range.
Sub Test()
Dim c As Double
Dim srchRng As Range
With ActiveWorkbook.ActiveSheet
c = ActiveCell.Select
Set srchRng = .Range("V17:V37").Find(what:=c)
If srchRng Is Nothing Then
MsgBox "Not found"
End If
End With
End Sub
Note sometimes a value of 0 exists within the specified range. If so I don't need to do anything but if 0 does not exist, I first need to find the least negative value (I use separately within the worksheet =MAX(IF(V17:V37<=0,V17:V37),MIN(V17:V37)) and this becomes my initial active cell value [eg -15.0] to look for in the specified range), and this least negative value will then be set to zero (by using a goal seek function in this cell)
So this macro will ultimately incorporate an If statement or conditional lookup
Any feedback appreciated.
I made suggested changes but revised code below still returns "Not found".
Cell V38 has the value -15.0 but this number does appear in the range V17:V37.
This simple macro should result in the cell in the range corresponding to the c value being selected. Any further help appreciated.
Dim c As Double
Dim srchRng As Range
With ThisWorkbook.Sheets("Reel_Pack")
c = Range("V38")
Set srchRng = .Range("V17:V37").Find(what:=c)
If srchRng Is Nothing Then
MsgBox "Not found"
End If
End With
End Sub
I found the following code to do the above.
Dim c As Double
c = ThisWorkbook.Sheets("Sheet1").Range("V58").Value
Dim cel As Range
For Each cel In ThisWorkbook.Sheets("Sheet1").Range("V17:V57")
With cel
If .Value = c Then
.Select
End If
End With
Next cel
End Sub
But can someone suggest please how to improve this by actually incorporating the equation I have in cell V58 within this macro? As mentioned that equation is =MAX(IF(V17:V37<=0,V17:V37),MIN(V17:V37)).
So the macro would look into the range V17:V37 and if there is an exact zero then Do Nothing otherwise locate the cell with the least negative number (which is what the equation above does) and select that cell.
I hope this is the correct reason:
c = ActiveCell.Select is returning the value -1 which equates to TRUE in VBA.
So, I think, the line is asking the question c = ActiveCell is Selected? which returns a boolean TRUE/FALSE converted to a double which equals -1.
c = ActiveCell returns the default property of the ActiveCell which is Value.
So that line should read c = ActiveCell or c = ActiveCell.Value rather than c = ActiveCell.Select.
Edit:
Not so sure of my reasoning now - c= Range("A1").Select selects cell A1 and returns -1 still.
I guess that why you should avoid Select like the plague unless you want to actually select a cell rather than just reference it. Have a read of How to avoid using Select in Excel VBA

Change range value based on criteria

I need to show a range if cell.value (D2) is less than today and another of cell.value (D2) is greather than today.
The copying and pasting may consume too much from excel, and it is not working anyway, is there another way to do this?
The cell value is always J3:AB3 , donĀ“t matter what date is in D2
This is what I have so far:
Dim C As Range
Set C = ThisWorkbook.Sheets("SIN").Range("D2")
If CDate(C.Value) <= Date Then
ThisWorkbook.Sheets("PARA").Range("J2:AB2").Copy Destination:=ThisWorkbook.Sheets("SIN").Range("D5:V5")
Else
ThisWorkbook.Sheets("PARA").Range("J3:AB3").Copy Destination:=ThisWorkbook.Sheets("SIN").Range("D5:V5")
End If

Excel VBA Application.Match between Long Time and Custom format

I can't for the life of me get this to work.
I have a column r of time values formatted as "yyyy-mm-dd hh:mm:ss.000 AM/PM" (this format is one of the Custom formats one can choose for a cell).
I also have a cell c containing a time value formatted as "*hh:mm:ss" (the first Time format).
I now want to find on which row i the c.Value is in range r. I do this using
i = Application.Match(c.Value, r, 1)
On this row I get the 'Run-time error '13': Type mismatch' and my guess is that it is because the cells in r has a Custom format, or because there is no date in c.Value.
How can I get this to work? Is there some other way I can get the placement of c.Value in the range r?
Code:
Sub test()
Dim r As Range
Set r = Range("A1:A10")
Dim c As Range
Set c = Range("B1")
Dim i As Integer
i = Application.Match(c.Value, r, 1) 'ERROR!
End Sub
SOLVED:
I didn't know Excel treats dates and times as numbers, so I got it to work when saving c.Value as a type Double before using it in the function.
SOLVED:
I didn't know Excel treats dates and times as numbers, so I got it to work when saving c.Value as a type Double before using it in the function.

sumif code returns error when using column number?

I have a quick question... not sure what I am doing wrong.
I would like to have a named range (single cell) updated with the value from a sumif based on data in another tab of excel. the formula should go through column 2 look for the date and sum any values in column 10.
even when I substitute out the columns with actual hard column letters, I am getting error 1004 method range of object worksheet failed. how can I re-code this to pick up sumif data from another tab?
here is my code
with data_ws
date = #5/13/2014#
[named_range] = worksheetfunction.sumif(.range(.columns(2)), date, _
.range(.columns(10))
end with
You are close and may have an idea from below why yours are not working:
Sub TestSumIf()
Dim oRngA As Range, oRngB As Range
With ActiveCell
.ClearContents
Set oRngA = .Columns(2).EntireColumn ' 1st EntireColumn on the right
Set oRngB = .Columns(3).EntireColumn ' 2nd EntireColumn on the right
.Value = WorksheetFunction.SumIf(oRngA, Date, oRngB)
End With
End Sub
Sample:

Range address, where do I find the sheet?

I work on a UDF and the user inputs a range, say "sheet1!A1:C8".
In the VBA I write the following:
Function RelativeSearch(Search, rng As Range, Row, Column)
MsgBox rng.Address
Here the msgbox only gives me A1:C8. How can I get the "Sheet1"?
I have tried to make rng as string but that does not work as I have to use rng.find later in the code.
Anyone know of a way to get the sheet from the range?
The Range object has a Worksheet property, so:
rng.Worksheet.Name will do what you want.
In addition the Address property has an External argument, so:
rng.Address(External:=True) yields the entire range address, e.g., [Book1]Sheet1!$D$28.
To get a reference to the Sheet, use rng.Parent.
In your specific case, you are looking for rng.Parent.Name.
So you could do
MsgBox rng.Parent.Name & "!" & rng.Address
This is not an answer to the question you asked, but I suspect it might help you avoid having to ask the question.
If you are trying to find, within rng, the value passed as Search, and then return a value derived by some offset by Row and Column, there is no need to know what worksheet rng is on:
Excel formula (perhaps in cell Sheet4!D6):
=RelativeSearch("b",Sheet1!A1:A6,3,2)
Code which will search the range A1:A6 in Sheet1 for the value "b" and then return the value from the cell that is 3 rows below and 2 columns to the right:
Function RelativeSearch(Search, rng As Range, Row, Column)
Dim r As Range
Set r = rng.Find(What:=Search, LookIn:=xlValues, LookAt:=xlWhole)
If r Is Nothing Then
RelativeSearch = CVErr(xlErrNA)
Else
RelativeSearch = r.Offset(Row, Column).Value
End If
End Function
(If you want it to be consistent with VLOOKUP's syntax, you will need to use r.Offset(Row - 1, Column - 1) rather than r.Offset(Row, Column).)