+ Reply to Thread
Results 1 to 11 of 11

Calling a Sub from Functiuon to Change Values in Active Sheet

  1. #1
    Registered User
    Join Date
    11-21-2022
    Location
    India
    MS-Off Ver
    2013
    Posts
    7

    Calling a Sub from Functiuon to Change Values in Active Sheet

    Hello Friends!!

    I am using following code to create a header row at "Cell_ref" Location.

    Public Function Pressure_Drop(Cell_ref As String)
    'Generates Header Row for Pressure Drop Calculations
    'Dim Cell_ref As String
    'Cell_ref = "A2"
    Pressure_Drop = "Pressure Drop Calculation as per Crane Technical Paper"
    Evaluate "Pressure_Drop_Header(" & Cell_ref & ")"
    End Function

    Public Sub Pressure_Drop_Header(Cell_ref As String)
    Dim Cell1 As Range
    Set Cell1 = Range(Cell_ref)
    Cell1.Value = "Fitting Type"
    Cell1.Offset(0, 1).Value = "Fluid"
    Cell1.Offset(0, 2).Value = "Inlet Pressure, in Pa"
    Cell1.Offset(0, 3).Value = "Pipe Material"
    Cell1.Offset(0, 4).Value = "NPS Unit"
    Cell1.Offset(0, 5).Value = "NPS"
    Cell1.Offset(0, 6).Value = "Schedule"
    Cell1.Offset(0, 7).Value = "Ft, Friction Factor for NPS Crane"
    Cell1.Offset(0, 8).Value = "K, Pressure Drop Factor"
    Cell1.Offset(0, 9).Value = "f, Friction Factor"
    Cell1.Offset(0, 10).Value = "Head Loss hL, in m"
    Cell1.Offset(0, 11).Value = "Pressure Drop, in Pa"
    Cell1.Offset(0, 12).Value = "Outlet Pressure, in Pa"
    Columns("A:Z").AutoFit
    End Sub


    On calling Function from worksheet like:

    =Pressure_Drop("A2")

    I get error in value. Can somebody help please.

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    This is, IMO, a bad idea, but you need more quotes:

    Please Login or Register  to view this content.
    Rory

  3. #3
    Registered User
    Join Date
    11-21-2022
    Location
    India
    MS-Off Ver
    2013
    Posts
    7

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    Thanks Rory!!

    But what could be a better way to do this. I want to generate a header on my sheet. That too on call. Cant add a button beforehand to execute sub.

    May be you can suggest some better way to code it.

    Thanks once again.
    Last edited by AnkitTiwari; 01-31-2023 at 07:59 AM.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    A UDF (a function procedure called from a spreadsheet cell) cannot change the spreadsheet other than to return a value to the cell(s) it is called from. It cannot call another procedure that changes the spreadsheet.

    It appears that your intention is to use a single function to fill a contiguous range (row) of cells. A UDF can return an array without much difficulty. I describe the strategy for returning an array from a UDF here: https://www.excelforum.com/excel-pro...ml#post4071488

    Did I understand the problem correctly?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    Quote Originally Posted by MrShorty View Post
    A UDF (a function procedure called from a spreadsheet cell) cannot change the spreadsheet other than to return a value to the cell(s) it is called from. It cannot call another procedure that changes the spreadsheet.
    Actually, it can. One way is using the Evaluate trick in this thread. It is not though, to my mind, a good idea.

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    Quote Originally Posted by AnkitTiwari View Post
    But what could be a better way to do this.
    I don't know without knowing why you think a formula is the best option for this.

  7. #7
    Registered User
    Join Date
    11-21-2022
    Location
    India
    MS-Off Ver
    2013
    Posts
    7

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    Quote Originally Posted by rorya View Post
    I don't know without knowing why you think a formula is the best option for this.
    If not formula then, what could be the other way round to fill cells with some prefixed data.

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    You didn't really answer my question. So:
    1. Why can't you use a button?
    2. Why does it need to be done in code?
    3. What made you decide that a formula was necessary? Given that the column headers are static, why can't you just type them in the cells in the first place?

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    Is there a reason you could not use a named array constant -- like this example using the names of months defined as "Quarter1" -- https://support.microsoft.com/en-us/...b-25e898b265f5 That would completely bypass any need to use VBA in favor of built in Excel features.

  10. #10
    Registered User
    Join Date
    11-21-2022
    Location
    India
    MS-Off Ver
    2013
    Posts
    7

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    Quote Originally Posted by MrShorty View Post
    Is there a reason you could not use a named array constant -- like this example using the names of months defined as "Quarter1" --
    Thanks Mr Shorty!

    It was exactly the thing I was looking to do but, was not aware of doing it this way. Very Thanks.

  11. #11
    Registered User
    Join Date
    11-21-2022
    Location
    India
    MS-Off Ver
    2013
    Posts
    7

    Re: Calling a Sub from Functiuon to Change Values in Active Sheet

    Quote Originally Posted by rorya View Post
    You didn't really answer my question. So:
    1. Why can't you use a button?
    2. Why does it need to be done in code?
    3. What made you decide that a formula was necessary? Given that the column headers are static, why can't you just type them in the cells in the first place?
    Hi Rorya,

    1. Using a button: well I can but I wanted to call it like we do call functions from spreadsheet
    2. I have written the code which worked well when it was in sub but, as soon as I convert it to function it stopped working. One problem was with quotes, you rightly pointed.
    3. Yes I can, but then I have written some data validation code for following rows using a button. So just went about inserting header too with a function.

    Should I post you the code, I am still working on some issues with data validation there.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to change a fixed sheet reference in Consolidate method to active sheet?
    By aries6776 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-10-2020, 10:57 AM
  2. [SOLVED] Change sheet1 to Active.Sheet in VBA
    By burrjc in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-09-2019, 03:45 PM
  3. Calling list of values from another sheet
    By turkanet in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-27-2019, 09:32 AM
  4. Active Sheet use Drop Down Change
    By putritersenyum in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-09-2015, 08:36 PM
  5. [SOLVED] change values of active x checkbox within VBA
    By tmallin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-03-2014, 04:30 PM
  6. Change to Active Sheet in Macro
    By uberathlete in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-12-2014, 05:43 PM
  7. Change name to active sheet
    By Jerseynjphillypa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-30-2012, 08:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1