简体   繁体   中英

Using Index Match In excel VBA

Dim DateValues As Variant
Dim Shortfall As Variant
Dim Region As Variant
Dim Bucket As Variant

Dim Shortfall_rng As Range
Dim Region_rng As Range
Dim Bucket_rng As Range
Dim Datevalues_rng As Range

Dim estring As Variant

Dim StartCopying As Range


Set StartCopying = Worksheets("Asia_All").Range("O4")

Region = Worksheets("Asia_All").Range("O1").Value ' Value in cell O1 is "New York" (without Quotes)
Bucket = Worksheets("Asia_All").Range("O2").Value ' Value in cell O2 is "<!--1-->0 - 1" (without Quotes)
DateValues = Worksheets("Asia_All").Range("O3").Value ' Value in cell O3 is "2007-01-02" (without Quotes)



Set Shortfall_rng = Worksheets("Asia_All").Range("$B$2:$B$142411")
Set Bucket_rng = Worksheets("Asia_All").Range("$F$2:$F$142411")
Set Datevalues_rng = Worksheets("Asia_All").Range("J$2:$J$142411")
Set Region_rng = Worksheets("Asia_All").Range("$K$2:$K$142411")


estring = Evaluate("Index(" & Shortfall_rng.Address & ", Match(" & Bucket & Region & DateValues & ", " & Bucket_rng.Address & " & " & Region_rng.Address & "&" & Datevalues_rng.Address & ", 0))")

StartCopying.Value = estring

I am trying to use Index Match ,but I am getting #Value! error. Please note that it works absolutely fine if i manually enter the index match formula in the cell. This error appears only when i try to replicate it in excel vba.To give a brief,

Bucket_rng column contains a coulmn of multiple values of

 <!--1-->0 - 1
 <!--2-->1 - 2.5
 <!--3-->2.5 - 5
 <!--4-->5 - 10
 <!--5-->10+

Shortfall_rng column contains a coulmn of multiple values like

-4.69
-3.94
-53.94
150.18
-18.78
-9.98
-17.04
-41.32
0.0001

Datevalues_rng column contains a coulmn of multiple values in yyyy-mm-dd format

and

Region_rng column contains a coulmn of multiple values like

China
New York
Australia
Japan Continent

You'd have to enclose the string arguments in quotes inside the formula. It's easier, IMO, to use the cell addresses of the criteria cells too:

Dim ws                    As Worksheet
Dim DateValues            As Range
Dim Shortfall             As Range
Dim Region                As Range
Dim Bucket                As Range
Dim Shortfall_rng         As Range
Dim Region_rng            As Range
Dim Bucket_rng            As Range
Dim Datevalues_rng        As Range
Dim estring               As Variant
Dim StartCopying          As Range

Set ws = Worksheets("Asia_All")

Set StartCopying = ws.Range("O4")

Set Region = ws.Range("O1")    ' Value in cell O1 is "New York" (without Quotes)
Set Bucket = ws.Range("O2")    ' Value in cell O2 is "<!--1-->0 - 1" (without Quotes)
Set DateValues = ws.Range("O3")    ' Value in cell O3 is "2007-01-02" (without Quotes)



Set Shortfall_rng = ws.Range("$B$2:$B$142411")
Set Bucket_rng = ws.Range("$F$2:$F$142411")
Set Datevalues_rng = ws.Range("J$2:$J$142411")
Set Region_rng = ws.Range("$K$2:$K$142411")


estring = ws.Evaluate("Index(" & Shortfall_rng.Address & ", Match(" & Bucket.Address & "&" & Region.Address & "&" & DateValues.Address & ", " & Bucket_rng.Address & " & " & Region_rng.Address & "&" & Datevalues_rng.Address & ", 0))")

StartCopying.Value = estring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM