I have trying to find a code that will look at the contents of a cell G1 and then search column A for a match and select that cell.
This is part of a longer code, but this is the only bit I can't find out how to do.
Sub searchnFind()
Dim searchResult As Range
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
Set searchResult = ws.Range("A:A").Find(What:=ws.Range("G1").Value, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not searchResult Is Nothing Then
searchResult.Select
End If
End Sub
Related
I have a working code to find a specific string in a column of a specific sheet, offset and clear the contents of a specific cell. However it only clears the first occurrence of this search and I would like to have the code work on all occurrences. Can someone help me to wrap a Loop or a FindNext around this code because I wasn't able to. Please see here below the code I already have. Thnx
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
On Error Resume Next
Worksheets(2).Columns("A:A").Select
Set Action6 = Selection.Find(What:=SearchValue6, After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Action6 Is Nothing Then
'MsgBox "No clearings made in " & ActiveWorkbook.Name
Else
Action6.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.ClearContents
End If
Please, try using the next updated code and send some feedback:
Sub FindMultipleTimes()
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
Dim ws As Worksheet: Set ws = Worksheets(2)
Dim firstAddress As String
Set Action6 = ws.Columns("A:A").Find(What:=SearchValue6, After:=ws.Range("A1"), LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Action6 Is Nothing Then
firstAddress = Action6.address
Do
Action6.Offset(0, 1).ClearContents
Set Action6 = ws.Columns("A:A").FindNext(Action6) 'find the next occurrence
Loop While Action6.address <> firstAddress
Else
MsgBox SearchValue6 & " could not be found in column ""A:A"" of sheet " & ws.name
End If
End Sub
I only adapted your code, but do you want letting the workbook necessary to extract SearchValue6 value, open?
I need to find a string that will be always on the Row 3 but any column in a specific sheet, after the code find it the application GoTo should take the user to this column. However, I am having trouble with my code, maybe someone knows what I am doing wrong?
Sub Pivot1()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "Nr. 1"
Application.FindFormat.Clear
With Sheets("Analysis").Cells(3, Columns.Count).End(xlToLeft).Column
Set cl = .Find(What:=SearchString, After:=Range("A:FA"), LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cl Is Nothing Then Application.Goto cl
End With
End Sub
I know selection is not good to use. However, I need to find a string in another sheet and need it to be selected (it can even select and change the color) so the user will able to see it.
My code is only taking me to the sheet but not to the cell where the string I need to find is.
Sub Risk1()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "1."
Application.FindFormat.Clear
Sheet2.Activate
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
FirstFound = cl.Activate.cell
End If
Next
End Sub
The string that I am looking for can be in any cell of column A.
Try this. If it's in one sheet no need to loop through all of them. It's still not clear to me what happens if the value is found more than once?
Btw you don't need VBA for this, the worksheet Find will do exactly this.
Sub Risk1()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "1."
Application.FindFormat.Clear
With Sheets("Over").Columns(1)
Set cl = .Find(What:=SearchString, After:=.Cells(1, 1), LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cl Is Nothing Then Application.Goto cl 'better than select or activate and sheet does not need to be active first
End With
End Sub
so I am trying to copy a value from one workbook into another, and keep getting syntax compilation errors. If anyone knows why it would be very helpful
Sub findsomething()
Dim rng As Range
Dim account As String
Dim rownumber As Long
Dim dehyp As Long
dehyp = Replace(Range("A5").Value, "-", "")
account = Sheet.Cells(dehyp)
Set rng = sheet1.List-of-substances-in-the-third-phase-of-CMP-(2016-
2021).xlsx.Columns("A:A").Find(What:=account,
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
rownumber = rng.Row
Sheet1.Cells(2, 2).Value = Sheet1.List-of-substances-in-the-third-
phase-of-CMP-(2016-2021).xlsx.Cells(rownumber,
3).Value
End Sub
Cell A5 contains
numbers with hypens such as 279-01-2.
but to be searchable in the other document needs to be in the form of 279012
Some of your code is unclear, but it would be something more like:
Sub findsomething()
Dim rng As Range
Dim account As String
Dim rownumber As Long
Dim dehyp As Long
Dim wb As Workbook
dehyp = Replace(Range("A5").Value, "-", "") '<< be more specific here about workbook/sheet
account = Sheet.Cells(dehyp) '<< and here
Set wb = Workbooks.Open( _
"L:\PRS\CEPA\Chemicals Management Plan\!Overviews and Summaries\" & _
"List-of-substances-in-the-third-phase-of-CMP-(2016-2021).xlsx")
Set rng = wb.Sheets("sheet1").Columns(1).Find(What:=account, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If not rng is nothing then
thisworkbook.sheets("Sheet1").Cells(2, 2).Value = _
wb.Sheets("sheet1").Cells(rng.Row, 3).Value
End If
End Sub
This would be tidier as a Vlookup though.
I am trying to move the first instances of each value of the column "firstname" from one sheet to another. I am able to move the first instance of the first cell value from the firstname column, but I am not able to move the next and so on till the end. Below is the code I am working with at the moment.
Is there any problem with the for loops?
Option Explicit
Private Const RAW_DATA_SHEET = "UserDetails"
Private Const REPORT_SHEET = "Report"
Private Const USER_NAME_COL = "User Name"
Private Const FIRST_NAME_COL = "First Name"
Private Const LAST_NAME_COL = "Last Name"
Private Sub CommandButton1_Click()
Dim firstname As String
Dim firstnameActive As Range
Dim sheet As Worksheet
Dim sheetre As Worksheet
Dim userNameHeader As Range
Dim firstnameHeader As Range
Dim userToFindRow As Range
Dim user As String
Dim curcell As Range
Application.ScreenUpdating = False
Set sheetre = Sheets("Report")
Set sheet = Sheets("UserDetails")
For Each firstnameHeader In sheet.Range("B2" & sheet.Range("B" & sheet.Cells.Rows.Count).End(xlUp).row)
Set curcell = sheetre.Range("A3")
' Find the columns of "User Name"
Set userNameHeader = sheet.Rows(1).Find(What:=USER_NAME_COL, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'note: SearchOrder:=xlByColumns
Set firstnameHeader = sheet.Rows(1).Find(What:=FIRST_NAME_COL, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'note: SearchOrder:=xlByColumns
' Use to find the row number of the provided user
Set userToFindRow = sheet.Columns(userNameHeader.Column).Find(What:=user, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False) 'note: SearchOrder:=xlByRows
For Each userToFindRow In sheet.Range("E2" & sheet.Range("E" & sheet.Cells.Rows.Count).End(xlUp).row)
' Get the value of "firstname" cell for provided user
Set firstnameActive = sheet.Cells(userToFindRow.Column, firstnameHeader.Column)
If Not IsEmpty(firstnameActive) Then
curcell.Value = firstnameActive
Set curcell = curcell.Offset(1)
End If
Next userToFindRow
Next firstnameHeader
Application.ScreenUpdating = True
End Sub
why the loop is not working?
Sathak, I think you need to change your For Each Loop. Syntax of For each is we tend to search in defined range. You need to add :B & :E in both for each range to make loop work.
Check the modified code and try to use this For Each loop in your code:
First For each loop:
For Each firstnameHeader In sheet.Range("B2:B" & sheet.Range("B" & sheet.Cells.Rows.Count).End(xlUp).Row)
Second For each loop:
For Each userToFindRow In sheet.Range("E2:E" & sheet.Range("E" & sheet.Cells.Rows.Count).End(xlUp).Row)