Consulting

Results 1 to 9 of 9

Thread: Directing SendKeys to the Correct Application

  1. #1
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location

    Directing SendKeys to the Correct Application

    Because of the length limitations of Range.FormulaArray, when I need to enter a large array formula in a cell, I enter it as a normal formula and then apply Cntrl+Shift+Enter with SendKeys. For example:

    Sub ArFormula()
        ActiveCell.Formula = "=MAX(IF(A2>B1:B12,B1:B12))"
        With Application
            .SendKeys "{F2}" ' get into edit mode
            .SendKeys "^+~" '  use CNTRL + Shift + Enter
        End With
    End Sub
    This works fine if I am running it from the worksheet using the Ribbon Menu. However, if I try to run it from the VBA window, it fails. It fails because its the VBE that gets the keystrokes, not Excel.

    In Excel F2 gets into the Edit mode for a cell, in the VBE, F2 does something else.

    Is there any way to modify the code to insure that Excel gets the keystrokes and not the VBE ??
    Have a Great Day!

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    I'd use:

    sub M_snb()
      CallByName ActiveCell, "formulaarray", 4, "=MAX(IF(A2>B1:B12,B1:B12))"
    End Sub

  3. #3
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Thanks for responding.........sadly once the number of characters in the formula exceeds 256, the CallByName method fails:
    Sub ytrewq3()
        Dim ReallyBigFormula As String, piece As String
        
        piece = "+A1"
        With Application.WorksheetFunction
            ReallyBigFormula = "=A1" & .Rept(piece, 90)
        End With
        
        CallByName ActiveCell, "formulaarray", 4, ReallyBigFormula
        
    End Sub
    Have a Great Day!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Could you use a named formula as here?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Very interesting!!.....................I am starting with the proposed array formula as a String variable in VBA..............is there a way to create a Named Formula from a String ??
    Have a Great Day!

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can't say I understand it but the non-array Named formula appears to give the same results as the array formula, at least with your sample formula.
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Thanks for the help !! Your Sub has an extra Exit Sub in the middle....................take that out and it runs just fine!

    Here is an adaptation of your method:

    Sub RBF()
        Dim ReallyBigFormula As String, piece As String
        
        piece = "+$A$1"
        With Application.WorksheetFunction
            ReallyBigFormula = "=$A$1" & .Rept(piece, 90)
        End With
        
        ActiveWorkbook.Names.Add Name:="Test", RefersTo:=ReallyBigFormula
        
        Selection.FormulaArray = "=Test"
    End Sub
    Last edited by GarysStudent; 05-28-2016 at 04:45 PM.
    Have a Great Day!

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    A formula that long probably has several parts that return values on their own. Each of those parts can be a named formula, then use that name in another part that needs the returned value.

    =IF(CONCATENATE(VLOOKUP(BAH, BLAH, BLAH), HLOOKUP(,BLAHBLAH)),BLEHBLEHBLEH,WHAWHAWHA)

    Named Formula VL1 = VLOOKUP(BAH, BLAH, BLAH)
    Named Formula HL1 = HLOOKUP(,BLAHBLAH)
    Named Formula CON1 = CONCATENATE(VL1, HL1)
    Etc

    Then:
    {=If(CON1,BLEH1,WHA1)}

    I'm not really a formula guy, you might have to use R1C1 notation.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    in VBA you can

    Sub M_snb() 
      ActiveWorkbook.Names.Add "Test", "=" & replace(string(91,"|"),"|","+$A$1")
         
      Selection.FormulaArray = "=Test" 
    End Sub

Posting Permissions

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