So I’m very new to this code writing and I’m trying to search a excel sheet for a number that is inputed from a barcode and found in a row then selecting another row to input my on hand quantities bar code is in row g the row I want to end up in is F. This is what I have and it doesn’t work. Please beware this is my fist attempt at any code writing.
Sub Barcodesearch()
'
' Barcodesearch Macro
'
' Keyboard Shortcut: Ctrl+Shift+B
'
Range("G3:G344").Select
Selection.Find What:= (Inputbox "Please scan a barcode and hit enter if needed"), After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("F").Offset (0, 2)
End Sub
You need to handle the case where no match was made:
Sub Barcodesearch()
Dim bc, f As Range, ws As Worksheet
Set ws = ActiveSheet
bc = Trim(InputBox("Please scan a barcode and hit enter if needed"))
If Len(bc) = 0 Then Exit Sub 'no entry was made
Set f = ws.Range("G3:G344").Find(What:=bc, LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False)
If Not f Is Nothing Then
f.Offset(0, -1).Select 'got a match
Else
MsgBox "Barcode '" & bc & "' not found" 'no match
End If
End Sub
Related
I have no experience of VBA, and i'm trying to understand if i can get it to run something for me in Excel.
I’m trying to get the user to input a value, click find, this value will be present in the column D. Once the value has been found it must move the sheet to that cell, then scroll to the right to open a hyperlink associated with the previous cell.
I can get the code to do the above based on an exact input but can’t get it to work on the user input.
The simple code to do it against a specific value and open the file is below:
`Sub Macro7()
'
' Macro7 Macro
'
'
Cells.Find(What:="BMS1244", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
Range("N1468").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub`
How do I replace the specific value with user input?
thanks
Does this work for you?
Sub Macro1()
'
' Macro1 Macro
'
'
Dim found As Range
Dim LinkCell As Range
Dim what_to_find As String
what_to_find = InputBox("Which value would you like to find?")
Set found = Cells.Find(What:=what_to_find, After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'change the 5 to however many columns to the right you want to jump
Set LinkCell = found.Offset(0, 5)
LinkCell.Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
Sub Macro4()
'
' Macro4 Macro
'
' Keyboard Shortcut: Ctrl+Shift+D
'
Dim CellContent0
Dim CellContent1
Dim CellContent2
Dim CellContent3
CellContent0 = ActiveCell.Address
CellContent1 = ActiveCell.Offset(, -4)
CellContent2 = ActiveCell.Offset(, 1)
Sheets("1c").Select
Cells.Find(What:=CellContent1, After _
:=ActiveCell, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False). _
Activate
Cells.Find(What:=CellContent2, After _
:=ActiveCell, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False). _
Activate
ActiveCell.Offset(, -1).Copy
Sheets("shipping").Select
Range(CellContent0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Please advice how to fix the code.
I need to copy 2 cells to the left(-4) and right(1) from active cell. Then I go to another sheet and get value based on previous copied cells(-1).
Next I want to move back to previous sheet and paste copied value into initial active cell
For example If I run the macros from cell D7, finally I need to paste copied value to the same cell D7.
Since you do not answer my clarification question, please try the next adapted code which avoids selecting/activating, which only consume Excel resources, slowing the code speed and not bringing any benefit. It copies where your code tried to do it and the same value from clipboard is copied in the initially selected cell:
Sub Macro4()
' Macro4 Macro
' Keyboard Shortcut: Ctrl+Shift+D
Dim CellContent0 As Range, CellContent1 As Range, CellContent2 As Range
Dim ws1C As Worksheet, wsSh As Worksheet, Find1 As Range, Find2 As Range
Set ws1C = Sheets("1c")
Set wsSh = Sheets("shipping")
Set CellContent0 = ActiveCell
Set CellContent1 = CellContent0.Offset(, -4)
Set CellContent2 = CellContent0.Offset(, 1)
Set Find1 = ws1C.cells.Find(What:=CellContent1.value, After _
:=CellContent0, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Set Find2 = ws1C.cells.Find(What:=CellContent2.value, After _
:=CellContent0, LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows _
, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not Find2 Is Nothing Then 'if a match has been found:
wsSh.Range(CellContent0.Address).value = Find2.Offset(, -1).value
'now I try copying what I understood from your comment:
CellContent0.value = Find2.Offset(, -1).value
End If
End Sub
Copying only the cell value, no clipboard is necessary, too.
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
I'm relatively new to the whole Visual Basic scene but do have basic knowledge in programming and I understand how to read code and follow the logic patterns.
What I am trying to accomplish in this macro is the following:
Find the row in the spreadsheet that says the word "Suppressed" then delete that row and the following rows until it finds the row which says "Other Response Categories" in it, and stop there without deleting that from the row.
Find the row in the spreadsheet that says the words "Requires Challenge Response" and delete that row along with all rows underneath it until it finds a row which has a line of text in it named "Tracking Links Clicked" and stop there without deleting that from the row.
Find the row in the spreadsheet that says the words "Link Name (HTML)" and delete that row along with all rows underneath it.
I have used the "Record Macro" function to get a general idea on how to remove lines of text from excel but only by using ranged areas which are selected then deleted; not searching for key phrases.
I'm in the works on researching a lot of VB stuff in order to actually write what I want to accomplish.
Edit2: So I modified and simplified down the VB code that you provided to at least try and get the same response you provided; but only searching for one of the values. I wanted to get correct input for one value before trying to add in more.
Edit 3: Was able to write the script with the assistance of a friend, thank you so much everyone for their input. I have attached the working script on here:
Option Explicit
Sub Autoformat()
Dim WSA As Worksheet
Dim Rng1 As Range
Dim Rng2 As Range
Dim lpRange As Range
Dim sArr As Variant
Set WSA = ActiveSheet
Dim i As Long
sArr = Array("Suppressed", "Other Response Categories", "Requires Challenge Response", "Tracking Links Clicked", "Link Name (HTML)")
Rows("6:9").Delete
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
For i = 0 To 3 Step 2
Set lpRange = WSA.UsedRange
Set Rng1 = lpRange.Find(What:=sArr(i), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
MatchCase:=False)
Set Rng2 = lpRange.Find(What:=sArr(i + 1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
MatchCase:=False)
On Error Resume Next
If Not Rng1 Is Nothing And Not Rng2 Is Nothing And Rng2.Row > Rng1.Row Then
WSA.Rows(Rng1.Row & ":" & Rng2.Row - 1).Delete
ElseIf Not Rng1 Is Nothing And Rng2 Is Nothing Then
WSA.Rows(Rng1.Row).Delete
End If
Next i
Set lpRange = WSA.UsedRange
Set Rng2 = lpRange.Find(What:=sArr(i), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
MatchCase:=False)
If Not Rng2 Is Nothing Then
WSA.Rows(Rng2.Row & ":" & Rows.Count).Clear
End If
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
Sub ClearNames()
Dim n As Name
For Each n In ThisWorkbook.Names
n.Delete
Next n
End Sub
The Macro Recorder is a great way of discovering the syntax of statements you do not know. However, the Macro Recorder does not know your objectives; it just records each of your actions. The result needs a lot of tidying up.
You must learn Excel VBA if you are going to post questions and expect to understand the answers. Search the web for "Excel VBA Tutorial". There are many to choose from so pick one that matches your learning style. I preferred to visit a large library and try out the VB Excel primers they had. I then bought the one I liked.
This is to give you are start at tidying up something created with the Macro Recorder.
I placed your key phrases in random cells down a worksheet and then searched for them in turn. The macro recorder's output was:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 07/05/2014 by Tony Dallimore
'
Cells.Find(What:="Suppressed", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Other Response Categories", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Requires Challenge Response", After:=ActiveCell, LookIn _
:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Cells.Find(What:="Link Name (HTML)", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
End Sub
This code finds the four phrases but does not do anything else. This shows the syntax of the Find method. We need to tidy this code and save row numbers rather than activate (select) cells. Rather than try to explain each change I have created the code below from the code above. Study the differences and try to understand what I have done and why. Come back with questions if necessary but the more you can achieve on your own, the faster you will build up your skills.
Sub Demo()
Dim Rng As Range
Dim RowSupp As Long
Dim RowOther As Long
Dim RowReq As Long
Dim RowLink As Long
With Worksheets("Sheet1")
Set Rng = .Cells.Find(What:="Suppressed", After:=.Cells(Rows.Count, Columns.Count), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
' Note in the above statement I have replaced "ActiveCell" with ".Cells(Rows.Count, Columns.Count)"
' which is the bottom right cell. Find does not look at the start cell, it wraps and starts
' searching from A1. I have also replaced "xlPart" with "xlWhole".
If Rng Is Nothing Then
Call MsgBox("""Suppressed"" not found", vbOKOnly)
Exit Sub
End If
RowSupp = Rng.Row
Set Rng = .Cells.Find(What:="Other Response Categories", After:=.Cells(Rng.Row, Rng.Column), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
Call MsgBox("""Other Response Categories"" not found", vbOKOnly)
Exit Sub
End If
RowOther = Rng.Row
Set Rng = .Cells.Find(What:="Requires Challenge Response", After:=.Cells(Rng.Row, Rng.Column), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
Call MsgBox("""Requires Challenge Response"" not found", vbOKOnly)
Exit Sub
End If
RowReq = Rng.Row
Set Rng = .Cells.Find(What:="Link Name (HTML)", After:=.Cells(Rng.Row, Rng.Column), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then
Call MsgBox("""Requires Challenge Response"" not found", vbOKOnly)
Exit Sub
End If
RowLink = Rng.Row
End With
Debug.Print """Suppressed"" found on row " & RowSupp
Debug.Print """Other Response Categories"" found on row " & RowOther
Debug.Print """Requires Challenge Response"" found on row " & RowReq
Debug.Print """Link Name (HTML)"" found on row " & RowLink
End Sub
The above macro has found the four rows and has proved it has found them by outputting their values to the Immediate Window.
For my dummy worksheet, the output is:
"Suppressed" found on row 6
"Other Response Categories" found on row 11
"Requires Challenge Response" found on row 16
"Link Name (HTML)" found on row 22
If I am unsure what I am doing, I always code this way. I identify step 1 and code a routine to achieve step 1. I then identify step 2 and update my code to achieve that as well.
If my data matched yours, you would want rows 6 to 10 deleted.
You have posted:
Rows("6:9").Select
Selection.Delete Shift:=xlUp
If we tidy that up we first get:
.Rows("6:9").Delete Shift:=xlUp
The next step is to replace the 6 and the 9 with the row numbers that macro Demo discovered:
.Rows(RowSupp & ":" & RowOther - 1).Delete Shift:=xlUp
Place this under RowOther = Rng.Row and run Demo again.
The first lot of rows are deleted.
Step 3 is to consider how to adjust the third Find statement. The current macro relies on RowOther not moving between Finds 2 and 3. But it has moved up by the number of lines deleted. You cannot use .Cells(Rng.Row, Rng.Column) as the start point for Find 3.
I leave you to think about where to start Find 3.
I have created this macro code using the record function.
Sub Macro1()
Cells.Find(What:="Text to find", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
Range("E5").Select
ActiveCell.FormulaR1C1 = "text to enter"
Range("D6").Select
Cells.Find(What:="Text to find", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
Range("E9").Select
ActiveCell.FormulaR1C1 = "text to enter"
End Sub
I need this macro to continue through the same column until it cannot find any more instances of the searched for word without it going back to the top of the column.
So it starts in a column, every time it finds a specified word it tabs across 1 column and pastes in a specified word.
It continues to search for the specified word in the same column until it cannot find it without starting at the top of the column.
Hope that makes some sense.
Not sure I understand but I think what your looking for is:
For each cell in columns(4).cells
If cell.value="Text to find" Then Cell.offset(0,1) = "Text to enter"
Next cell
You can use Find and FindNext to do this quickly, ie to:
search column D for the text in StrOld
enter any matches into column E with the text in StrIn
code
Sub Recut()
Dim strAddress As String
Dim StrIn As String
Dim StrOut As String
Dim rng1 As Range
StrOld = "Old"
StrIn = "New"
Set rng1 = Range("D:D").Find(StrOld, , xlFormulas, xlWhole, , , True)
If Not rng1 Is Nothing Then
strAddress = rng1.Address
Do
rng1.Offset(0, 1) = StrIn
Set rng1 = Range("D:D").FindNext(rng1)
Loop While Not rng1 Is Nothing And rng1.Address <> strAddress
End If
End Sub