0
votes

Im in a bit of a hard spot at the moment, i've been searching the web for some weeks now whitout any luck. I hope some of you might have the answer however.

I want to search through a row for a cell with a specific numeric value. The row contains weeknumbers. When found i would like to be able to handle the cell like an object, or at least i need to know the coordiantes of the cell for further processing.

Furthermore, can that process be reversed. Meaning, can i get the value from say the next cell in the same row if i have the coordinates of the prior cell?

Example would be: Search through row for the week number for this week. Hopefully get the coordinates for the cell that contains the right number. enter image description here

When i have that column, i want to find the content of the cells monday to friday for that week for every coworker

I doesnt matter if this i done by VBA or formulas.

Thanks a lot in advance. Best regards Teambit

3
You should probably specify if the same value can occur multiple times. And if so, how should this be handled? Should it return the first cells which has the value or all the cells?Jakob Busk Sørensen
Have you tried any of the lookup and/or index functions? so LOOKUP, HLOOKUP, VLOOKUP and INDEX+MATCH. You can use the OFFSET function to pick up cells that are above/below/left/right of a cell by a set number. It may help if you post some data and expected functionality/results.Simon Wray
The numbers im searching through are weeknumbers. Each monday im generating a list of my coworkers on work, and what they are doing during the week.Teambit

3 Answers

1
votes

No need for VBA for this... you should be able to achieve it with formulae easily enough.

So, cell L3 has the formula:=ADDRESS(3,MATCH($B$1,$A$3:$N$3,0)). This will perform a MATCH on the week entered into cell $B$1 and find it in the range of weeks $A$3:$N$3. The ADDRESS function will return the cell address for where the week number is found, so $G$3 in my example.

Then the lower grid/table will display the working week for the current week. The OFFSET function is moving the cell address down 3 rows, then right 1, 2, 3, 4, or 6 rows for each day of the week. OFFSET will return a 0 for a blank, so the T function is ensuring we get a text equivalent, or blank instead of 0. I'm using INDIRECT to pass the OFFSET function the starting cell address, so it is using the value of our starting week cell ($G$3), instead of that cell ($L$1).

I've not put anything in to make this dynamic per worker, so it's always using row 3, but you can get a cell reference using ADDRESS from a VLOOKUP for a name and then plug that into the other formulae.

Excel ADDRESS, MATCH, OFFSET Formula

0
votes

Check if this help u..

Sub SubOne()

 Dim sh As Worksheet
 Dim rw As Range
 Dim iFoundIt As Range

 Dim RowCount As Integer

 ' var to iterate trough rows
 RowCount = 0

 Set sh = ActiveSheet

 For Each rw In sh.Rows

  ' iterate over all cells in first row
  If sh.Cells(rw.Row, 1).Value = "ThisIsMySpecialValue" Then
    ' value is found, we save the cell for future options
    Set iFoundIt = sh.Cells(rw.Row, 1)
    Exit For
  End If

  RowCount = RowCount + 1
 Next rw

 Debug.Print (iFoundIt.Row)
 Debug.Print (iFoundIt.Column)
End Sub
0
votes

I know if i understand your question well, but if you have coordinates of cell and you wana e.g. next cell on left you can use something like this

Sub findWeek()
    Dim targetSheet As Worksheet
    Set targetSheet = Sheets("Sheet1")

    Dim l As Long

    Dim row As Long
    row = 2

    Dim myVal As String
    myVal = "some name of week you looking for"
    Dim resultCell As Range

    With targetSheet
        Do While .Cells(row, l).Value <> myVal

            l = l + 1
        Loop

        resultCell = .Cells(row, l)
    End With


End Sub

So i updated answer, try something like this. Just change sheet name, maybe row and even name which are you looking for. And in value resultCell you will have object of cell which you are looking for. And if you look at cell/range api you can get its coordinates etc...

So you can make it a function which will return range, or whatever you want... But always in value resultCell will be cell which you are looking for. Maybe you will need to take care if week is not found ;) because this will stuck in loop, but its not that hard