Inserting data into the same row that a button is located in - vba

I have created a macro which copies data from one worksheet to another. I want a generalized macro which copies data from the same row number as that of button, instead of B2 as mentioned below in the code.
Currently this code is working fine; the button text is updated and MacroA has been assigned to it. I read about topleftcell, but am unable to implement it.
Sub MacroA()
'
' MacroA Macro
'
Range("I2:J2").Select
Selection.Copy
Range("B2").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Range("D2").Select
Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close
End Sub

Untested, but might help you along...
Sub Tester()
Dim c As Range, sht As Worksheet
Dim d As Range
Set sht = ActiveSheet
Set c = sht.Shapes(Application.Caller).TopLeftCell
sht.Cells(c.Row, 2).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
With ActiveSheet
Set d = .Cells.Find(What:="", After:=.Range("D2"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
sht.Range("I2:J2").Copy d
.Parent.Save
.Parent.Close
End With
Application.CutCopyMode = False
End Sub

Related

Excel macro paste same value in ctrl+f box although different cell value

New to forum and vba but want to learn more.
Got two tables of large data and want to look for a cell value equal to the cell value to the left of my active cell in table 1 and then find that value in the 2nd table. When value is found I want to return the cell value found in the 5th column to the right of column A in the 2nd table.
The macro I have created works well - if it hadn't been that it always looks for the same value "10.136.32.10" i.e. this value does not change as the active cell moves down table 1. I would like the value to change depending on what is actually copied from the cell to the left. Is there a way to do this? I use Ctrl+f function and then paste in the cell value copied from table 1
Have the following macro:
Sub Makro2()
'
' Makro2 Makro
'
'
ActiveCell.Offset(0, -1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("SKF-NOV-6-2017").Select
Cells.Find(What:="10.136.32.10", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 4).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All Equipment").Select
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub
Here is the code by which you can do your job. This macro searches immediately on all rows. If you only need to search for an active cell, then you need to remove the loop.
Sub macro2()
Dim lr As Long, r As Long, c As Long
Dim str As String
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 1 To lr
str = Cells(r, c).Offset(0, -1)
Sheets("SKF-NOV-6-2017").Select
Cells.Find(What:=str, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 4).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All Equipment").Select
Cells(r, c + 1).past
Next r
End Sub

vba Entire Column should copy

" find " cell value in header will keep changing in raw file, i need " find " cell value ENTIRE column should copy and paste in sheet2
Sub Macro3()
Cells.Find(What:="FSP Center", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate`
Cells.FindNext(After:=ActiveCell).Activate`
Columns("A:A").Select 'i want to select entire column
Selection.Copy
Sheets("Sheet2").Select
ActiveSheet.Paste
End Sub
Sub Macro3()
Dim f As Range
Set f = Rows(1).Find(What:="FSP Center", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext)
If Not f Is Nothing Then
f.EntireColumn.Copy Sheets("Sheet2").Range("A1")
End If
End Sub
'fixed the misspelling "If"

Determine cell value to find in another sheet the same value

I have a sheet with all the store locations and an email template. In the email template sheet a vlookup result will show in G25.I need the value in G25 (e.g. New Barrie) to find in another sheet (All Locations) the same value "New Barrie".
I don't know how to look for the value in G25 instead of the hardcoded New Barrie.
Sub Email()
' Email Macro
Range("G25").Select
Selection.Copy
Sheets("All Locations").Select
Cells.Find(What:="New Barrie", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A37").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Sheets("Email Template").Select
End Sub
I believe you're after something like follows:
Option Explicit
Sub Email()
Dim val As String
Dim myCell As Range
val = Worksheets("template").Range("G25").Value '<--| store worksheet "template" cell "G25" value into a string variable
Set myCell = Worksheets("All Locations").Cells.Find(What:=val, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) '<--| set myCell to the one in "All Locations" worksheet whose content matches the stored value
If Not myCell Is Nothing Then myCell.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub
just review LookIn, LookAt and MatchCase arguments of Find() method to be sure they actually fits your need

Excel VBA Highlight search

I'm trying to use a word in one cell, and then go to another sheet and find that word in another cell. The tricky part is that the second sheet has this word in many places, and I only want the cell that has the word highlighted in blue.
I've tried the following, but the loop keeps on passing over the blue highlighted word and continuing. What am I doing wrong?
Sub TryingIt()
Dim r As Excel.Range
Dim strName As String
Dim strFirstFound As String
strName = ActiveCell.Text
Sheets("Waiting For").Select
Range("A1").Select
Cells.Find(What:=strName, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Set r = ActiveCell
If r.Interior.color = vbBlue Then
r.Offset(1, 0).Select
Else:
Do
Cells.Find(What:=strName, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Set r = ActiveCell
Loop While r.Interior.color <> vbBlue
r.Offset(1, 0).Select
End If
End Sub
Here's the answer:
You have to be careful to be more specific about the color. In my case, it was RGB(0,176,240), not just vbBlue.

need be more dynamic when using search function in vba

I have the following code and is stuck when wondering how to make the search result more dynamic, that is after searching the "price", i need to copy the "price" and the cell at the right of it to cell A1, any help is appreciated.
Sub Macro1()
Cells.Find(What:="price", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Range("L14:M14").Select
Selection.Copy
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
End Sub
Sub Macro1()
Dim f as Range
Set f = Activesheet.Cells.Find(What:="price", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:= False, SearchFormat:=False
if not f is nothing then
f.resize(1,2).copy Activesheet.Range("a1")
end if
End Sub