Skip to content

Commit 42347bc

Browse files
authored
Update README.md
1 parent 8fe5376 commit 42347bc

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

README.md

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -346,27 +346,48 @@ End Sub
346346
With VBA CSV interface, many things can be done, for example, an user can perform like SQL joins such as:
347347

348348
```
349-
Sub JoinTwoTables()
350-
Dim WB As Workbook
351-
Dim WS As Worksheet
352-
Dim t1 As CSVArrayList
353-
Dim t2 As CSVArrayList
354-
Dim arrT1() As Variant
355-
Dim arrT2() As Variant
349+
Sub JoinTwoCSV()
350+
Dim csv1 As CSVinterface
351+
Dim csv2 As CSVinterface
356352
Dim rTable As CSVArrayList
357353
358-
Set WB = ThisWorkbook
359-
Set WS = WB.Sheets("Orders"): arrT1() = WS.Range("A1:G21").Value2
360-
Set WS = WB.Sheets("Ships and sales"): arrT2() = WS.Range("A1:F27").Value2
361-
Set t1 = New CSVArrayList: t1.items = arrT1
362-
Set t2 = New CSVArrayList: t2.items = arrT2
363-
' Join 1st, "Region", and 3th to 5th fields of left table with "Total_Revenue" field from the right table,
364-
' on "Order_ID" of both tables and Total_Revenue, from the right table, is greater than 3000000
365-
' and Region, from the left table, is equal to "Central America and the Caribbean"
366-
Set rTable = t1.LeftJoin(t1, t2, _
367-
"{1,Region,3-5};{Total_Revenue}", _
368-
"Order_ID;Order_ID", _
369-
"t2.Total_Revenue>3000000 & t1.Region='Central America and the Caribbean'")
354+
'@--------------------------------------------------
355+
' Import data from CSV files
356+
Set csv1 = New CSVinterface
357+
With csv1
358+
.parseConfig.delimitersGuessing = True
359+
.parseConfig.path = Environ("USERPROFILE") & "\Desktop\Sales details.csv"
360+
.ImportFromCSV .parseConfig
361+
End With
362+
Set csv2 = New CSVinterface
363+
With csv2
364+
.parseConfig.delimitersGuessing = True
365+
.parseConfig.path = Environ("USERPROFILE") & "\Desktop\Sales revenues.csv"
366+
.ImportFromCSV .parseConfig
367+
End With
368+
'@--------------------------------------------------
369+
' Perform a like SQL Left join on imported data.
370+
With csv1.items
371+
' Join 1st, "Region", and 3th to 5th fields of left table with "Total_Revenue" field from the right table,
372+
' on "Order_ID" of both tables if Total_Revenue is greater than 3000000
373+
' and Region is equal to "Central America and the Caribbean"
374+
Set rTable = .LeftJoin(csv1.items, csv2.items, _
375+
"{1,Region,3-5};{Total_Revenue}", _
376+
"Order_ID;Order_ID", _
377+
"t2.Total_Revenue>3000000 & t1.Region='Central America and the Caribbean'")
378+
379+
' The "*" symbol can be used to retrieve all fields
380+
' from a given table like this.
381+
382+
'Set rTable = .LeftJoin(csv1.items, csv2.items, _
383+
"{*};{Total_Revenue}", _
384+
"Order_ID;Order_ID", _
385+
"t2.Total_Revenue>3000000 & t1.Region='Central America and the Caribbean'")
386+
387+
End With
388+
'@--------------------------------------------------
389+
' Write the result in a spreadsheet.
390+
csv1.DumpToSheet DataSource:=rTable
370391
End Sub
371392
```
372393

0 commit comments

Comments
 (0)