Hi All-
I have a problem I hope someone can help me with. In a nutshell, I'm trying to use data in an existing table (table1) to "look up" data in another table (table2) and summarize the data from table2 in table1. Here is an example:
table1:
<tbody>
</tbody>
table2:
<tbody>
</tbody>
So basically I want to loop through the FundName field of table1 and if they exist in table2, AND the dates match in both tables, then move the Amount field from table2 to table1 in the NAV field. I've used a recordset for things like this in the past, but I'm having trouble tonight (I'm tired and a little rusty). To be clear, I need a solution in VBA and I can NOT use a pivot table. Below is the code I'm working with:
I have a problem I hope someone can help me with. In a nutshell, I'm trying to use data in an existing table (table1) to "look up" data in another table (table2) and summarize the data from table2 in table1. Here is an example:
table1:
LoadDate | FundName | NAV |
3/8/13 | HY-EUR | |
3/8/13 | HY-GBP |
<tbody>
</tbody>
table2:
Load Date | FundDetailID | InputType | Amount |
3/7/13 | HY-EUR | NAV | 100 |
3/8/13 | HY-EUR | NAV | 101 |
3/8/13 | HY-GBP | P/L | 102 |
3/10/13 | HY-CAD | NAV | 103 |
<tbody>
</tbody>
So basically I want to loop through the FundName field of table1 and if they exist in table2, AND the dates match in both tables, then move the Amount field from table2 to table1 in the NAV field. I've used a recordset for things like this in the past, but I'm having trouble tonight (I'm tired and a little rusty). To be clear, I need a solution in VBA and I can NOT use a pivot table. Below is the code I'm working with:
Code:
Public Enum InputHedgeColumns
LoadDate = 1
FundName = 2
NAV = 3
End Enum
----------------------------------------------------------------
Sub TestHedgeUpdate()
Application.ScreenUpdating = False
Dim HedgeSheet, Input_All As Worksheet
Dim sInputData As ListObject
Set HedgeSheet = ThisWorkbook.Sheets("HedgeSheet")
Set Input_All = ThisWorkbook.Sheets("Input_All")
Set sInputData = [HedgeInputData].ListObject
Application.DisplayAlerts = False
Dim intCount As Integer
intCount = 2
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0 Macro;" & _
"HDR=YES"";"
.Open
''Process the Input Table - For each row of Input Data, apply the filter condition and store the result in the Output file
For Each Item1 In sInputData.ListRows
inputDataRow = Item1.Range.Value
sGlobalDate = [GlobalLoadDt]
''''''''''''Type mismatch error on next line'''''''''''''''''''''''''''''''''''''''''''''''''''
strQuery = "SELECT [Amount] FROM [InputAllTable]" _
& "WHERE [FundDetailID] IN ('" & inputDataRow(1, InputHedgeColumns.FundName) & ") " & _
"AND [LoadDate] = #" & sGlobalDate & "#" And [InputType] & " = 'NAV'"
With rs
.Open strQuery, cn, 3, 3
End With
HedgeSheet.Range("HedgeInputData[[NAV]]").CopyFromRecordset rs
rs.Close
Next
.Close
End With
Application.DisplayAlerts = True
End Sub