Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3485606
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:55:40+00:00 2026-05-18T10:55:40+00:00

I have this formula in a cell: =GetData(Channel_01,Chicago) Which executes this code: Public Function

  • 0

I have this formula in a cell:

=GetData("Channel_01","Chicago")

Which executes this code:

Public Function GetData(ChannelCode As String, Key As String) As String
    Dim sql As String
    Dim cmd As New ADODB.Command
    Dim outputTo As Range
    Set outputTo = Application.Caller    
    sql = "select * from ChannelData WHERE ChannelCode = ? AND Key1 = ?"
    Set cmd = getCommand(sql, ChannelCode, Key)
    Dim rs As ADODB.Recordset
    Set rs = cmd.Execute
    WritePivotRecordset ChannelCode, rs, outputTo.Offset(1, 0)
End Function

Public Sub WritePivotRecordset(ChannelCode As String, rs As ADODB.Recordset, destination As Range)
    Dim i As Integer
    '*** WHEN THIS LINE OF CODE IS REACHED AND EXECUTES, PROCESSING STOPS
    Set destination.Value = ChannelCode
    For i = 1 To rs.Fields.Count - 1    'skip first field
        destination.Offset(0, i).Value = rs.Fields(i).Name
    Next
    destination.Offset(1, 0).CopyFromRecordset rs
End Sub

The problem occurs on this line:

'*** WHEN THIS LINE OF CODE IS REACHED AND EXECUTES, PROCESSING STOPS
    Set destination.Value = ChannelCode

Is setting this invoking a spreadsheet recalc, which terminates the executing VBA thread or something like that? I thought so, so I tried this before writing any output:

Application.Calculation = xlCalculationManual

But now on that same line of code I get:
Application-defined or object-defined error.

Is writing from a VBA function to the same worksheet from which the VBA function is called, just not allowed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:55:40+00:00Added an answer on May 18, 2026 at 10:55 am

    This is just the built-in behavior of Excel. Functions called from the worksheet (often called UDFs in Excel terminology – user-defined functions) can’t do anything to the worksheet other than return a value.

    In your code above, there appears to be another error, though.

    Set destination.Value = ChannelCode 
    

    should fail because you’re using the syntax for setting an object variable to an object reference. If you had an error handler in there it would catch the error. (Excel just terminates any UDF that lets an error go unhandled.) The line should be:

    destination.Value = ChannelCode 
    

    However, your routine will still fail at that point because of the rule about UDFs not having side effects on cells. Note that even if you did have an error handler, it wouldn’t catch that. VBA doesn’t raise an error when a UDF tries to modify a cell; it just stops the UDF and returns a #VALUE! error.

    In your case, it looks like you can rewrite your function to return an array containing the values you want, instead of trying to modify cells to the right and below of the calling cell. (Or you can call your function from some macro instead of a UDF.)

    EDIT:

    As far as returning the array, there is an ADO method – GetRows – that will return one from a RecordSet:

    code critique – am I creating a Rube Goldberg machine?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 0,4*A1 in a cell (as a string). How can convert this string
I have this code in jQuery, that I want to reimplement with the prototype
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
This one has me stumped. When I set the formula for a selected cell
In excel 2007, I have a formula in a cell like the following: =COUNTIFS('2008-10-31'!$C:$C;>=&'$A7)
I have this SphereInFrustrum function here: 0.49% int FrustumG::sphereInFrustum(Vec3 &p, float radius) { int
I have this idea for a free backup application. The largest problem I need
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.