VBA Sub returns #VALUE! after data connection refresh

ggeisen

New Member
Joined
Apr 12, 2013
Messages
5
I have a VBA Sub that accepts a Range from a table that is connected to a data connection to Sharepoint. The Sub sums all values in a column whose cell height <> 0. (unfiltered rows) When I open the file, or do a manual data connection refresh, the Sub returns #VALUE!. If I double click the formula cell (to edit it), then hit Return, the cell computes correctly. (Clicking Calculate Now does not have an effect).

When I hover my mouse over the cell, the error hint is, "A value used in the formula is the wrong data type"

Debugging ...
The Sub simply loops through the cell range and sums the cells with a height greater than zero. I believe the range that is passed in is invalid for some reason or has no rows immediately after a data connection refresh. The Sub is called automatically after the data connection refresh. I break and watch the range and Cell.row values at each iteration. The range shows 100 rows, but the cell.row value does not change and the loop exits after three iterations. it looks like the cell values are invalid: inspecting cell.Height in the loop shows, "<Unable to the the Height property from the Range class>".

If I perform the same debugging steps after the previously mentioned edit to invoke the Sub calculation, everything works fine, including the cell.row value incrementing at each iteration.

Interestingly, replacing the Sub call with the internal SUM() function works fine after a refresh, except w/o the needed functionality :(.

Can anyone suggest a fix, or explain why this is happening? The cell value and Sub are included below....



Cell Value: =SumVis(A1:A100)
Function SumVis(r As Range)
Dim cell As Excel.Range
Dim total As Variant
For Each cell In r.Cells
If cell.Height <> 0 Then
total = total + cell.Value
End If
Next
SumVis = total
End Function
 

Some videos you may like

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
ggeisen,

Welcome to the MrExcel forum.

Your Function is not returning a value. The following works in my test environment (see the BOLD):


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel

Rich (BB code):
Option Explicit
Function SumVis(r As Range) As Variant
Dim cell As Excel.Range
Dim total As Variant
For Each cell In r.Cells
  If cell.Height <> 0 Then
    total = total + cell.Value
  End If
Next
SumVis = total
End Function

You did not say what the format of range A1:A100 was.

If it is dollars and cents, then change this:

Rich (BB code):
Function SumVis(r As Range) As Variant

To this:

Rich (BB code):
Function SumVis(r As Range) As Double
 
Last edited:

ggeisen

New Member
Joined
Apr 12, 2013
Messages
5
Your Function is not returning a value. The following works in my test environment (see the BOLD):

Rich (BB code):
Function SumVis(r As Range) As Variant

Adding As Variant or As Double has no effect. I suspect it is implied, although not in best form. The code will likely still work in your test environment w/o the As clause. Note that I mention the Function works fine if it is invoked manually, or even typed in elsewhere on the sheet. It returns the summation expected, except after a Refresh.

I still suspect the Range passed in immediately after the Refresh may not be valid by indication of the data values inspected in the debugger. I do not know why this would be :(

The data type for A1:A100 is monitary.
 
Last edited:

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
ggeisen,

So that we can get it right the next time:

You can upload your workbook to Box Net,
sensitive data scrubbed/removed/changed
mark the workbook for sharing
and provide us with a link to your workbook.


If you are not able to provide your workbook/worksheet:

Click on the Reply to Thread button, and just put the word BUMP in the post. Then, click on the Post Quick Reply button, and someone else will assist you.
 

ggeisen

New Member
Joined
Apr 12, 2013
Messages
5

ADVERTISEMENT

@hiker95,

I can surely upload a sanitized workbook, but you won't have access to the data connection, which is where I think the problem is stemming from. The Function works fine until it is acting upon a table that has a data connection.
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
ggeisen,

We could change your Function into a macro.

What is the worksheet name, and what is the cell that you want the result sum in?

You could then call the macro name from the end of the code that does the download of the information.
 

ggeisen

New Member
Joined
Apr 12, 2013
Messages
5

ADVERTISEMENT

Interesting development .... and work around

I created several other simple Functions. They all return #VALUE! after a Data Connection Refresh if they operate (have parameters) on the table that has the data connection to Sharepoint. Cells that call Functions which do not operate on the data connected table work fine.

I've discovered a work-around:

Place Application.Volatile in all the Functions that are used to operate against the data connected table and press F9 after opening the file, or after any Data Connection Refresh.
Code:
Function SumVis(r As Range) As Double
Application.Volatile

My guess is the workbook recalculation is occurring too soon after the external data connection refresh completes and "regenerates" the table. That may explain why the Functions cannot operate on the table.
 

ggeisen

New Member
Joined
Apr 12, 2013
Messages
5
We could change your Function into a macro.

What is the worksheet name, and what is the cell that you want the result sum in?

You could then call the macro name from the end of the code that does the download of the information.

The worksheet name is 'KEY DATES', and the destination cell is H1. But I'm confused ... I don't have code that does the download. I've checked Refresh data when opening the file. from the Connection Properties dialog.
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
ggeisen,

The worksheet name is 'KEY DATES', and the destination cell is H1. But I'm confused ... I don't have code that does the download. I've checked Refresh data when opening the file. from the Connection Properties dialog

I am not familiar with the above.

Sample raw data after the download (not all 100 cells are shown for brevity):

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>H</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;">1</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style="text-align: right;;">2</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style="text-align: right;;">3</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style="text-align: right;;">4</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="text-align: right;;">5</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style="text-align: right;;">6</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style="text-align: right;;">7</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="text-align: right;;">8</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style="text-align: right;;">9</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style="text-align: right;;">10</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style="text-align: right;;">11</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style="text-align: right;;">12</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style="text-align: right;;">13</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">14</td><td style="text-align: right;;">14</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">15</td><td style="text-align: right;;">15</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">16</td><td style="text-align: right;;">16</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">17</td><td style="text-align: right;;">17</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">18</td><td style="text-align: right;;">18</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">19</td><td style="text-align: right;;">19</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">20</td><td style="text-align: right;;">20</td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:5.4em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">KEY DATES</p><br /><br />

After the download, then run the below macro, and we get this:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>H</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;">1</td><td style="text-align: right;;">5050</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style="text-align: right;;">2</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style="text-align: right;;">3</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style="text-align: right;;">4</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="text-align: right;;">5</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style="text-align: right;;">6</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style="text-align: right;;">7</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="text-align: right;;">8</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style="text-align: right;;">9</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style="text-align: right;;">10</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style="text-align: right;;">11</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style="text-align: right;;">12</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style="text-align: right;;">13</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">14</td><td style="text-align: right;;">14</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">15</td><td style="text-align: right;;">15</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">16</td><td style="text-align: right;;">16</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">17</td><td style="text-align: right;;">17</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">18</td><td style="text-align: right;;">18</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">19</td><td style="text-align: right;;">19</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">20</td><td style="text-align: right;;">20</td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:5.4em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">KEY DATES</p><br /><br />

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub MySumVis()
' hiker95, 04/12/2013
' http://www.mrexcel.com/forum/excel-questions/696747-visual-basic-applications-sub-returns-value-after-data-connection-refresh.html
Dim c As Range, rng As Range
Dim total
Set rng = Sheets("KEY DATES").Range("A1:A100")
For Each c In rng
  If c.Height <> 0 Then
    total = total + c.Value
  End If
Next c
Sheets("KEY DATES").Range("H1") = total
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the MySumVis macro.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,514
Messages
5,602,087
Members
414,501
Latest member
mdhaumyu

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