I'm trying to set the ActiveCell's value as follows:
I need to look at the value of the cell that is one row above the active cell (in the same column)
I then need to find this value in the 3rd column of the 'Source - Questions' sheet; I need to start the search from the bottom going up because I need to find the last instance of this value
When I find this cell I need to take the value of the next cell that is just under the cell that was found (in the same column)
This is my code, I get an exception without any helpful information.
ActiveCell.Value = Cells.Find(What:=ActiveCell.Offset(-1, 0).Value,
After:=Sheets("Source - Questions").Cell(1000, 3), LookIn:=xlFormulas,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious,
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Value
Any help would be appreciated.
Cheers
Correct the two small errors:
Sub asdf()
Dim r As Range, s As Worksheet, v As Variant
Set s = Sheets("Source - Questions")
v = ActiveCell.Offset(-1, 0).Value
Set r = s.Cells.Find(What:=v, _
After:=s.Cells(1000, 3), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False, _
SearchFormat:=False).Offset(1, 0)
ActiveCell.Value = r.Value
End Sub
Related
I am attempting to have a macro insert a column on sheet "Runs", and then paste information from sheet "Templates" onto the newly inserted column on a specific Row. I have named the range for row four as "Eight", however, info from templates is pasted onto column A, Row 4, and not the newly inserted column.
Set myWorksheet = Worksheets("Runs")
myFirstColumnT = myWorksheet.Cells.Find( _
What:="TS", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
myLastColumnT = myWorksheet.Cells.Find( _
What:="TE", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
For iCounter = myLastColumnT To (myFirstColumnT + 1) Step -100000000
myWorksheet.Columns(iCounter).Insert
Sheets("Templates").Select
Range("B2:B16").Copy
Sheets("Runs").Select
With Columns(iCounter).Select
Range("eight").PasteSpecial
End With
Next iCounter
The issue is your With doesn't actually do anything and if it did your Range doesn't reference it.
I've removed your loop, it has such a massive step it isn't actually looping anything. Also you don't need a loop for this.
I removed the .column from your .finds because that will cause an error if it fails, I also added in some error checking for if (when) it doesn't find anything.
I removed all instances of .Select because they aren't necessary.
Dim myworksheet As Worksheet
Dim myfirstcolumnt As Range
Dim mylastcolumnt As Range
Dim newcol As Long
Set myworksheet = Worksheets("Runs")
'I'm assuming you need this for a reason other than the loop, otherwise you can remove it
Set myfirstcolumnt = myworksheet.Cells.Find( _
What:="TS", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
If myfirstcolumnt Is Nothing Then
MsgBox "TS not found"
Exit Sub
End If
Set mylastcolumnt = myworksheet.Cells.Find( _
What:="TE", _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
If mylastcolumnt Is Nothing Then 'Avoiding errors
MsgBox "TE not found"
Exit Sub
End If
newcol = mylastcolumnt.Column + 1 'No need to loop to find the column you're making
myworksheet.Columns(newcol).Insert 'use the new column index to add the column
Sheets("Templates").Range("b2:b16").Copy
myworksheet.Cells(4, newcol).PasteSpecial 'We know it's going in row 4 and we have the new column index now
If you want to use your named range you can do myworksheet.Cells(myworksheet.range("Eight").Row, newcol)... Though I suggest changing the name, a range called "Eight" pointing to Row 4 isn't very clear.
Is there a way to search for a value in a list such as in column A, identify the row number of that value (for e.g. it could be Row 40 of Column A), go to a different column (e.g. Row 40, Column B) and then insert data into that column but through the macro (so it is done automatically).
I have tried to play around using the code below but cannot seem to get anywhere;
Dim Cell As Range
Dim RowNumber As Long
Columns("B:B").Select
Set cell = Selection.Find(What:="celda", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
RowNumber = Cell.Row
If cell = "celda" Then
'find row, go to Column B of that row, and insert "abc"
Else
'do it another thing
End If
I found the code above in the link below;
How to find a value in an excel column by vba code Cells.Find (not my own work but props to the creator)
You need little modification to your code. Try below...
Sub FindAndAdd()
Dim fCell As Range
Dim strSearch As String
strSearch = "Harun"
With ActiveSheet
Set fCell = .Columns("A:A").Find(What:=strSearch, _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
'Lookat:=xlWhole to match whole word.
End With
If Not fCell Is Nothing Then
fCell.Offset(0, 1) = "New Value"
Else
MsgBox "No match found.", vbInformation, "Search Result"
End If
End Sub
The main goal is to get the value of the Cell where Row that contains "NQQ" intersect with Column that contains "Revenue", i highlighted the Cell with yellow color.
Till now i just succed to get the row nr which contains "NQQ" and i get stucked.
'Find row of NQQ
Columns("A:A").Select
Set Cell = Selection.Find(What:="NQQ", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
CellNr = Cell.Row
Apply the same logic to Revenue, and use the Row and Column properties of the found ranges. Always incorporate a check when using Find that the search has found the item to avoid errors.
You can do this with formulae, btw.
Sub x()
Dim Cell As Range, Cell1 As Range
Set Cell = Columns("A:A").Find(What:="NQQ", LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
Set Cell1 = Rows(1).Find(What:="Revenue") 'assume revenue is in row 1
If Not Cell Is Nothing And Not Cell1 Is Nothing Then 'avoid error if one or both not found
MsgBox Cells(Cell.Row, Cell1.Column).Address
End If
End Sub
Dim Add As String
Add = Cells(WorksheetFunction.Match("NQQ", Range("A:A"), 0), "C").Address
Debug.Print Add
If "Revenue" column is not always in the same position, use this code:
Add = Cells(WorksheetFunction.Match("NQQ", Range("A:A"), 0), _
WorksheetFunction.Match("Revenue", Range("1:1"), 0)).Address
Debug.Print Add
I am trying to find the last column with data in it. Some of my columns may contain blank columns between them.
The problem with my code is it keeps reverting back to column 13 as the last used column, when in fact the last used column is 19. I want my code to start counting from L8.
How can I fix my code to include blank columns in between?
LastCol = ws.Cells.Find(What:="*", _
After:=ws.Range("L8"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False).Column
Screen Shot
The Range.Find method returns a Range object that represents the first cell where that information is found. So if the first non-empty cell is in L13, that is what would always be returned by this formula. You may choose to search backward (from L1), but there is an easier and much more straightforward way. Just use:
LastCol = ws.Cells(8, ws.Columns.Count).End(xlToLeft).Column
Start from A1 and search backwards:
Sub sjkdfhsgf()
Set ws = ActiveSheet
Set terminus = ws.Cells.Find(What:="*", _
After:=ws.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
Lastcol = terminus.Column
MsgBox Lastcol
End Sub
I would like some help finishing of this piece of code. I need to highlight cells in a column from active cell down to Variable row.
Snippet of my code:
Start = Cells.Find(What:="MEETING", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(7, 0).Select
Range(ActiveCell & Stop).Select 'it is this bit that doesn't work
You rarely have to activate or select a range
Sub BrokenPiece()
Dim rStart As Range
Dim rStop As Range
Dim CompleteSelection As Range
Set rStart = Cells.Find(What:="MEETING", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not rStart Is Nothing Then
Set rStop = rStart.Offset(7, 0)
Set CompleteSelection = Range(rStart, rStop)
CompleteSelection.Select
End If
End Sub
If you want to find the address of the last occurrence of Note: in Columns(1).
DateStop = Columns(1).Find(What:="Note:", LookAt:=xlPart, SearchDirection:=xlPrevious, MatchCase:=False).Row - 2
You would first set your range
DateStop = Columns(1).Find(What:="Note:", LookAt:=xlPart, SearchDirection:=xlPrevious, MatchCase:=False)
Then make sure that you actually found something
If not DateStop is nothing then
Now there are to ways to get the row that you want
result = DateStop.Row -2
or
result = DateStop.Offset(-2).Row
If you choose the Offset method, you should test that DateStop.Row -2 is a valid row. In this you will not throw an error
If DateStop.Row -2 > 0 then
So let's put it together
Function DateStopRowMinus2()
Dim DateStop
Set DateStop = Columns(1).Find(What:="Note:", LookAt:=xlPart, SearchDirection:=xlPrevious, MatchCase:=False)
If Not DateStop Is Nothing Then
If DateStop.Row - 2 > 0 Then
DateStopRowMinus2 = DateStop.Offset(-2)
End If
End If
End Function
Notice when I referred to the Offset of DateStop, I didn't specify a column. That is because the Offset property takes two optional parameters
Offset([Row], [Column])
We know that they are optional because the parameters are enclosed in square brackets []. Here are some valid examples
Range("A10").Offset(2)
Range("A10").Offset(2, 0)
Range("A10").Offset(0, 2)
Range("A10").Offset(0, 2)
Range("A10").Offset(,2)
Range("A10").Offset(2, 2)