I am trying to find if a certain date is in a range of dates.
This is the range of dates:
01/01/2013
11/02/2013
29/03/2013
20/05/2013
01/07/2013
05/08/2013
02/09/2013
14/10/2013
11/11/2013
25/12/2013
26/12/2013
Here is the VBA code:
' Format Holiday Rows '
With ConfigData.Range("B8:B18")
Set holidays = .Find(s1.Cells(row_count, 1))
If Not holidays Is Nothing Then
MsgBox s1.Cells(row_count, 1)
End If
End With
In the above code, the first MsgBox that pops up reads "11/01/2013". This makes absolutely no sense, as that value is not in the range.
Note: ConfigData.Range("B8:B18") refers to the range of dates shown above.
ALSO: This code is within a for loop that increments the value of s1.Cells(row_count, 1). Starting at 01/01/2013 until 31/12/2013
If you just want to confirm a calendar day in your series is within the holiday list, then you could even use vlookup:
Dim strFound As String
On Error Resume Next
strFound = Application.Vlookup(s1.Cells(row_count, 1), .Range("B8:B18"), 1, 0)
If IsError(strFound) Then
MsgBox "Not Found"
Else
'-- Found
End If
On Error GoTo 0
The following code works for me:
Sub thing()
Dim cell As Range, _
holidays As Range
For Each cell In Range("D1:D365")
With Range("A1:A11")
Set holidays = .Find(cell.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not holidays Is Nothing Then
Debug.Print cell.Value
End If
End With
Next cell
End Sub
If this doesn't work, I'd suggest it's likely you have a cell formatting issue. Select one of your date cells. Go to the immediate window (Alt+F11, then Ctrl+G from Excel), type ? Selection.Value2 and press enter. Does that return a numeric value (~41000)?
Alternatively, you could reenter the dates in a completely new sheet (enter the first couple manually and drag down, do not copy and paste as formatting will be copied also) and try again. This should at least remove odd formatting as a potential issue.
It is important to note that excel uses american date formatting. ie mm/dd/yyyy and it can therefore be a little tricky to get the .Find() function to work properly. Make sure your variables are formated properly in order for excel to hopefully give you what you're looking for:
Dim strdate As String
Dim aCell As Range
strdate = ActiveSheet.Cells(1,1)
strdate = Format(strdate, "Short Date")
On Error Resume Next
Set aCell = Cells.Find(What:=CDate(strdate), After:=Range("A1"), LookIn:=xlFormulas , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If rCell Is Nothing Then
MsgBox("Date cannot be found. Try Again")
End If
End Sub
Of course there are a lot of annoying things that can happen with the date formatting, but this is assuming the dates you're looking for ar in the "Short Date" format.
'To find a cell elsewhere in a worksheet with the same specific date as a reference cell:
'First copy all dates to cells immediately to their left.
'Format the copied cells as "General"
'Run this code - then use the dateRow and DateCol variables (eg in vlookup)
'Works in Excel 2013 (The "General" column must not be hidden - Hide by formatting in background colour)
Dim dateVal
Dim DateRow
Dim DateCol
dateVal = Range("j8").Value 'must be in general format
Cells.Find(What:=dateVal, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
DateRow = ActiveCell.Row
DateCol = ActiveCell.Column
MsgBox (DateRow & " " & DateCol)
End Sub
Related
I have a excel serving as timeline record. Column B contains date of a year, and columns right on it record various event.
I want to make a button which can jump to the row of current date. The first thing I try to do is find a cell with specific date in it. I get a date from a existing cell in column B, then turn back to find it. However the Find method returns nothing.
Sub gotoToday()
Dim LDate As Date
Dim dateCol As Range
Dim cell As Range
LDate = Range("b197").Value ' do get a valid date value here
Set dataCol = Range("B2:B365") ' b197 is inside the range
dataCol.Select
Set cell = Selection.Find(what:=LDate, after:=ActiveCell, LookIn:=xlFormulas, _
Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
cell.Select ' cell get Nothing here
End Sub
Basically I am following This stackoverflow post. Not sure what thing I am missing, can any one help me out?
My guess is that you are looking for a Date type (Dim LDate As Date) whereas dataCol contains strings. Try looking for a string:
Dim LDate As String
'...
LDate = Range("b197").Text
'...
EDIT
You can get a string from a date, formatted to your liking, by using the Format function. Example:
LDate = Format(Range("b197").Value, "mm/dd/yyyy")
If your values in column B really are dates, computed using formulas and formatted as mm/dd, the Find will work with LDate as a string and LookIn:=xlValues, even if they are displayed without the year.
I have a sub routine which I want to take the worksheet name and search through every column B in all worksheets, once it has found a match I want it to take a direct link to the 'total' cell from the first workbook. this 'total' cell will always be the last cell in column 'J' however the row will change.
The worksheet name is determined by an earlier sub which imports a CSV
The cell where the 'total' reference should be placed is offset from the search result, it needs to be a link so if the value changes, the cell will update to match the new total.
So far I am able to 'find' the cell using worksheet name, however the value entered into the offset cell is the heading from the first worksheet, not a linked value to the 'total'
Sub Item_Return()
Dim scanstring As String
Dim foundscan As Range
scanstring = ActiveSheet.Name
Dim lcaddr As Range
Set lcaddr = Range("J" & Range("J1", Range("J" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlTextValues).Rows.Count)
For Each Sh In ThisWorkbook.Sheets
With Sh.Columns("B")
Set foundscan = .Find(What:=scanstring, LookIn:=xlValues, Lookat:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
End With
If Not foundscan Is Nothing Then
foundscan.Offset(0, 1).Value = lcaddr.Address(External:=True)
Sh.Activate
foundscan.Activate
ActiveWindow.ScrollRow = foundscan.Row
Exit Sub
End If
Next
MsgBox scanstring & " was not found"
End Sub
The value returned is not the linked cell to last used cell in row "J", it is the value in the first cell in row "J"
Any and all help is appreciated
I just needed to change the range to select the cell only so Set lcaddr = Range("J" & Range("J1", Range("J" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlTextValues).Rows.Count) became
Set lcaddr = Range("J1").End(xlDown)
I'm really new to VBA, and could do with your help please.
Sheet2 is a long list of data (jobs) where each row in column B contains a unique job reference number.
I want users to input one of these numbers into a cell in Sheet 1 (G11), then the macro searches Sheet2ColumnB for the number, goes across 21 cells in that same row, then enters today's date and time into that cell.
(It then goes back to Sheet1 and says "Job Booked Out" but I think I can do this bit)
I've tried to modify some other code I've found, but get errors in the 4th line, and I have no clue if it works.
Sub CloseJob()
Dim cell As Range
Dim temp As Range
For Each cell In Sheets("Sheet1").UsedRange.cell("G11").Cells
If cell <> "" And cell.Row <> 1 Then
Set temp = Sheets("Sheet2").Columns("B").Find(What:=cell.Value, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=True)
'if found
If Not temp Is Nothing Then
'if the search_criteria is in the same sheet
cell.Offset(0, 21) = Date
End If
End If
Next
End Sub
Error:
"Run-time error 483. Object doesn't support this property or method"
by your narrative you seem after this:
Sub CloseJob()
Dim temp As Range
Set temp = Sheets("Sheet2").Columns("B").Find(What:=Sheets("Sheet1").Range("G11").Value, _
LookIn:=xlFormulas, LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=True)
'if found
If Not temp Is Nothing Then temp.Offset(0, 21) = Date
End Sub
notice I changed LookAt:=xlPart to LookAt:=xlWhole for an exact match
Test and I will be able to get connect with like minded peopl
I am working with a code to have a user enter in a year, to which excel would then find in a row, and then autofill the formula in that column. I have managed to be able to get the input and column aspect down, but am struggling with how to incorporate my sting into a range. Please help!
Code below.
Sub Copy_formula()
NewPageName = InputBox("Enter in Year")
Dim rFind As Range
Sheets("data_calc").Select
With Range("A2:AH2")
Set rFind = .Find(What:=NewPageName, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
MsgBox rFind.Column
End If
End With
Selection.AutoFill Destination:=Range(Cells(3, "rFind"), Cells(2393, "rFind"))
Range(Cells(3, "rFind"), Cells(2393, "rFind")).Select
End Sub
"rFind" within double quotes is a String which VBA will try to convert to a column number within a Cells() statement. As columns run from "A" to "XFD", anything beyond that should cause an error. So loose the double quotes.
However, that won't entirely solve your problem. The default return for a Range object is its .Value property.
What you want is the Range.Column property.
So change your Range(Cells(3, "rFind"), Cells(2393, "rFind")) to:
Range(Cells(3, rFind.Column), Cells(2393, rFind.Column))
I have a sheet with days of the year in the row 2, I would like to search this range and find a date via an inputbox, then loop through that column with the selected date to find all cells with a particular letter in it ("E" for instance), then take the corresponding names in column A and copy it to another sheet creating a list of names in the new sheet.
I have code for the date selection via a inputbox and that works well but Im having difficulties with the remainder.
Sub Worksheet_Find()
Dim strdate As String
Dim rCell As Range
Dim lReply As Long
strdate = Application.InputBox(Prompt:="Enter a Date to Locate on This Worksheet", _
Title:="DATE FIND", Default:=Format(Date, "Short Date"), Type:=1)
If strdate = "False" Then Exit Sub
strdate = Format(strdate, "Short Date")
On Error Resume Next
Set rCell = Cells.Find(What:=CDate(strdate), After:=Range("A1"), LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
rCell.Select
On Error GoTo 0
If rCell Is Nothing Then
lReply = MsgBox("Date cannot be found. Try Again", vbYesNo)
If lReply = vbYes Then Run "FindDate":
End If
End Sub
Any help would be greatly appreciated.
wingnut74
I would expect you do a simple Do While loop:
Do While Len(Sheets("SheetName").Cells(i, rCell.collumn).value)>0
'do your if condition here... (if Sheets("SheetName").Cells(i, rCell.collumn).value Like "*E*" or use Instr() function
i=i+1
Loop
something like that... if you are not sure how to use loops, look that up in some tutorials