I have two sheets in my Excel workbook, one called Customer and the other called Product:
I wish to make a Lookup table in the Customer sheet, where in for example cell G2, above the lookup table, I put in the customer_id I wish to find all rows for in the sheet called Product. So, for example if I in G2 put in "1", I will get the two matching rows in sheet "Product". I have tried using VLOOKUP, but I always just match the first row, and none of the others.
EDIT:
in the lookup table you will only get the information in the sheet named "Product": if you put in "1" you will get rows 2 and 3 from the Product sheet. Customer_id only has one row per customer_id in the sheet called "customer", whereas in the sheet called "Product" you can have many rows per customer_id
Your source data for Pivot Table would be your Product Sheet:
Create a Pivot Table, and take Customer Field to filter section in the setup:
Just changing manually the number in Cell B2 will return the Product related To that customer id.
NOTE: If you input a Customer_id that is not in the Product Sheet, an alert Msgbox will pop up warning you about that. So if you see at any moment that alert, it means that customer_id has 0 records in PRoduct Sheet :)
Hope this helps
You could try Workbook_Change:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wsCus As Worksheet, wsPro As Worksheet
Dim LastRow As Long, ID As Long, i As Long
Dim arr As Variant
Dim FullRecord
With ThisWorkbook
Set wsCus = .Worksheets("Customer")
Set wsPro = .Worksheets("Product")
End With
If Not Intersect(Target, wsCus.Range("G2")) Is Nothing And Target.Count = 1 Then
ID = Target.Value
With wsPro
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
arr = .Range("A2:A" & LastRow)
For i = LBound(arr) To UBound(arr)
If arr(i, 1) = ID Then
If FullRecord = "" Then
FullRecord = i + 1
Else
FullRecord = FullRecord & ", " & i + 1
End If
End If
Next i
Application.EnableEvents = False
If FullRecord = "" Then
wsCus.Range("I2").Value = "No match found"
Else
wsCus.Range("I2").Value = "Matched lines for ID (" & ID & "): " & FullRecord
End If
Application.EnableEvents = True
End With
End If
End Sub
Instructions:
Once you have your workbook open press ALT & F11 to open VBA Editor.
Double click on the Customer sheet on the left upper part.
Select Worksheet.
Change event.
Paste the code as you see in the picture.
Then save the workbook as Excel Macro - Enabled Workbook, close VBA Editor and change G2 value.
Steps:
Results:
Find Match
No Match
Related
i have an Workbook with 5 Sheets where you can add Data while Using a Userform. The Data begins in each Sheet in row 10, since i nead the rows above 10 for my interface. I have also an Mastersheet, where the Data from these specific Sheets should be copied and should be edited if needed by DoubleClick and an Unserform. I Managed to write the Code for Copying the Data from these Sheets, but I also need the Sheetname next to the Data in Column A, so it's clear where the Data is from. The Data begins also in Row 10 of the Mastersheet.
1.Problem: Can't get the Sheet names in Column A and the Data to start in Column A.
2. If one Sheet is empty it Copies the Headers into the Mastersheet
3. By Double Click in the Row my Userform doesn't appear.
When i write The Sheet Name in Column B of the Mastersheet, the Data gets edited. But The Mastersheet should refresh itself when the editing is done
I would really appreciate if someone could help me, since i just startet with vba and don't know What to do.
`
Private Sub Worksheet_Activate()
Dim Ws As Worksheet
Me.Rows("6:" & Rows.count).Clear
Me.Range("A9:Z9").Value = Array("Markt".... (And So on)
For Each Ws In ThisWorkbook.Worksheets
If Ws.Name <> "Summary" And Ws.Name <> "Startseite" And Ws.Name <> "Agenda" (And so on) Then
Ws.Rows("10:" & Ws.Cells.Find("*", searchdirection:=xlPrevious).Row).Copy Me.Range("A" & Me.Cells.Find("*", searchdirection:=xlPrevious).Row + 1)
End If
Next Ws
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
Dim Sheet As String
Dim POS As Integer
Cancel = True
If Range(Target.Address).Row >= 10 Then
Sheet = Cells(Target.Row, 2).Value 'Get The Sheet Name from Column B I could change that bu I need the Sheet name in a Column to do that
If IsNumeric(Cells(Target.Row, 1).Value) Then
POS = CInt(Cells(Target.Row, 1).Value)
Else
POS = 0
End If
Else
Exit Sub
End If
If POS = 0 Then
Exit Sub
Else
Sheets("Code").Range("G2") = Sheet
Sheets("Code").Range("G3") = POS
End If
Edit.Show
End Sub
`
I tried changing the code but it didn't work
I am trying to create a Macro for inventory where essentially, Sheet 1 has all the information including barcode #, product name, and product size in rows. In Sheet 2 I have a button that, when clicked, allows me to scan a barcode of a product in my hand.
What I want is that when the barcode that I scan matches one of the barcodes in Sheet 1, all the other information pertaining to that product gets copied over into Sheet 2, or another sheet. I'm not sure which would be easier.
Right now the code that I have only adds some additional information next to the barcode that was matched. I'm not sure how to add that information as well as the original information from sheet 1 to a new sheet.
Private Sub CommandButton1_Click()
Dim MatchRow As Variant
Dim code As Variant
Dim matchedCell As Range
barcode = InputBox("Please scan a barcode and hit enter if you need to")
' verify that there is a successful match in the searched range
Set Shta = ActiveWorkbook.Worksheets("Sheet1")
Set Shtb = ActiveWorkbook.Worksheets("Sheet2")
Set Sheet1Range = Sheet1.Range("D2:D108")
If Not IsError(Application.Match(barcode, Sheet1Range, 0)) Then
MatchRow = Application.Match(barcode, Sheet1Range, 0) '<-- get the row number
MatchCatalog = MatchRow + 1
Set matchedCell = Range("D" & MatchCatalog) '<-- set the range (add 1 row since you are starting from row 2)
matchedCell.Offset(0, 2).Value = Now
matchedCell.Offset(0, 3).Value = MatchCatalog
matchedCell.Offset(0, 4).Value = barcode
'option 2: without setting the range
Range("AE" & MatchRow).Offset(1, 2).Value = Now
End If
End Sub
Thanks in advance!
Even some help on how to copy data from an entire row into a sheet would be helpful as a start.
You can import your information with a VLOOKUP or INDEX(MATCH()) depending on how your data is set up. You can just use the macro to only drop your barcode into a cell. This will be the look up value (first criteria) in your VLOOKUP
The below will put your barcode on A1
Private Sub CommandButton1_Click()
Dim Barcode
barcode = InputBox("Please scan a barcode and hit enter if you need to")
ThisWorkbook.Sheets("Sheet2").Range("A1") = Barcode
End Sub
I'm having a hard time trying to find a macro for the following use:
Taking in consideration this example:
Consider that i have in the "sheet 1" the table with the columns Country and Food with its values.
In the sheet 2, i have two columns named Country#1 and Food#1. The macro i want, needs to autofill the Food#1 cell that is associated with the right text in Country#1 cell, via the drop down list.
Example: When i select "Madrid" in Country#1, it needs to autofill the Food#1 with the text "Tapas and tortillas".
I'm sorry if this is a re-post question, but i didn't saw anything close as this :|
Best regards,
Luís
You need a Sheet Change Event like below...
The following code assumes that you have a list of Countries and their food in column A and B respectively on Sheet1 and the country dropdown list is in column A on Sheet2.
Right click the Sheet2 Tab --> View code --> Past the following code into the opened code window.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim wsSource As Worksheet
Dim r As Long
Set wsSource = Sheets("Sheet1") 'Source sheet which contains a table of countries and their food
If Target.Column = 1 And Target.Row > 1 Then
If Application.CountIf(wsSource.Columns(1), Target.Value) > 0 Then
Application.EnableEvents = False
r = Application.Match(Target.Value, wsSource.Columns(1), 0)
Target.Offset(0, 1) = wsSource.Cells(r, 2)
Application.EnableEvents = True
End If
End If
End Sub
I have created a generic excel file to help demonstrate what I'm looking to do. The file I've named Tool.xlsm contains two worksheets; Sheet1 and Sheet2. Sheet1 will be designed to have a few fields which will accept user input. Sheet2 will be hidden from the user, but will contain the various drop down list selection options and their corresponding descriptions which should be displayed in another cell on Sheet1 when a specific code is selected. Additionally, Sheet2 will contain numerous ID#s in one column, and their corresponding usernames in the next column. The purpose of this is for the user to be able quickly associate an ID# with user it belongs to.
Here is what I have so far...I doubt I'm going about it as efficiently as I should be, but I'd greatly appreciate your all's expertise!
Sub Button1_Click()
'Based on selected value of C1, show corresponding message in J1'
'Can this be done by simply referencing the code descriptions in sheet2?'
If Range("C1") = "code 1" Then
Range("J1") = "code 1 description"
End If
If Range("C1") = "code 2" Then
Range("J1") = "code 2 description"
End If
'End of code selection'
End Sub
Sub Button2_Click()
'Based on ID# entered into C3, display corresponding name in J1 (Sheet2 contains ID#s with corresponding names)'
'There has to be an esier way to loop through 1000s of records and display corresponding ID# and Person''s name'
'Rather than assigning Person 1, to Range J1, I should be able to just reference the cell Sheet2!E3 but that doesn''t seem to work'
If Range("C3") = "1001" Then
Range("J1") = "Person 1"
End If
If Range("C3") = "34349090" Then
Range("J1") = "Person 83"
End If
'End ID# search'
End Sub
Sub Button3_Click()
'Clear unlocked cells'
End Sub
my file in dropbox
To your queries:
Can this be done by simply referencing the code descriptions in sheet2?
Yes. You can use VLOOKUP formula for this.
Likewise, you could use the VLOOKUP formula to return the names based on the IDs.
E.g., assume your usernames are in column K and ID's in column J:
On sheet 1, assuming the ID in cell C3, enter the formula: =VLOOKUP(C3, Sheet2!$J$K, 2, False)
you can use the worksheet_change event. Kindly set rngFindCode & rngFindCode1 range accordingly to refer to your data in sheet2.
Below is the code.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Application.EnableEvents = False
If Target.Address = "$C$1" And Target.Cells.Count = 1 And Target.Value <> "" Then
Dim rngFindCode As Range '
Dim cellCode As Range
Set rngFindCode = Sheets("Sheet2").Range("C1:C100") ' Refers to range where code is in sheet 2
Set cellCode = rngFindCode.Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not cellCode Is Nothing Then
Range("J1").Value = cellCode.Offset(0, 1).Value
End If
ElseIf Target.Address = "$C$3" And Target.Cells.Count = 1 And Target.Value <> "" Then
Dim rngFindCode1 As Range '
Dim cellCode1 As Range
Set rngFindCode1 = Sheets("Sheet2").Range("E1:E100") 'Refers to range where name is
Set cellCode1 = rngFindCode1.Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not cellCode1 Is Nothing Then
Range("J1").Value = cellCode1.Offset(0, 1).Value
End If
Else
Range("J1").Value = ""
End If
Application.EnableEvents = True
End Sub
I have the following worksheet called Data:
In the same workbook I have another worksheet called Employee Database.
In Excel, how can I color the "Employee E-mail Address" and the corresponding "Company" and "Company URL" cells red from the Data worksheet if the "Employee E-mail Address" is not in the Employee Database?
In otherwords, I am trying to make the Employee Database worksheet look like this:
I've just given an example and in reality I have over 10,000 cells worth of data to do this to. I started doing this manually and realized it will take me forever.
I'd love to know if there is a macro that can do this in Excel?
Help would be so much appreciated! I have the example workbook of the screenshots above available for download here:
http://www.mediafire.com/?dttztp66dvjkzn8
You can do this without VBA, but it will require a slight change to the data on your Data sheet.
I don't recommend the "Pivot Table" or "Subtotal"-style of data storage in Excel, where you enter a primary key in one column only once then fill down associated data next to it until the next primary key.
Like merged cells, this will only lead to problems later when you want to re-organize your data.
Here's what I did:
Fill in missing email addresses on Data sheet
Highlight cells A2 all the way down column A to the end of the data in column B. So if you had company names in cells B2:B100, but only had emails from A2:A98, you should highlight A2:A100. This is because we are filling in the email address in each row of available data.
Go to Editing » Find & Select » Go To Special, select Blanks and click OK.
Now with blanks selected, type = ↑ (up arrow) , then press Ctrl+Enter. The blank cells in column A will fill in with the missing email addresses. Highlight column A, copy and paste values.
Create Dynamic Named Range for Emails
On the Employee Database sheet, create a named range called "Emails" with the following formula in the "Refers to" box:
=OFFSET('Employee Database'!$C$1,1,0,COUNTA('Employee Database'!$C:$C)-1,1)
Add Conditional Formatting
On the Data sheet, highlight A2:C whatever (ex: A2:C20000), then go to Home » Styles » Conditional Formatting and use the following formula:
=ISNA(MATCH($A2,Emails,0))
Select the color scheme you want and click OK. Here's how it looks on my computer with some sample data:
There are a few minor constraints:
You cannot leave column A blank on the Data sheet any more.
You cannot have blank rows on the Employee Database sheet in between rows of data. This is due to the way the dynamic range works.
Benefits
The benefits of this approach are, IMO, huge.
You can add or remove rows from the Employee Database sheet, and the highlighting will automatically adjust. Ex: if I add d#gmail.com and remove c#nbc.com, the formatting on the Data sheet updates immediately.
You don't have to alter your existing worksheet structure (other than filling in the missing data and adding a range name). No need for additional worksheets.
Your workbook can stay VBA-free (if it didn't have any already).
Is this what you are trying? This will create a new sheet "Desired Result" with the output. Paste this in a module.
Option Explicit
Sub Sample()
Dim wsData As Worksheet, wsDB As Worksheet, wsO As Worksheet
Dim lRow As Long, i As Long
Dim clrRng As Range
Set wsData = Sheets("Data")
Set wsDB = Sheets("Employee Database")
Set wsO = Sheets.Add
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Desired Result").Delete
Application.DisplayAlerts = True
On Error GoTo 0
With wsO
.Name = "Desired Result"
wsData.Cells.Copy .Cells
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 2 To lRow
If .Range("A" & i).Value = "" Then .Range("A" & i).Value = .Range("A" & i - 1).Value
Next i
For i = 1 To lRow
If Application.WorksheetFunction.CountIf(wsDB.Columns(3), .Range("A" & i).Value) = 0 Then
If clrRng Is Nothing Then
Set clrRng = .Rows(i)
Else
Set clrRng = Union(clrRng, .Rows(i))
End If
End If
Next i
If Not clrRng Is Nothing Then clrRng.Interior.ColorIndex = 3
For i = lRow To 2 Step -1
If .Range("A" & i).Value = .Range("A" & i - 1).Value Then .Range("A" & i).ClearContents
Next i
End With
End Sub