How to use a 'variable/string' within a MATCH Statement?

NRGZ

New Member
Joined
Jul 23, 2021
Messages
47
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. Mobile
  3. Web
I.... Think i'm understanding the MATCH statement correctly but how do i use a variable within a MATCH statement or am i trying to get the MATCH statement to do something it cant?
Initial Code:

Private Sub cntrct1()

Sheets("totals").Activate

Dim y As Integer
Dim z As Integer
Dim prod As String
Dim prod0 As String

y = WorksheetFunction.CountA(Worksheets("contracts").Range(Worksheets("contracts").Cells(2, 1), Worksheets("contracts").Cells(999, 1)))

prod = Sheets("contracts").Cells(2, 1)

Cells(2, 6) = WorksheetFunction.Match(prod.Value, Range(Worksheets("contracts").Cells(2, 1), Worksheets("contracts").Cells(y, 1), 0))



' check status only for testing
Cells(4, 10) = prod
Cells(4, 11) = y


End Sub

What i'm trying to do is retrieve the value of a cell where the the variable from 'totals' matches the variable in 'contracts', this would loop so until all variables have been matched from the 'totals' worksheet. (at the moment i'm trying to get it to match one and then i'll get to the rest)

Thanks In Advance,
o/
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Match returns a position, not a value. What cell do you want the value from?

Also, strings don't have any properties, so it would just be prod not prod.value, and you need to qualify the Range property in your Match line with the worksheet too.
 
Upvote 0
Match returns a position, not a value. What cell do you want the value from?

Also, strings don't have any properties, so it would just be prod not prod.value, and you need to qualify the Range property in your Match line with the worksheet too.
As i say i'm feeling my way with VB so MATCH is not what i'm wanting to use.

What i'm trying to do is retrieve the value of a cell where the the variable from 'totals' matches the variable in 'contracts'.
Source Value being searched for resides in 'contracts' cells(2, 3) and i want the value to 'show' in Cells(2, 6) in 'Totals'.

Thanks,
 
Upvote 0
That doesn't answer my question I'm afraid. You are searching for the value in column A, but which column do you want the result from?
 
Upvote 0
That doesn't answer my question I'm afraid. You are searching for the value in column A, but which column do you want the result from?
Sheet 'contracts'
Row 2
Column 3
 
Upvote 0
That really doesn't make sense. If you just want the value of a particular cell (C2), you can refer to it directly without using Match or any other lookup function.
 
Upvote 0
That really doesn't make sense. If you just want the value of a particular cell (C2), you can refer to it directly without using Match or any other lookup function.
Agreed but I want to retrieve the value of a cell depending upon the matching of 2 values.
Let me explain what i'm trying to do,
I am importing a number of CSVs, to separate sheets, that contain production information with the amount that is being ordered. Each sheet holds order information for a specific individual product.
A procedure grabs the product code, description and totals the quantity being ordered of that product and populates the TOTALS sheet with that information.
The CONTRACTS sheet holds information on individual products that we have 'contractual' quantity commitments for.
What i'm trying to do is take the 'value', product code, of a cell in the TOTALS sheet and IF we have a 'match' in the CONTRACTS sheet return the value of a cell containing what our contractual commitment for that product is. What the 'excel' side of things will do is then calculate what our remaining stock is.

I hope this paints a better picture.
Thanks,
(the attached image shows the layout of the TOTALS sheet)
 

Attachments

  • products.jpg
    products.jpg
    22.7 KB · Views: 11
Upvote 0
It sounds like you want something like this then:

Code:
With Worksheets("contracts")
   If WorksheetFunction.Countif(.Range(.Cells(2, 1), .Cells(y, 1)), prod) > 0 then Sheets("Totals").Cells(2, 6).Value = .Cells(2, 3).Value
End With

which basically says that if the value in prod appears in column A on Contracts, put the value of C2 on Contracts into F2 on the Totals sheet. Note that if you properly qualify your ranges with the relevant worksheet, you don't have to activate a specific sheet.
 
Upvote 0
It sounds like you want something like this then:

Code:
With Worksheets("contracts")
   If WorksheetFunction.Countif(.Range(.Cells(2, 1), .Cells(y, 1)), prod) > 0 then Sheets("Totals").Cells(2, 6).Value = .Cells(2, 3).Value
End With

which basically says that if the value in prod appears in column A on Contracts, put the value of C2 on Contracts into F2 on the Totals sheet. Note that if you properly qualify your ranges with the relevant worksheet, you don't have to activate a specific sheet.
That does work, sort of.
My code so far,
Private Sub cntrct1()

Sheets("totals").Activate

Dim y As Integer
Dim z As Integer
Dim prod As String
Dim prod0 As String

y = WorksheetFunction.CountA(Worksheets("contracts").Range(Worksheets("contracts").Cells(2, 1), Worksheets("contracts").Cells(999, 1)))

With Worksheets("contracts")

For z = 2 To y + 1


prod = Sheets("contracts").Cells(z, 1)

If WorksheetFunction.CountIf(.Range(.Cells(z, 1), .Cells(y, 1)), prod) > 0 Then Sheets("Totals").Cells(z, 6).Value = .Cells(z, 3).Value

Next z

End With

' check status only for testing
Cells(4, 10) = prod
Cells(4, 11) = y



End Sub

It works in a sequential manner but as you can see from the attached images its not actually pulling the correct value to TOTALS because its not identifying were it actually is.
example: Sheets 'TOTALS' Prod - 1491 = 90 were as 'CONTRACTS' prod - 1491 = 217
I feel like i'm getting out of my depth with knowing how to do this but i'm understanding how the coding your writing is working.
Thanks.
Totals0.jpgcontracts0.jpg
 
Upvote 0
What you are saying doesn't appear to correlate to what you actually want. You said you wanted the value in C2. Clearly that is not correct. The value you want is dependent on the row where the product is found, which is what I asked originally. Assuming that is the case, you do want Match since that will find the row position. My best guess currently would be something like:

Code:
Dim matchPos
with Worksheets("contracts")
 matchpos = Application.Match(prod, .Range(.Cells(2, 1), .Cells(y, 1)), 0) + 1
if not iserror(matchpos) then
Sheets("Totals").Cells(2, 6).value = .cells(matchpos, "C").value
end if
end with
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top