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
Related
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.
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
I am trying to write a macro in excel sheet. In this macro, do I take a copy of the cell number (B04) and I search on the worksheet (3), but the problem that I am having is when I want to change the content of the cell macro is also searching for new content
Range("D6").Select
Selection.Copy
Sheets("3").Select
Cells.Find(What:="Yasser Arafat Ateya ELsayed EL", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("B6").Select
Application.CutCopyMode = False
Selection.ShowDetail = True
End Sub
Going with the flow of your code, and with a bit of explanation to help you, amend your code to something like this. Also learn about explicitly referencing separate worksheets in your code rather than relying on ActiveSheet.
Sub findSomeText()
'the find method returns a Range so define a variable to catch this
Dim fndrng As Range
'to search for different strings use a String variable
Dim srchStr As String
'get the srchStr (in this case from D6 in the ActiveSheet)
srchStr = ActiveSheet.Range("D6").Value
'use Sheets("3") - you don't need to select it
'note that this means a Sheet with a (Tab)NAME of "3", not necessarily the third sheet
With Sheets("3")
'explicitly set the ActiveCell to Find from
Range("A1").Activate
'apply the Find method, note use LookIn:=xlValues; use of srchStr and .Cells
Set fndrng = .Cells.Find(What:=srchStr, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
'check if a match has been found
If Not fndrng Is Nothing Then
'your code if srchStr has been found goes here e.g. found value into cell B6
Range("B6").Value = fndrng.Value
' perhaps use MsgBox "Found" to test?
Else
'your code if srchStr has NOT been found goes here
MsgBox "Not Found"
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 need to do is the following using an Excel Macro.
Go to Sheet 2 Copy Range of text L3 to R26
Then go back to Sheet 1 search for cell containing text "Recess Size" in column L
Then paste copied Range starting at cell containing "Recess Size"
This needs to repeat until the end of the column.
This is the code I have come up with using the Microsoft Support Page
Dim x As Integer
NumRows = Range("L2", Range("L600").End(xldown)).Rows.Count
Range("L2").Select
For x = 1 To NumRows
Sheets("Sheet2").Select
Range("A1:G24").Select
Selection.Copy
Sheets("Sheet1").Select
Cells.Find(What:="Recess Size", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
Next
Unfortunately this goes into a continuous loop and will not stop until I press ESC and every other occurrence of the action is pasted 1 column to the left? Is there anything in the code that isn't right?
Hope someone can help.
Activesheet and Activecell are too vague.
If you are working with a multisheet program it is good practice to specify which sheet you are working in or use a "With Sheets("Sheet1")" for a multiple of "Sheet1".
This may not be a cut and paste but you get my meaning:
Sub Sheetspractice()
NumRows = Sheets("Sheet1").Range("L2:L" & Rows.Count).End(xlup).Row
Sheets("Sheet1").Range("L2").Select
For x = 1 To NumRows
Sheets("Sheet2").Range("A1:G24").Select
Selection.Copy
Sheets("Sheet1").Cells.Find(What:="Recess Size", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Sheets("Sheet1").Paste
Sheets("Sheet1").Range("A1").Offset(1, 0).Select
Next
End Sub