Formula inserted in VBA produces the wrong results.

myfathersson

New Member
Joined
May 4, 2010
Messages
22
Well here's one for the books.

I have a VBA routine that inserts a formula into a cell. When I step through the routine the formula produces the correct results, but when I run the routine without stepping through it, incorrect results are produced.

The formula is designd to sum the values in a specific cell across multiple sheets. The sheets are named with prefixes identifying the job category. In Excel, if you enter a wildcard formula as follows, Excel replaces the wildcard with a range of sheet names.
Code:
=sum('Prefix*'!k22)

For example the formula
Code:
=sum('SWS(I)-*'!k22)

will cause Excel to produce a formula something like
Code:
=sum('SWS(I)-BJones:SWS(I)-SRogers, SWS(I)-JBloggins'!k22)

Which is what I get when I step through the routine. But when I run the routine I get
Code:
=sum('SWS(I)-JBloggins'!k22)

Here's my code.
Code:
  Private Sub UpdateSumTSandByCatSheets(oDic As Object)

    Dim lstoByCatTable As ListObject, rwoTableRow As ListRow, arrKeys As Variant, idx As Integer, strFormula As String
    Application.Calculation = xlCalculationManual

    'Update the summary_timesheet
    For Each c In wbTemplate.Worksheets("summary_timesheet").Range("hoursworkedblock")
        strFormula = WorksheetFunction.Substitute(c.Formula, "#REF", "'*-*'")
        c.Formula = strFormula
    Next c
    
    'Update the by_category sheet
    'Instantiate table object and table row object
    Set lstoByCatTable = wbTemplate.Worksheets("by_category").ListObjects("ByCatTable")
    Set rwoTableRow = lstoByCatTable.ListRows(1)
    'Dump the key values from oDic into an array.
    arrKeys = oDic.keys
    
    'the ByCatTable in the by_category sheet in the template has one blank row by default.  Fill in this first row with the first
    'item from oDic.  Note: the oDic key is the long form category name and the item value is the short form.
    'The value put in the Days column of the ByCatTable is a formula set to sum all the Total Days (cell k22)
    'from all sheets with a prefix of a given short cat name.
    rwoTableRow.Range(1, BYCATTABLECATCOL).Value = arrKeys(0)
    strFormula = "=sum('" & oDic.Item(arrKeys(0)) & "-*'!k22)"
    rwoTableRow.Range(1, BYCATTABLEDAYSCOL).Formula = strFormula
    
    'Loop through the oDic key array, adding and filling in table rows for each item in the array.
    For idx = 1 To UBound(arrKeys) 'we start at 1 because item 0 (the first item) was stored previously.
        Set rwoTableRow = lstoByCatTable.ListRows.Add 'add a new table row at the bottom of the table
        rwoTableRow.Range(1, BYCATTABLECATCOL).Value = arrKeys(idx) 'put the long cat name in the Category column
        rwoTableRow.Range(1, BYCATTABLEDAYSCOL).Formula = "=sum('" & oDic.Item(arrKeys(idx)) & "-*'!k22)" 'put the sum formula in the Days column.
    Next idx

    Application.Calculation = xlCalculationAutomatic
    
End Sub

Things I've tried:
- using application.wait to slow down the process thinking the auto calc process is too slow to keep up with the program.
- turn off auto calc before the formula generation then turn it back on again after all is complete.
- building the formula in a string first before using it in the statement to insert the formula.

Some Background to explain the why's and the wherefores:
My company has recently implemented an online time tracking system. Our clients each require timesheets to be provided in the billing package we send them; these sheets must be in the format specified by the client. The time tracking system does not produce the timesheets in the required format so I wrote a program that uses a CSV dump of the tracking system to generate the timesheets in the required format. The timesheets are all put into one workbook (one workbook for each client) and in the case here, the workbook also contains summary sheets the have sum formulas to sum across multiple sheets. It is these cross-sheet sum formulas that I'm trying to generate to sum the sheets in stored in the workbook. Note: the names and number of timesheet worksheets varies from month to month, which is the reason I need to regen the formula each time.

Any ideas as to what is going on? Anyone see any gotchyas in the code or anywhere?

Of course this is the last routine I had to write...it was all going so well.
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I think you have tried all the possible solutions but you need to check again the code if any tag is remained open. Also check if there is any overwriting. Sometimes these small things tend to give errors even in a correct formula or code.....
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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
Back
Top