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
Related
I created a macro which finds all matched values of a cell in a range. The user enter for example numb in the cell A2 Then every time he clicks on Find button it will show him the matched cell for example Order Number then purchase number then part number ...
Here is my code
Sub find_matched_cells()
w = ActiveWorkbook.Sheets("Sheet4").Range("A2")
ActiveWorkbook.Sheets("Sheet4").Range("A4:A104").Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
End Sub
It works fine but when the user for example press on a cell not in my range then on the button find will not work. The issue is in
After:=ActiveCell
For example if by mistake you selected a cell in a range not in A4:A104 then the ActiveCell will not be in my range and it will give an error.
I need to add condition for example the first ActiveCell Starts from A4 and if I find the first matched cell then ActiveCell will take that value.
Any other suggestions please? Thank you very much.
One approach is this...
Sub find_matched_cells()
Dim Rng As Range
Set Rng = ActiveWorkbook.Sheets("Sheet4").Range("A4:A104")
w = ActiveWorkbook.Sheets("Sheet4").Range("A2")
If Intersect(ActiveCell, Rng) Is Nothing Then Range("A4").Select
Rng.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
End Sub
You could check the intersection. I would re-write the code slightly depending on what you need to actually do to each of these found cells - is it just select them? I have amended code below to avoid error if the value is not found.
Sub x()
Dim rFind As Range, sAddr As String, w
w = ActiveWorkbook.Sheets("Sheet4").Range("A2")
With ActiveWorkbook.Sheets("Sheet4").Range("A4:A104")
Set rFind = .Find(What:=w, LookIn:=xlFormulas, LookAt:=xlPart, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
sAddr = rFind.Address
Do
rFind.Select
MsgBox w & " found at " & rFind.Address
Set rFind = .FindNext(rFind)
Loop While rFind.Address <> sAddr
End If
End With
End Sub
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
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'm having troubles with a Macro that i can't figure it out how to do it, i need a Macro that can scan trough a workbook, Find 3 values, "Data: ", "N°" and "Rodovia:", make a offset of 1 column from them and select that value, and paste it in another sheet like this:
Data: | 10/03/2014
N°: | L02.020.22C
Rodovida: | GO-020
So it must select "10/03/2014", "L02.020.22C" and "GO-020"
I can do it using find and Resize i know, but they are not in the same row or column, they are located in random rows and columns, thats the problem, i tried to use a Range().Select with multiples .Find().offset() inside but it didn't worked
After that i need it to do it with all the cases in the workbook, so i need it to give me that
Case1Data|Case1N°|Case1Rodovia
Case2Data|Case2N°|Case2Rodovia
Case3Data|Case3N°|Case3Rodovia
UPDATE, Code so far:
Sub Gather_Values()
Dim Rng As Range
With Sheets("01").Range("A:AJ")
Set Rng = .Find(What:="Data: ", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Selection.Copy
Sheets.Add.Name = "New"
Worksheets("New").Activate
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End With
End Sub
This subroutine will collate the 3 values for a single sheet named "01", searching the UsedRange of that sheet:
Sub Gather_Values()
Dim Rng As Range
Dim Sht As Worksheet
'create new worksheet, name it "New"
Set Sht = Sheets.Add
Sht.Name = "New"
'set column titles in the new sheet
Sht.Range("B1").Value = "Data"
Sht.Range("C1").Value = "N°"
Sht.Range("D1").Value = "Rodovia"
'search the entire UsedRange of sheet 01
With Sheets("01").UsedRange
Set Rng = .Find(What:="Data: ", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("B2").Value = Rng.Value 'put value from the Find into B column of new sheet
Set Rng = .Find(What:="N°", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("C2").Value = Rng.Value 'put value from the Find into C column of new sheet
Set Rng = .Find(What:="Rodovia:", _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, 1)
Sht.Range("D2").Value = Rng.Value 'put value from the Find into D column of new sheet
End With
End Sub
If the values appear more than once on a single sheet you will need to determine either what ranges of cells will always contain separate records or come up with a different way of parsing the data to make sure the values you find in sequence are of the same "case" or "record".
It would be helpful to see a sampling of the raw data that you are parsing to see if there is a better way to collate it than using Find(). It would be best to see an instance where there is more than one "case" in the raw data.