简体   繁体   中英

VBA VLookup isn't finding a successful match

I am loading 2 csv input files into new workbook and then using the values in MDAAccountList to search for a record in AssetAllocExt using application.Vlookup. When the code runs and does the Vlookup part is returns Error 2042 and cannot find a match. When I write the same Vlookup values as a normal formula it find a successful match and returns values. I have used the same code previously without any issues so not sure if its a data format issue. I have included data examples below.

MDAAccountList

AccountNumber
334324
364438
494757
496819
444165
444608
558425
450246
517853

AssetAllocExt

AccountNumber   Australian Equities Australian Equities %   Global Equities Global Equities %
334324  290980.26   0.3703  115411.3    0.1469

364438  121314.12   0.2496  50278.12    0.1034

494757  345521.5    0.294   226973.46   0.1931


Public Sub LoadMDAAccountList()

Dim WB As Workbook
Dim SourceWB As Workbook
Dim WS As Worksheet
Dim ASheet As Worksheet
Dim LastRow As Long

'Turns off screenupdating and events:
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlManual

'Sets the variables:
Set WB = ActiveWorkbook
Set ASheet = ActiveSheet
Set SourceWB = Workbooks.Open("C:\test\MDAAccountList.csv", local:=True)

'Copies each sheet of the SourceWB to the end of original wb:
For Each WS In SourceWB.Worksheets
    WS.Copy after:=WB.Sheets(WB.Sheets.Count)
Next WS

SourceWB.Close SaveChanges:=False
Set WS = Nothing
Set SourceWB = Nothing

WB.Activate
ASheet.Select
    Set ASheet = Nothing
    Set WB = Nothing

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

'-------------

Public Sub LoadAssetAllocationExtract()

Dim WB As Workbook
Dim SourceWB As Workbook
Dim WS As Worksheet
Dim ASheet As Worksheet
Dim LastRow As Long

'Turns off screenupdating and events:
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlManual

'Sets the variables:
Set WB = ActiveWorkbook
Set ASheet = ActiveSheet
Set SourceWB = Workbooks.Open("C:\test\AssetAllocationExtract.csv", local:=True)

'Copies each sheet of the SourceWB to the end of original wb:
For Each WS In SourceWB.Worksheets
    WS.Copy after:=WB.Sheets(WB.Sheets.Count)
Next WS

SourceWB.Close SaveChanges:=False
    Set WS = Nothing
    Set SourceWB = Nothing

WB.Activate
ASheet.Select
    Set ASheet = Nothing
    Set WB = Nothing

Application.DisplayAlerts = False

Sheets("AssetAllocationExtract").Activate
ActiveSheet.Name = "AssetAllocExt"

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

'-------------

Public Sub CheckAssetAllocation()

Dim PortfolioAustEq As String
Dim PortfolioGlobEq As String
Dim PortfolioAustEqFound As Variant
Dim PortfolioGlobEqFound As Variant

Dim UniqueReference As String
Dim SelectedPortfolio As String

Dim Lookup_Range As Range

Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlManual

Sheets("MDAAccountList").Activate
Range("A2").Activate
Range("A2").Select

Do
  SelectedPortfolio = ActiveCell.Value

  Set Lookup_Range = Range("AssetAllocExt!A:O")

  UniqueReferenceFound = Application.VLookup(SelectedPortfolio, Lookup_Range, 1, 0)
  PortfolioAustEqFound = Application.VLookup(SelectedPortfolio, Lookup_Range, 3, 0)
  PortfolioGlobEqFound = Application.VLookup(SelectedPortfolio, Lookup_Range, 5, 0)

Loop Until IsEmpty(ActiveCell.Value)

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

You can find a more detailed explanation here about the issue you are having, but long stort short, making the change @CLR suggested is what you need.

Meanwhile, see below the

...
Dim SelectedPortfolio As Long 'or change the correct datatype
...

In essence, what you are trying to do in the code is something like this:

=VLOOKUP(TEXT(SelectedPortfolio, "0"), Lookup_Range, 1, 0)

Which will not work in the spreadsheet either.

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