我正在将2个CSV输入文件加载到新工作簿中,然后使用MDAAccountList中的值使用application.Vlookup在AssetAllocExt中搜索记录。代码运行并执行Vlookup部分后,将返回错误2042,并且找不到匹配项。当我编写与常规公式相同的Vlookup值时,它将找到成功的匹配项并返回值。我以前使用过相同的代码,没有任何问题,所以不确定是否是数据格式问题。我在下面提供了数据示例。
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
答案 0 :(得分:0)
您可以找到关于您遇到的问题的更详细的说明here,但要花很长时间才能完成,建议您更改@CLR。
同时,请参见
...
Dim SelectedPortfolio As Long 'or change the correct datatype
...
本质上,您试图在代码中执行的操作是这样的:
=VLOOKUP(TEXT(SelectedPortfolio, "0"), Lookup_Range, 1, 0)
这在电子表格中也不起作用。