+ Reply to Thread
Results 1 to 8 of 8

Macro to Fill with Formula, but Hide Formula to User

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Red face Macro to Fill with Formula, but Hide Formula to User

    I would like to populate the F column with the following formula pulling from the same row in column E, but cant have the formula visible to the user. How can I create a macro to have the corresponding value fill in for the data set? Should I use a loop?

    Any help is much appreciated.



    Formula: copy to clipboard
    =IF(E2="BLANK","BLANK",IF(E2<6,"< 6 Days",IF(AND(E2>5,E2<11),"6-10 Days",IF(AND(E2>10,E2<16),"10-15 Days",IF(AND(E2>15),">15 Days")))))


    Thank you,

    Nick

  2. #2
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Macro to Fill Column with Formula

    I would like to fill column F with the resulting value from the following formula:

    Formula: copy to clipboard
    =IF(E2="BLANK","BLANK",IF(E2<6,"< 6 Days",IF(AND(E2>5,E2<11),"6-10 Days",IF(AND(E2>10,E2<16),"10-15 Days",IF(AND(E2>15),">15 Days")))))


    The only catch is that the formula cannot be visible from the worksheet.

    Thank you for taking a look.

  3. #3
    Forum Contributor Jack7774's Avatar
    Join Date
    08-31-2012
    Location
    Georgia, US
    MS-Off Ver
    Excel 2010
    Posts
    576

    Re: Macro to Fill Column with Formula

    I am new here but i think you don't want the formula to be seen if the cell E2 is blank right? If yes then change your formula to this

    =IF(E2="","",IF(E2<6,"< 6 Days",IF(AND(E2>5,E2<11),"6-10 Days",IF(AND(E2>10,E2<16),"10-15 Days",IF(AND(E2>15),">15 Days")))))

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    What do you mean the formula can't be visible from the worksheet?
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    09-06-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    225

    Re: Macro to Fill Column with Formula

    Norie,

    In column F I want the values created by the formula to show. When you click on the cell, I want the value to show up in the function bar, rather than the formula being visible. Any ideas on how to make a macro perform this?

  6. #6
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Macro to Fill Column with Formula

    you could just protect the cell and hide the formula then protect the sheet
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Macro to Fill Column with Formula

    So you want the code to put the formula in then replace it with values?
    With Range("F2", Range("E" & Rows.Count).End(xlUp).Offset(, 1))
        .Formula = "=IF(E2=""BLANK"",E2,LOOKUP(E1,{0,6,10,15},{""<6"",""6-10"",""10-15"","">15""})&"" Days"")"
        .Value = .Value
    End With

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,993

    Re: Macro to Fill with Formula, but Hide Formula to User

    There are two approaches to this. One is to use a loop to provide the result of this formula to all cells in column F, one time. This is only a good idea if you know the data in column E will not change. Another way is to detect any time the data in column E changes, and then update the cell in column F. Maybe you even need both; here they are.

    Option Explicit
    
    ' Provide value in column F for all values in column E
    Public Sub test()
    
       Dim r As Long
       
       For r = 2 To Cells(Rows.Count, "E").End(xlUp).Row
          Cells(r, "F") = ResultValue(Cells(r, "E"))
       Next r
       
    End Sub
    
    ' Update the value in column F any time a value in column E changes
    Public Sub Worksheet_Change(ByVal Target As Range)
    
       If Target.Column = 5 Then ' column E
          Cells(Target.Row, "F") = ResultValue(Target)
       End If
    
    End Sub
    
    Public Function ResultValue(InValue As Variant) As String
    
          If InValue = "BLANK" Then
             ResultValue = "BLANK"
          ElseIf InValue < 6 Then
             ResultValue = "< 6 Days"
          ElseIf InValue > 5 And InValue < 11 Then
             ResultValue = "6-10 Days"
          ElseIf InValue > 10 And InValue < 16 Then
             ResultValue = "10-15 Days"
          ElseIf InValue > 15 Then
             ResultValue = ">15 Days"
          Else
             ' What happens if there is a bad/unexpected value in column E?
             ResultValue = "???"
          End If
    
    End Function
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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