Microsoft Excel - Numbers won't sum

Asked By Nancy Ziese
03-Nov-09 12:57 PM

I am using a VBA to query and fill a query table with data from another excel workbook.  I am having problems with a field name Hours which is defined as a number with 2 decimal places in both workbooks.  When I use the sum function on that particular column it always sums at 0.00.  It is as if the cells are being seen as text instead of numbers.  Any ideas?

This is a piece of my code:

 bdate = Range("begin").Value
   edate = Range("end").Value
   fdate = 1 / 1 / 2009
   Sheets("Calc SP").Select
    Application.Goto Reference:="CalcSPClearArea"
    Selection.ClearContents
   wherestatus = " AND ((`Query_from_fipe109#ffdc`.STATUS IN ('COMPLETE', 'AWAIT ADDS') AND `Query_from_fipe109#ffdc`.STATUS_DATE Between #" & bdate & "#" & " And #" & edate & "#)"
   orstatus = " OR (`Query_from_fipe109#ffdc`.STATUS IN ('IN PROCESS') AND `Query_from_fipe109#ffdc`.STATUS_DATE Between #" & fdate & "#" & " And #" & edate & "#));"
   wherejobtype = " where `Query_from_fipe109#ffdc`.JOB_TYPE = 17"
   Set cnn = New ADODB.Connection
   Set cmd = New ADODB.Command
   Set rs = New ADODB.Recordset
   strDbPath = "I:\Map_Folders\NZ170Y\Metrics\Reporting\CurrentData.xlsx"
   cnn.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0;" & "Data Source=" & strDbPath & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=Yes;"""
   cnn.Open
   Set cmd.ActiveConnection = cnn

   cmd.CommandText = "Select `Query_from_fipe109#ffdc`.JOB_NO AS JOB_NO, `Query_from_fipe109#ffdc`.CLIENT AS CLIENT, `Query_from_fipe109#ffdc`.HOURS_WRKD AS HOURS, `Query_from_fipe109#ffdc`.CLLI AS CLLI,`Query_from_fipe109#ffdc`.OPERATOR_ID AS OPERATOR, `Query_from_fipe109#ffdc`.STATUS AS STATUS   from `I:\Map_Folders\NZ170Y\Metrics\Reporting\CurrentData.xlsx`.`Query_from_fipe109#ffdc`" & wherejobtype & wherestatus & orstatus
   Set rs = cmd.Execute
   Set qt = ActiveSheet.QueryTables.Add(Connection:=rs, Destination:=ActiveSheet.Range("A13"))
   qt.Refresh  

 rs.Close
   cnn.Close
   Set rs = Nothing
   Set cnn = Nothing

 

  Jonathan VH replied to Nancy Ziese
03-Nov-09 01:44 PM

That interesting code aside, it sounds as though column C of your destination sheet may be formatted as Text before the query result set gets dropped in. If you turn on the Number stored as Text warning, do these values get flagged?

response  response

03-Nov-09 01:56 PM
No  and  the column is formated as number with 2 decimal places and is still formated the same after the query that brings the data in is run.  The formating is the same in the workbook from which the data comes.

reponse  reponse

03-Nov-09 01:59 PM
It also displays in the correct format.
  Jonathan VH replied to Nancy Ziese
03-Nov-09 02:20 PM

Do you format the column's cells before or after the query result is dropped in? If after, format them before.

Does the + operator work with the values? I.e. if you use the addition operator on a few of the values rather than the SUM function, is the result still zero? =C14+C15+C16

reply  reply
03-Nov-09 02:28 PM

The column is formated before it is populated.

Oddly enough, =C17+C18+C21+C22+C23+C24 works

=SUM(C17:C24) does not

I cannot use the + operator since I don't know how many rows I will have in the column when it is populated.

  Jonathan VH replied to Nancy Ziese
03-Nov-09 03:22 PM

This sounds like the same behavior I mentioned the other day on this thread:

http://www.eggheadcafe.com/community/aspnet/66/10125942/re-sum-values-with-symbo.aspx

My guess is that the values have some characteristic (embedded spaces?) that cause them to be non-numeric. I recommend that you experiment with the values to discover and fix this characteristic of the query values (perhaps try adding IMEX=1 or IMEX=0 to the extended properties of the connection string). Alternatively you could force the values to numeric after they're created, or use a solution like I posted in that thread.

reply  reply
03-Nov-09 06:17 PM

Using your suggestions I cannot find anything that works. 

 I did discover something interesting. 

When I select one of the cells in the column and then click on the formula bar where the value is displayed

 then click out of it, the value becomes a number.  

Not sure why or how I can use this to change the data type for the entire range.  Have any ideas?

  Jonathan VH replied to Nancy Ziese
03-Nov-09 06:35 PM

Yes, you are causing the text values to be reevaluated as numbers. You can do that by double-clicking the cells or using F2 (edit) Enter.

If you cannot get the query results to be numeric, then you can change them en masse after they are dropped in by selecting the range and choosing Text to Columns (on the Data ribbon) and then clicking Next, Next, Finish on the dialog boxes.

THANK You  THANK You
03-Nov-09 06:39 PM
That works and it will be easy.  Thank you.
Create New Account
help
Remplacer_"A2"_par_la_première_cellule_vide Excel Excel - French Discussions ADODB.Connection (1) ADODB.Command (1) Error (1) Excel (1) Macro (1) ADODB (1) VBA (1) MoveNext (1) Bonjour. Essaie C'est ici que = E7a bloque End With End Sub (Merci, Denys) 'Must have the Microsoft Activex Data Objects 2.0 Library 'in the tools- -- -reference- --in the VBA section Sub _ srcRange As String, _ TTL As Boolean, _ outArr As Variant) Dim myConn As ADODB.Connection, myCmd As ADODB.Command Dim HDR As String, myRS As ADODB.Recordset, RS_n As Integer, RS_f As Integer
following code that adds a record to an Access database: Sub ADOFromExcelToAccess() Dim cn As ADODB.Connection, rs As ADODB.Recordset Set cn = New ADODB.Connection cn.Open "Provider = Microsoft.Jet.OLEDB.4.0; " & _ Set rs = New ADODB.Recordset rs.Open "CurrentPMT", cn, adOpenKeyset existing record instead of adding a new one. Thanks in advance, MikeS Excel Programming Discussions ADODB.Connection (1) ADODB.Command (1) ADODB.Recordset (1) ADOFromExcelToAccess (1) Excel (1) CurrentPMTScore (1) PRICE (1) ADODB (1
ADODB connection string problem I cannot for the life of me get my connection string working, can is access code. Private Sub Command4_Click() Dim rs As ADODB.Recordset Dim con As New ADODB.Connection Dim cmd As New ADODB.Command Set con = "" cmd.ActiveConnection = con cmd.CommandText = "Production.SyncJobCompleteTest" cmd.CommandType = adCmdStoredProc Set rs = cmd to believe. . . PLEASE HELP, I am new at this. . . Try this: Dim conConnection As New ADODB . Connection conConnection . ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & _ App . Path & " \ " & "database1.mdb
Adodb Connection Sir / Madam, I want to know the steps to create the adodb connection in visual basic 6.0. Please can u help me out; Please give any simple example to me if u can possible. Thanking u. hi here is Dim cn as ADODB.Connection Set cn = New ADODB.Connection With cn .Provider = "Microsoft.Jet.OLEDB.4.0" .ConnectionString = "Data Source = C: \ MyFolder \ MyWorkbook.xls
ADODB.Connection to Teradata Timesout DataBase Hi All, I am running Teradata queries from MS Access using a ADODB.Connection. The queries run directly unsing Teradata in about 3 Min. I cannot keep the ADODB connection from timing out before three minutes. The error message is: I guess that I do not understand the ODBC reference on a ADODB.Connection. Anyway: I have other quicker queries that run fine. How do I keep from timing