+ Reply to Thread
Results 1 to 11 of 11

Inputting Formulas into cells using VBA

Hybrid View

Foboman Inputting Formulas into cells... 12-28-2007, 03:51 PM
VBA Noob Foboman, Remember to wrap... 12-28-2007, 04:07 PM
Foboman VBA Noob, Thanks for the... 01-02-2008, 02:18 AM
VBA Noob Try this example Event... 01-02-2008, 05:14 AM
royUK Why would you need VBA to do... 01-02-2008, 05:55 AM
  1. #1
    Registered User
    Join Date
    12-20-2007
    Posts
    10

    Inputting Formulas into cells using VBA

    I'd like to figure out a way to 'insert' a formula into a specific range of cells using VBA only when the Value of $A$7 is changed to a value that does NOT equal "Custom".

    Heres what I have so far using Worksheet change event....

    Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Address = "$A$7" Then
            If Target.Value <> "Custom" Then
             Range("C13:C27").Formula = "=myfunction(A7)"
            End If
            End If
    End Sub
    As you can see above, I'd like to have '=myfunction(A7)' inputted as a forumla into some cells. Code above gets me into the second If statement, but none of the cells in my range update. Not certain I'm approaching it the right way. Any guidance is appreciated. Thanks. -FM
    Last edited by VBA Noob; 12-28-2007 at 03:58 PM.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Foboman,

    Remember to wrap your code next time.

    Is the trigger a formula or a by the user changing A7. If it's a user change it works for me

    Try adding this to the start
    application.EnableEvents = false
    and this to the end
    application.EnableEvents = True
    Also is the formula a custom User defined function ??

    If so it might be worth posting that code

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    12-20-2007
    Posts
    10
    VBA Noob, Thanks for the response. See bolded comments below.

    Is the trigger a formula or a by the user changing A7. If it's a user change it works for me

    -trigger is user-initiated. They will be selecting a 'string' from a drop down list in A7.

    Try adding this to the start
    application.EnableEvents = false
    and this to the end
    application.EnableEvents = True
    -added these lines. still dont see any formula populating the cells.

    Also is the formula a custom User defined function ??

    If so it might be worth posting that code

    The formula I want to ppulate the cell is a UDF. Its a simple Case statement function used to evaluate the contents of A7 See below.

    Function quicknetwork(quick1 As String)
    Select Case quick1
    Case "Show All Network Space Available"
    quicknetwork = "ALL"
    Case "Show All Network Space Utilized"
    quicknetwork = "ALL"
    Case "Show ALL Space Available"
    quicknetwork = "ALL"
    Case "Show ALL space Utilized"
    quicknetwork = "ALL"
    Case "Custom"
    quicknetwork = ""
    Case Else
    quicknetwork = "NONE"
    End Select
    End Function
    Let me know if you have any other ideas, suggestions.

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Try this example

    Event Macro Code
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
      If Target.Address = "$A$7" Then
            If Target.Value <> "Custom" Then
             Range("C13:C27").Formula = "=quicknetwork(A7)"
            End If
            End If
    Application.ScreenUpdating = True
    End Sub
    Module Code
    Function quicknetwork(quick1 As String)
    Select Case LCase(quick1)
        Case "show all network space available", _
             "show all network space utilized", _
             "show all space available", _
             "show all space utilized"
              quicknetwork = "ALL"
        Case "Custom"
              quicknetwork = ""
        Case Else
             quicknetwork = "NONE"
        End Select
    End Function
    Amended - Or this shorter code
    Function quicknetwork(quick1 As String)
    Select Case True
            Case LCase(quick1) Like "*show all*"
            quicknetwork = "ALL"
            Case LCase(quick1) Like "*custom*"
            quicknetwork = ""
            Case Else
            quicknetwork = "NONE"
        End Select
    End Function
    ThisWorksheet Module To turn on events
    Private Sub Workbook_Open()
        Application.ScreenUpdating = True
    End Sub
    VBA Noob
    Attached Files Attached Files
    Last edited by VBA Noob; 01-02-2008 at 06:12 AM.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Why would you need VBA to do this? An IF formula would work.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Registered User
    Join Date
    12-20-2007
    Posts
    10
    Thanks for the attachment. I fooled around with it a bit and think I found whats causing the problem. I failed to mention (hadn't thought it relevant), that my A7 cell uses a list to validate entry options. The user will not be 'typing' in values to change this cells contents, but instead, is selecting one from a list. When A7s contents is changed using listed drop down values, the cell range wont take a formula. If I remove the 'list', it all works perfectly. Is there any way to get this working with a list? See my edited attachment below.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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