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
Related
I am using userforms to input data, at a certain point part of the data is copied to one of the sheets in the workbook.
My code then needs to use one of the values in the sheet to check if this value apears on another sheet, if it does it copies values linked to that value to the original sheet and then populates the userform so that further info can be captured.
If I activate on error resume next everything works except the tab function stops working on the userform, if I run it without on error resume next, I get run-time error:
'91' Object variable or with block variable not set.
How do i fix this?
Sub Find_7_day()
Dim vfind
Dim rng As Range
Sheets("Test Data").Select
Sheets("Test Data").Range("$E$3").Select
vfind = ActiveCell
'On Error Resume Next
Call Sheet
Set rng = Cells.Find(What:=vfind, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
If ActiveCell = vfind Then
Call Old_7_day
Call Form_7_day_fill
Else
Sheets("Test Data").Select
End If
End Sub
You can't declare and .Activate a variable range at the same time:
Dim rng As Range
Set rng = Cells.Find(What:=vfind, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate
suggestion, avoid using .Select and Activate, explanation here How to Avoid the Select Method in VBA & Why
Code:
Sub Find_7_day()
Dim vfind As String
Dim rng As Range
vfind = Sheets("Test Data").Range("$E$3").Value
Call Sheet
Set rng = Cells.Find(What:=vfind, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True, _
SearchFormat:=False)
If rng Is Nothing Then
MsgBox vfind & " " & "dont exist"
Exit Sub
End If
If rng.Value = vfind Then
Call Old_7_day
Call Form_7_day_fill
Else
Sheets("Test Data").Select
End If
Exit Sub
End Sub
I have the piece of code below:
Selection.Find(What:="4", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Dim cl As Range, rTest As Range
Set rTest = Range("a1", Range("a1").End(xlToRight))
For Each cl In rTest
If Not cl.Value > 0 Then
cl.EntireColumn.Hidden = True
End If
Next cl
End Sub
Where says What=4, i would like to search the Range(e15) of another worksheet. Search the value of E15 in one sheet and look for it in a specific range in another sheet. I have all the other piece set, but I dont know how I can reference the value of e15, this can 4 or any other number. After finding, hide all columns that are not my specific value. Many thanks!
you should act like follows
Dim f As Range
Set f = Selection.Find(What:=Worksheets("otherWorksheetName").Range("e15").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not f Is Nothing Then
f.Activate '<--| what do you need this for?
Range("A1", Range("A1").End(xlToRight)).EntireColumn.Hidden = True '<--| hide all columns in wanted range
f.EntireColumn.Hidden = True '<--| unhide found range column
End If
where you have to change "otherWorksheetName" to you actual "other " worksheet name
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.
I am currently working on a report, originating in Infoview (SAP business Objects)
This is a report that provides valuable information on a weekly basis, to enhance awareness of current shop performance.
Like the tile of the post might show i want to find a cell with specific text. It has multiple occurrences, and I want to past a previously selected cell in all of those instances.
I can reach the same result by Ctrl-F, "Search all" ( for the "specific text") and than Paste (the previously selected cell)
( http://www.extendoffice.com/documents/excel/816-excel-select-cells-with-specific-text.html)
But i would like to automate this.
I want to use:
Cells.Find(What:="[ö]", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
and
Cells.FindNext(After:=ActiveCell).Activate
But I can't incorporate the two in one macro that gives me the result i describes above.
The previously selected cell contains a formula, containing index(match) and a reference to a cell on the same row as the "specific text".
In my opinion this way of doing stuff saves me a lot of trouble with dynamic cell references ect.
I hope you can help
Your request is a little vague, but I believe this will get you started
Dim PasteValue as string 'this is what you're pasting in
Dim WS as Worksheet
Dim FirstCell as string
Dim rng as range
PasteValue = 'do something here to get your value
set rng = Cells.Find(What:="[ö]", LookIn:=xlFormulas, LookAt:= xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
while not rng is nothing 'make sure you found something
if len(FirstCell) = 0 then
firstcell = rng.address 'save this spot off so we don't keep looping
end if
rng.value = PasteValue
'now find the next one
set rng = Cells.Find(What:="[ö]", LookIn:=xlFormulas, LookAt:= xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
if rng.address = FirstCell then 'back at the first cell
set rng = nothing 'so get out of the loop
endif
end while
Thanks for the code above, it helped a lot.
This is the final code, in case anyone needs it. The
If rng Is Nothing Then Exit Do
Loop While rng.Address <> strFirstAddress
is particulary usefull.
Sub Fill_VCNC()
Dim formula_ö As String
Dim rng As Range
Dim strFirstAddress As String
With Range("S:S")
Set rng = Cells.Find(What:="[ö]", LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
If Not rng Is Nothing Then
strFirstAddress = rng.Address
Do
rng.formula = formula_ö
.NumberFormat = "0.00"
Set rng = .FindNext(rng)
If rng Is Nothing Then Exit Do
Loop While rng.Address <> strFirstAddress
End If
End With
End Sub
( source : https://social.msdn.microsoft.com/Forums/en-US/958fca4e-b19d-4b50-8235-e05adc7f25d5/loop-through-a-range-until-value-not-found?forum=exceldev)
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