Consulting

Results 1 to 4 of 4

Thread: VBA for calculations

  1. #1
    VBAX Regular
    Joined
    May 2014
    Posts
    71
    Location

    VBA for calculations

    I have the below code:
    VB
     Sub Calculations()Dim iHomopolymer As Variant
    Dim Res As Variant
    Dim rData As Range
    
    
        With Worksheets("annovar")
        'set the range
            Set rData = .Cells(4, 1).CurrentRegion
            
            'search row 1 for the column number
            iHomopolymer = Application.Match("Homopolymer", rData.Rows(1), 0)
    
    
            Res = Application.VLookup(.Range("A2").Value, .Range("CA:CG"), 6, 0)
    
    
    
    
            If Not IsError(Res) Then
    
    
                Select Case Res
                    Case "Comprehensive Epilepsy"
                         .Cells(5, iHomopolymer).Value = "=IF(COUNTIFS(panel!B$2:B$6713,Q5,panel!C$2:C$6713,""<="" & R5,panel!D$2:D$6713, "">="" & R5),VLOOKUP(R5,panel!$C$2:$E$6713,3,1), ""No"")"
    
    
                    Case "Marfan Disorder"
                        .Cells(5, iHomopolymer).Value = "=IF(COUNTIFS(panel!B$25610:B$29333,Q5,panel!C$25610:C$29333,""<="" & R5,panel!D$25610:D$29333, "">="" & R5),VLOOKUP(R5,panel!$C$25610:$E$29333,3,1), ""No"")"
                End Select
            End If
            
    End With
    End Sub
    The user is supposed to make a selection in A2 and the value from that is used in a vlookup. Depending on the value returned to variable Res, a specific formula is used and the calculations are stored in the Homopolymer column on the annovar sheet. When I step through the code I can see that the lookup text is being passed to Res, but the formula is not working and I do not know why. I have this question posted at:

    http://www.excelforum.com/excel-prog...-new-post.html

    that is a long post with many more details. I have attached an example as well. Thank you.

    Basically the user selects the case and Comprehensive Epilepsy is the vlookup value passed to Res, next the formula for that text is used in the Select Case in calculations. The calculated values are then stored in the Homopolymer column of annovar (which is variable in row count)... currently it is 192, but tomorrow it may be 210. Thank you.
    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is a long post and the responses are many and detailed. It would be pointless us trying to a) start afresh here, b) try and follow what is happening there ad diffuse the responses here as well as there.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    May 2014
    Posts
    71
    Location
    I know the issue is the formulas, I just have no idea how to fix them. Thanks.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I'm not as good with WS formulas as a lot of the people here, but in the VBA try ...


    Cells(5, iHomopolymer).Formula = .......

    Now that assumes that the formula you've constructed is correct
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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