Finding text within a row, then finding text within that column - excel

I'm trying to find a cell within a set row with a specific string ("Final Mark", then search the cells below that text. If that cell contains a specific piece of text ("Fail"), I then need a msgbox to pop-up.
This is for a workbook that collects data on marked assignments. I've tried the below code, but it's not working.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FinalMark As Range
Set FinalMark = Rows(5).Find(what:="Final Mark", LookIn:=xlValues, lookat:=xlWhole)
If Not FinalMark Is Nothing Then
If Not FinalMark.Column.Find(what:="Fail", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True) Is Nothing Then
MsgBox "Input reason for fail in Further Notes.", vbInformation
End If
End If
End Sub
I was hoping it would search the column where I found the text "Final Mark", but it doesn't.

Change
If Not FinalMark.Column
to
If Not Columns(FinalMark.Column)

Related

Set the column value of a found cell to a name

I am trying to set values for variables to be used later on in my code based on the column value of a found cell.
Thanks to some existing subject, I was able to find the cell, but I am unable to set its column value to a name.
Here is my code:
Dim rFind As Range
With Range("A1:DD1")
Set rFind = .Find(What:="FIND", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
MsgBox rFind.Column
End With
End Sub
The MsgBox returns the correct column number but my attempts at getting it set to a name has failed.
Thanks for your help !
EDIT:
My goal is to create an automatic table with data extracted from another table. I want to use the column number to extract data for each row of my table from the correct column. I currently use a system where I "hardcode" my names for the current column number (e.g.: Publi Const example As Integer = 5). However this is not a flexible solution if my data table were to change (new or removed columns). Finding the column to then set it would solve the issue.
Perhaps this, to name the whole column?
rFind.EntireColumn.Name = "Fred"
Fuller code
Sub x()
Dim rFind As Range
With Range("A1:DD1")
Set rFind = .Find(What:="FIND", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
rFind.EntireColumn.Name = "Fred"
Else
msgbox "Not found"
End If
End With
End Sub

How to check when text entered by way of VBA input box exists in range of cells form hidden sheet.

I have a working Excel sheet that when opened the user is asked to enter a product type. This is done by VBA code InputBox and places the input in cell A6 of the active sheet. I have a hidden sheet that lists all the possible types in cells A2:A160. I would like to have a way to check that what the user entered is valid and if not to try again.
This macro will search the hidden worksheet(Change Sheet2 to the hidden worksheet name). and a MsgBox. You should change ActiveSheet to the actual worksheet.
Sub FindInHiddenSht()
Dim findVal As Variant
With Sheet2 'Your hidden worksheet
Set findVal = .Cells.Find(What:=ActiveSheet.Range("A6").Value, After:=.Cells(1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
End With
If findVal Is Nothing Then
MsgBox "Not Found"
Else
MsgBox "Found at " & findVal.Address
End If
End Sub

Jumping to an excel cell after reading a barcode to see if there is a match

I have an excel sheet with barcodes and details of products. I would like to use a barcode scanner to find items associated with those barcodes in my excel sheet (for inventory purposes) and mark them. I would like excel to jump into the corresponding cell. I have tried creating a macro by reading similar posts and this is as far as I have got:
Private Sub CommandButton1_Click()
Dim code As Variant
Dim matchedCell As Range
code = InputBox("Please scan a barcode and hit enter if you need to")
Set matchedCell = Range("C2:C100").Find(what:=code, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
If Not matchedCell Is Nothing Then
Range(matchedCell.Address).Interior.ColorIndex = 6
Else: MsgBox "Barcode Not Found"
End If
End Sub
This only highlights the cell. Ideally, I would like to highlight the whole row. I would also like to jump to the corresponding cell. I have no experience in this and I just pulled a couple of different examples that I found online until I got this to work without error message. How can I modify this to add additional functions?
I also have a problem with entering the barcode into the inputbox. All I can do is get it entered into a cell by scanning it or manually enter the number into the input box - how could I make it enter the scanned code into this inputbox? The cursor is in the right place, but that is ignored.
Thank you
Private Sub CommandButton1_Click()
Dim code As Variant Dim matchedCell As Range
code = InputBox("Please scan a barcode and hit enter if you need to")
Set matchedCell = Range("C2:C100").Find(what:=code, LookIn:=xlValues, _
lookat:=xlWhole, MatchCase:=True)
If Not matchedCell Is Nothing Then
With matchedCell
Application.Goto .Cells(1)
.Resize(1, 10).Interior.ColorIndex = 6
End With
Else
MsgBox "Barcode Not Found"
End If
End Sub

VBA Find problems - error when nothing found

I bet this is something simple. Even though I've researched a lot and tried several methods I still get run time error 424.
The code is to find a number the user has entered. If the number is in the data set I want to do one thing but if the number is not in the data set I want to do something else.
Code is below.
Sub Test()
Dim Material As String
Dim cell As Range
Material = InputBox("Enter BIS # for material type")
Range("A7:a40").Select
Set cell = Selection.Find(What:=Material, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _, SearchFormat:=False).Activate
If cell Is Nothing Then
MsgBox "Boo"
Else
MsgBox "Great"
End If
End Sub
You can't call Activate if Find returns nothing, so that will cause an error. Also, Activate is a sub, not a function, so you can't set cell to its return value.
Note: There's no need to Select the Range("A7:A40") for the Find function to work. You can fully qualify the Range that the Find function is searching for the specific value by using Range("A7:A40").Find...
Try this instead:
Sub Test()
Dim Material As String
Dim cell As Range
Material = InputBox("Enter BIS # for material type")
Set cell = Range("A7:A40").Find(What:=Material, LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If cell Is Nothing Then ' <-- this lines checks if Find failed to find a match
MsgBox "Boo"
Else
cell.Activate
MsgBox "Great"
End If
End Sub

Find Code from Another Sheet and Return Info From Same Row

I have searched to find the answers to get to where I am but am now stuck! I am a relative beginner with VBA.
I have a Workbook that lists a few hundred orders that we are producing for our customer.
The order details are on the first sheet called "In Progress" and on the 3rd sheet called "StyleData" are more details about each product such as its composition, design reference, SKU etc...
At present my code searches column A on the Data sheet based on the 6 digit style code in the active cell on the In Progress Sheet, then goes to that cell. I have put a MsgBox in purely to put a pause in the code so I know where it has got to.
What I want it to do after finding the style code on the data sheet is return a value on the same row from column H, preferable in a format that the use can select and copy, then it will return to the original cell at the start of the macro.
Code as follows:
Sub get_composition()
Dim item_no As String
Dim data_sheet As Worksheet
Dim found_item As Range
Set Rng = ActiveCell
item_no = ActiveCell.Value
Set data_sheet = Sheets("StyleData")
If Trim(item_no) <> "" Then
With Sheets("StyleData").Range("A:A")
Set found_item = .Find(What:=item_no, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not found_item Is Nothing Then
Application.Goto found_item, True
Else
MsgBox "Nothing found"
End If
End With
End If
MsgBox "Return to Original Cell"
Application.Goto Rng
End Sub
if I understand what you want :
you arrive at cell "found_item" and want to return a value from the same row.
If it's so, you can use method Offset on "found_item"
found_item.Offset() allow you to navigate from the current range
https://msdn.microsoft.com/en-us/library/office/ff840060.aspx
If you are on column A, found_item.Offset(, 1) will return the range on the same line but column B

Resources