Problem with SQL in VBA

czl103

New Member
Joined
Sep 27, 2018
Messages
19
After below code runs, the result is 310, however the result of VBA code should be 321.3, all the digits after the decimal points were ignored in calculation. why does this happens?
Pls download the 2 files from below site, and run the xlsm file after downloading....
Code:
[URL="https://www.mrexcel.com/forum/redirect-to/?redirect=https%3A%2F%2Fjmp.sh%2F71yC8Ti"]https://jmp.sh/71yC8Ti[/URL]

Thanks in advance.

Code:
Dim cnn As Object, SQL As String, s As String, p As String, f As String
Set cnn = CreateObject("ADODB.Connection")
p = ThisWorkbook.Path & "\"f = Dir(p & "*.CSV")
cnn.Open "Provider=Microsoft.ace.OLEDB.16.0;Extended Properties='text;FMT=Delimited(,);hdr=YES';Data Source=" & p
Do While f <> ""   SQL = SQL & " SELECT int(Date) AS T, Amount 
FROM [" & f & "] 
UNION ALL "    f = Dir()LoopSQL = Left(SQL, Len(SQL) - 11)
SQL = "SELECT T,SUM(Amount) FROM (" & SQL & ") 
GROUP BY T"Range("a2:e" & Rows.Count) = ""[a2].CopyFromRecordset 
cnn.Execute(SQL)
cnn.CloseSet 
cnn = Nothing
Dim R%R = Cells(Rows.Count, "a").End(xlUp).Row[a1].Offset(R, 0) = "Sum"[b1].Offset(R, 0).Resize(1, 4) = "=sum(b2:b" & R & ")"
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

czl103

New Member
Joined
Sep 27, 2018
Messages
19

ADVERTISEMENT

Now I found one method to solve this problem, change the format of the first row number to "0.00", then the result is ok.
But this is not clever way, is there any good idea?
 

strooman

Active Member
Joined
Oct 29, 2013
Messages
314
I got an error on the line
Code:
[a2].CopyFromRecordset cnn.Execute(SQL)
undefined function "subtotal" in expression


Where does subtotal(Amount) refer to?
The column Amount in your csv file is empty when I import the csv file. Is this correct?
When I import your csv data I get this. Is this the correct data?

Row\Col
A​
B​
C​
D​
E​
F​
G​
1​
Order NoDateCustomer 1 NoCustomer 1 NameCustomer 2 NoCustomer 2 Name Amount
2​
1,11E+02​
11-1-2017 21:00​
2,22E+02​
aaa
3,33E+02​
bbb -
3​
1,11E+02​
11-1-2017 20:00​
2,22E+02​
aaa
3,33E+02​
bbb -
4​
1,11E+02​
11-1-2017 20:00​
2,22E+02​
aaa
3,33E+02​
bbb -
5​
1,11E+02​
11-1-2017 20:00​
2,22E+02​
aaa
3,33E+02​
bbb -
6​
1,11E+02​
11-1-2017 20:00​
2,22E+02​
aaa
3,33E+02​
bbb -
7​
1,11E+02​
11-1-2017 20:00​
2,22E+02​
aaa
3,33E+02​
bbb -
8​
1,11E+02​
11-1-2017 20:00​
2,22E+02​
aaa
3,33E+02​
bbb -
9​
1,11E+02​
11-1-2017 20:00​
2,22E+02​
aaa
3,33E+02​
bbb -
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,346
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

You can use a Schema.ini file to specify data types for the columns, like this:

Code:
Sub Sum()
    Dim cnn As Object, SQL$, s$, p$, f$
    Set cnn = CreateObject("ADODB.Connection")
    p = ThisWorkbook.Path & "\"
    CreateSchemaFile p
    f = Dir(p & "*.CSV")
    cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties='text;';Data Source=" & p

    Do While f <> ""
        SQL = SQL & " SELECT int(Date) AS T, Amount FROM [" & f & "] UNION ALL "
        f = Dir()
    Loop
    SQL = Left(SQL, Len(SQL) - 11)
    SQL = "SELECT T,sum(Amount) FROM (" & SQL & ") GROUP BY T"
    [A2:E1048576] = ""
    [a2].CopyFromRecordset cnn.Execute(SQL)
    cnn.Close
    Set cnn = Nothing
    Dim R%
    R = [A1048576].End(3).Row
    [a1].Offset(R, 0) = "Sum"
    [b1].Offset(R, 0).Resize(1, 4) = "=sum(b2:b" & R & ")"
End Sub
Sub CreateSchemaFile(FilePath As String)
    If Right$(FilePath, 1) <> "\" Then FilePath = FilePath & "\"

    Dim IntFileHandleLog As Integer
    IntFileHandleLog = FreeFile
    
    On Error Resume Next
    Kill FilePath
    On Error GoTo 0
    
    Open FilePath & "schema.ini" For Output As #IntFileHandleLog
    
    Dim FileName As String
    FileName = Dir(FilePath & "*.csv")
    
    If FileName <> vbNullString Then
        Do
            Print #IntFileHandleLog, "[" & FileName & "]"
            Print #IntFileHandleLog, "ColNameHeader=True"
            Print #IntFileHandleLog, "MaxScanRows = 0"
            Print #IntFileHandleLog, "Col1=" & """Order No"" Text"
            Print #IntFileHandleLog, "Col2=" & """Date"" DateTime"
            Print #IntFileHandleLog, "Col3=" & """Customer 1 No"" Text"
            Print #IntFileHandleLog, "Col4=" & """Customer 1 Name"" Text"
            Print #IntFileHandleLog, "Col5=" & """Customer 2 No"" Text"
            Print #IntFileHandleLog, "Col6=" & """Customer 2 Name"" Text"
            Print #IntFileHandleLog, "Col7=" & """Amount"" Currency"
            FileName = Dir
        Loop While FileName <> vbNullString
    End If
    Close #IntFileHandleLog
End Sub
 
Last edited:

czl103

New Member
Joined
Sep 27, 2018
Messages
19
Pls refer to response with red text
Where does subtotal(Amount) refer to?
the G column

The column Amount in your csv file is empty when I import the csv file. Is this correct?
Yes,correct, some non-zero number do exist below these rows

When I import your csv data I get this. Is this the correct data?
yes correct data

Thanks

 

czl103

New Member
Joined
Sep 27, 2018
Messages
19
How to run? still run the "Sum", or should run the Schema.ini file first and then "Sum"?
 

Forum statistics

Threads
1,141,298
Messages
5,705,565
Members
421,399
Latest member
hjweiss00

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top