New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

seting formula in cel by VBA

Macros, VBA, Excel Automation, etc.

seting formula in cel by VBA

Postby VSR » Tue Jan 24, 2012 3:12 pm

Hi All, I'm looking for help about "Err 1004" in row below:
Range("A1").Formula = "=INDEX('[fileA.xls]Sheet1'!$C:$D;MATCH(A2;'[fileA.xls]Sheet1'!$C:$C;0);2)"
  • 0

VSR
Rookie
 
Posts: 3
Joined: Jan 24, 2012
Reputation: 0

Re: seting formula in cel by VBA

Postby charles » Tue Jan 24, 2012 3:16 pm

VSR,

Could you post a copy of the workbook. To do so when you reply you will see a tab below the msgbox marked "Upload" click on it and then you can select the file you wish to attach.
  • 0

There are other ways too do this but...

Be kind let us know if you posted somewhere else!



Charles
charles
Excel Badass
 
Posts: 632
Joined: Dec 10, 2011
Location: Mississippi
Reputation: 1
Excel Version: Excel 2003, 2007 and Mac 2011

Re: seting formula in cel by VBA

Postby VSR » Tue Jan 24, 2012 3:42 pm

it's for example only with abstract data, when formula find equal to cell "A2" in "fileA", "colC", value from same row, but from col "D", is appears in cell "A1". It,s work in cell in sheet from workbook, perfect.
  • 0

You do not have the required permissions to view the files attached to this post.
VSR
Rookie
 
Posts: 3
Joined: Jan 24, 2012
Reputation: 0

Re: seting formula in cel by VBA

Postby charles » Tue Jan 24, 2012 8:29 pm

HI,

Take a look at this file.
You change the value in "E2" and the result will be displayed in "E1"
  • 0

You do not have the required permissions to view the files attached to this post.
There are other ways too do this but...

Be kind let us know if you posted somewhere else!



Charles
charles
Excel Badass
 
Posts: 632
Joined: Dec 10, 2011
Location: Mississippi
Reputation: 1
Excel Version: Excel 2003, 2007 and Mac 2011

Re: seting formula in cel by VBA

Postby Sisyphus » Tue Jan 24, 2012 11:23 pm

Hi,
This is a tacky business, not solved as easily as it looks. Long ago I wrote a number of functions to do this job. Here they are:
Code: Select all
Private Function RefCell(ByVal R As Long, _
                         ByVal C As Long, _
                         Optional ByVal Typ As Long, _
                         Optional ByVal VSh As Variant) As String
    ' cell(R, C)
    ' in Sheet(VSh) using XlReferenceType
   
    RefCell = ConvertColNum(C) & CStr(R)
    Typ = IIf(Typ, Typ, 4)
    RefCell = Application.ConvertFormula(RefCell, xlA1, xlA1, Typ)
    If VarType(VSh) = vbError Then VSh = ""
    If Val(VSh) Then VSh = CStr(VSh)
    If Len(VSh) Then _
       RefCell = InBrackets(Trim(VSh), 39) & "!" & RefCell
End Function

Using the values for R (Row) and C (Column) the function returns a cell reference like A1.
You can supply the optional Typ argument to specify the type of reference, like $A1 etc.
The Typ argument must be one of the xlReferenceType constants:
xlAbsolute 1
xlAbsRowRelColumn 2
xlRelative 4
xlRelRowAbsColumn 3

The final, optional, argument is Vsh. So, the function can return something like 'Sheet Two'!$R$32

The next function is AsFunction. It wraps the FUNC string in brackets and places the function's name before it, like
=SUMIF(B1:B22, 2)
In this example "=SUMIF" would be the FUNC argument, "B1:B22" and 2 elements of the ParamArray. You can have as many as you like. The = sign must be handed down so as to enable you to nest functions.
Code: Select all
Private Function AsFunction(ByVal FUNC As String, _
                            ParamArray Items() As Variant) As String
    ' Return [=][FUNC](strFunc)
   
    Dim Fn As Integer, Fs As String
    Dim S As String
   
    For Fn = 0 To UBound(Items)
        Fs = Trim(CStr(Items(Fn)))
        If Len(Fs) And Len(S) Then
            If Right(S, 1) <> "," Then S = S & ","
        End If
        S = S & Fs
    Next Fn
    AsFunction = Trim(FUNC) & InBrackets(S)
End Function

The final function in the trio, InBrackets, supports the other functions. All it does is to enclose the argument Expr in brackets. Called without further argument, normal round brackets will be added. However, you can pass an optional argument Quote to produce other kinds of brackets. The most common is Chr(34) to embrace a string, like "My string"
Code: Select all
Private Function InBrackets(ByVal Expr As String, _
                            Optional ByVal Quote As Long) As String
    ' Insert Expr between brackets or Chr(Quote)
   
    Dim Q1 As String, Q2 As String
   
    Q1 = Chr(IIf(Quote, Quote, 40))
    Q2 = Chr(IIf(Quote, Quote, 41))
    InBrackets = Q1 & Expr & Q2
End Function


Sticking with my above example of =SUMIF(B1:B22, 2) the code would look like this:
Code: Select all
AsFunction("=SUMIF", RefCell(1, 2) & ":" & RefCell(22, 2), 2)

To nest formulae, like in INDEX / MATCH, you would construct the inside formula first, store it in a string, then construct the outside formula using the prepared string as one of the elements in the ParamArray. I haven't tried the line of code and haven't used the system in a long time. But the above procedures are all tried and proven and by tinkering with it a little you can work out how to solve your problem. The tools are here! :D
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: seting formula in cel by VBA

Postby VSR » Wed Jan 25, 2012 11:19 am

IT'S SOLVED, MAY BE MY EXPLANATION WAS URONG.
  • 0

VSR
Rookie
 
Posts: 3
Joined: Jan 24, 2012
Reputation: 0


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 142 guests