I have Sheet1 which is a form with fields where we enter data to be fed in the database (Sheet2).
Ideally, here's what I want it to do:
I want to search a field/record using the form contents in Sheet1, then search for that term on Sheet2. If it doesn't exist on Sheet2, give me a pop up message saying data doesn't exist.
If it does exist in Column A on Sheet2, then select the cell to the right of the result (Column B). Then paste that cell's contents in relevant fields on Sheet1
Then continue until all of the fields on Sheet1 has been searched for on Sheet2.
Here's the code I've been using. It only works for about 5 lines before it comes up with an error. Any help would be greatly appreciated.
I really don't want the MsgBox to pop up at all.
Sub abc()
Do Until IsEmpty(ActiveCell)
Dim MyString As String
MyString = ActiveCell
Sheets("Sheet2").Select
Set RangeObj = Cells.Find(What:=MyString, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If RangeObj Is Nothing Then MsgBox "Not Found" Else: RangeObj.Select
ActiveCell.Offset(0, 1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(-1, -1).Select
Loop
End Sub
Please let me know what I'm doing wrong.
You were on the right path.. just a few amendments to how you were copying the data.
Option Explicit
Sub sheet2_lookup()
Dim strVal As String
Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet, RangeObj As Range
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
Cells(1, 1).Activate '' amend this to your starting cell
While ActiveCell.Value2 <> ""
strVal = ActiveCell.Value2
Set RangeObj = ws2.Columns("A").Find(What:=strVal, After:=Cells(1, 1), LookIn:=xlValues)
If RangeObj Is Nothing Then
MsgBox "Not Found"
Else
ActiveCell.Offset(0, 1).Value2 = RangeObj.Offset(0, 1).Value2
End If
ActiveCell.Offset(1, 0).Activate
Wend
End Sub
Let me know how it goes.
Related
I want to go into a sheet and look for a value. If the value is there I want to grab the data in its row and paste transposed in another sheet. This function is working.
However, if the value in the column is not there I want to paste filler text into the column where the data would otherwise go.
I am getting a "Type Mismatch" error when I run the following code. What is going wrong/ how can I made this happen.
Dim c As Long
Windows("WBGrab").Activate '-> opens doc we want to look a
c = Sheets("SheetName").Columns(2).Find(What:="Commercial Income", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
If c Is Nothing Then
Windows("WBPaste").Activate
Range("C2:C7").Value = "-"
Else
Sheets("SheetName").Range("C" & c & ":H" & c).Select '-> opens MBI DSCR sheet and copes naming & values
Selection.Copy '-> copies the selected area
Windows("WBPaste").Activate '-> opens back up the data lake
Sheets("Sheet1").Range("C" & 2).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=True
End Sub
If Find() fails to make a match then your code will error before getting to If c Is Nothing Then because of the .Row tagged onto the Find() line.
Something like this should work:
Sub Tester()
Dim c As Range, wsSrc As Worksheet, wsDest As Worksheet, cDest As Range
'set up source and destination sheets
Set wsSrc = Workbooks("WBGrab").Worksheets("SheetName") 'add the file extension to the name
Set wsDest = Workbooks("WBPaste").Worksheets("Sheet1")
Set c = wsSrc.Columns(2).Find(What:="Commercial Income", LookIn:=xlValues, _
LookAt:=xlPart, MatchCase:=False)
Set cDest = wsDest.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) 'next paste destination. C2?
If c Is Nothing Then 'didn't make a match with Find() ?
cDest.Resize(1, 7).Value = "-" 'fill placeholders
Else
c.Offset(0, 1).Resize(1, 7).Copy 'got match - copy range
cDest.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End If
End Sub
I need my macro to Look at a cell in my range, Find that value in the a different WS and paste a value on to that's next to the value i'm looking for (my original WS). do this again and again to the values in the range.
now it all works but for some reason the value is stuck on the first search and wont look for other values in the original range.
here is my code, and the pictures should help.
Sub Macro1()
'
'now im gonna match the "UDD" TO THE "S/O"
Worksheets("Sheet1").Activate
Range("c17").Select
Dim Searchkey As Range, cell As Range
Set Searchkey = Range("c17:c160")
For Each cell In Searchkey
Sheets("data").Activate
Cells.Find(What:=Searchkey, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(0, -1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, -1).Range("A1").Select
Selection.Copy
Next cell
End Sub
why is my macro stuck on "84225" and not looping to the other S/O?
Thank you
Sub mac1()
Worksheets("Sheet1").Activate
Range("c17").Select
Dim srch As Range, cell As Variant
Set srch = Range("c17:c160")
For Each cell In srch
Sheets("data").Activate
Cells.Find(What:=cell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Offset(0, -1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, -1).Range("A1").Select
Selection.Copy
Next cell
End Sub
this is working!
thank you all
On each loop you're searching for the whole range of SearchKey and not just Cell so I'm guessing it's always using the first cell in SearchKey as your search criteria.
You're also searching in the formula rather than the values, and looking for a part match which may return incorrect results (part match on 20 would return a find in 20, 201, 11120001, etc).
Not qualifying your sheet names and using Activate probably isn't helping much either.
Try this code:
Public Sub Test()
Dim SrcSht As Worksheet, TgtSht As Worksheet
Dim SearchKey As Range, Cell As Range
Dim FoundValue As Range
With ThisWorkbook
Set SrcSht = .Worksheets("Sheet1")
Set TgtSht = .Worksheets("Data")
End With
Set SearchKey = SrcSht.Range("C17:C21")
For Each Cell In SearchKey
'Search column 3 (C) for your the value
Set FoundValue = TgtSht.Columns(3).Find(What:=Cell, _
After:=TgtSht.Columns(3).Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
'Only proceed if value found, otherwise an error will occur.
If Not FoundValue Is Nothing Then
Cell.Offset(, 1) = FoundValue.Offset(, 1)
End If
Next Cell
End Sub
Edit:
To test the code place the cursor within the procedure and press F8 to process each line in turn. The FoundValue should contain a value each time it's executed.
To check this hover your cursor over the variable to see its value:
The row highlighted in yellow is the next line that will be executed. If FoundValue is nothing then the following line isn't processed, if it's not nothing then the line is executed.
'Declare Variables
Dim rng As Range
Dim AmQ As Worksheet, UpIss As Worksheet, QuMa As Worksheet
'Set your sheet names into variables for easier referencing
Set AmQ = Sheets("AMEND QUOTE")
Set UpIss = Sheets("UPISSUE")
For i = 7 To 57 '7 = Column H, 8 = Column G, etc.
'Set the address of the found value to the rng variable
Set rng = UpIss.Cells.Find(What:=AmQ.Cells(4, i).Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then 'CHECK IF THE SEARCH TERM (FROM QUOTE SHEET) WAS FOUND IN THE TARGET SHEET (ESTIMATE)
If Not rng = "" Then 'CHECK IF THE SEARCH TERM WAS A ZERO LENGTH STRING
rng.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(14, 0)).Copy 'Copy the cell 41 rows down and 3 columns across
'QuMa.Cells(4, i).Offset(14, 0).PasteSpecial Paste:=xlPasteValues 'Paste into the cell 14 rows below the original search term in the QUOTE sheet
Sheets("QUOTE MASTER").Select
Range("C5000").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
'Copy Formats
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True
'Copy Values
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'Autofit
Columns("C:Q").EntireColumn.AutoFit
Application.CutCopyMode = False
ElseIf rng = "" Then 'EXIT SUB IF SEARCH TERM WAS A ZERO LENGTH STRING
'MsgBox "Work is Done"
Exit Sub
End If
ElseIf rng Is Nothing Then 'EXIT SUB IF SEARCH TERM WAS NOT FOUND IN THE TARGET SHEET
'MsgBox "Work is Done"
Exit Sub
End If
Set rng = Nothing
Next i 'Move to the next column across and loop
This is where I am getting my error:
rng.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(14, 0)).Copy
not able to copy in the loop.
I have a Excel workbook (lets call serial_numbers) that contains a list of S/N on A1 to A10 (can be more or less).
Now I have to search for A1's value on workbook "database". That value is usually found in A1 cell of workbook "database".
In case that I find A1's valueI need to copy and paste B2's value of workbook "database", which cointains the current stock of that value.
Through Developer mode on Excel I got the following result:
Sub Macro1()
'
' Check stock for S/N in database
'
'
Range("A1").Select
Selection.Copy
Workbooks.Open Filename:="database.xlsx", _
UpdateLinks:=0
Range("A1").Select
Selection.Find(What:="XXXXXX", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range("B1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("serial_numbers.xlsx").Activate
Range("B1").Select
ActiveSheet.Paste
End Sub
That piece above seems to be not working properly and since Im new to this I cant get why. Can any of you help me?
If you are looking for non-vba solution then you may go for vlookup function.
Try this code :
Sub testMacro()
Dim wkbDB As Workbook
Dim rngFind As Range, rngSearch As Range, cell As Range
Dim shtDB As Worksheet
Set wkbDB = Workbooks.Open(Filename:="C:\database.xlsx", UpdateLinks:=0)
Set shtDB = wkbDB.Sheets("Sheet1")
Set rngSearch = ThisWorkbook.Sheets("Sheet1").Range("A1:A10")
For Each cell In rngSearch
Set rngFind = shtDB.Range("A1:A10").Find(What:=cell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows)
If Not rngFind Is Nothing Then
'serial_numbers.xlsx
cell.Offset(0, 1) = rngFind.Offset(0, 1)
End If
Next
End Sub
I need a find function to search in Column B of my worksheet titled "Quote Sheet" to find "Profit Adjustment" and it would be nice if it was case sensitive. Below is the code I am working with but I can't get the range or the wording correct. Any help is appreciated.
Dim rFound As Range
Set rFound = Range("B10:B1000")
Cells.Find(What:="Profit Adjustment", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True).Activate
ActiveCell.Offset(0, 8).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sub samPle()
Dim rFound As Range, cellToFind As Range
Set rFound = Sheets("Quote Sheet").Range("B10:B1000")
Set cellToFind = Cells.Find(What:="Profit Adjustment", MatchCase:=True)
If Not cellToFind Is Nothing Then
cellToFind.Activate
ActiveCell.Offset(0, 8).Copy ActiveCell.Offset(0, 8)
End If
End Sub
I would re-write your example like this:
Sub copy_paste_example()
Dim c As Range
With ActiveWorkbook.Sheets("Quote Sheet")
Set c = .Columns("B").Find(What:="Profit Adjustment", _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=True)
On Error GoTo NotFoundErr:
c.Offset(0, 8).Value = c.Value
End With
Exit Sub
NotFoundErr:
Debug.Print "value not found"
End Sub
Notes:
You weren't ever using the rfound Range Object so I removed it.
Activate and Select are not needed, and may even slow down your code.
Remember that Find returns a Range Object which can be useful later on in your code.
It wasn't immediately clear to me whether you only want to find the first occurrence of Profit Adjustment, or if you care about all occurrences. If you want to find all rows in Column B that contain Profit Adjustment, the below Macro will work as-is. If you want to find only the first occurrence, then simply uncomment the line that says Exit For.
Here's the code:
Sub FindValueInColumn()
Dim rowCount As Integer, currentRow As Integer, columnToSearch As Integer
Dim searchString As String
Dim quoteSheet As Worksheet
Set quoteSheet = ActiveWorkbook.Sheets("Quote Sheet")
searchString = "Profit Adjustment" 'string to look for
columnToSearch = 2 '2 represents column B
rowCount = quoteSheet.Cells(Rows.Count, columnToSearch).End(xlUp).Row
For currentRow = 1 To rowCount
If Not Cells(currentRow, columnToSearch).Find(What:=searchString, MatchCase:=True) Is Nothing Then
Cells(currentRow, 8).Value = Cells(currentRow, columnToSearch).Value
'uncomment the below "Exit For" line if you only care about the first occurrence
'Exit For
End If
Next
End Sub
Before search:
After search: