Page 5 of 12 FirstFirst ... 34567 ... LastLast
Results 41 to 50 of 117

Thread: Tests and Notes on Range objects in Excel Cell

  1. #41
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10

    Get 2D array from closed workbook using closed workbook reference

    I have a closed workbook, Closed.xlsm. ( It is the same folder as an open workbook, so I can get the path to it from a code line in the open workbook like ThisWorkbook.Path )

    ExecuteExcel4Macro
    _a) From that closed workbook (Closed.xlsm) I can get a value, using ExecuteExcel4Macro , from a single cell like this
    somevalue = application.ExecuteExcel4Macro("'C\somepath\[Closed.xlsm]SomeSheet'!R1C1")
    somevalue = application.ExecuteExcel4Macro("'" & ThisWorkbook.Path & "\[Closed.xlsm]SomeSheet'!R1C1")

    _b) I cannot do something similar, using ExecuteExcel4Macro , to return a 2D array from Closed.xlsm like
    some2Darray() =Application.ExecuteExcel4Macro("'C\path\[Closed.xlsm]somesheet'!R1C1:R5C2")
    That last code line will not work. It will error!

    However, I can do something similar to _a) and _b) using a similar approach with a simple closed workbook reference.

    Closed Workbook Reference
    _a) A simple formula , placed in an arbitrary spare cell , say the first, A1 , will return the value from a cell in a closed workbook
    So for example, to return the value from the first cell in a closed workbook into the first cell of an open workbook, a simple code line like this can be used in the open workbook:
    Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet' R1C1"

    This is what the closed workbook, Closed.xlsm, looks like
    Closed_xlsm.JPG : https://imgur.com/CBbun1h
    Attachment 2370
    If Closed.xlsm is closed and in the same folder as another workbook, macro.xlsm, then either of the following simple code lines in the following macro will return me the value from the first cell in the closed workbook, into the first cell in macro.xlsm

    So after running the simple macro I will get this:
    ClosedRefInCellA1.jpg : https://imgur.com/6N25bDe
    Attachment 2371

    Code:
    '
    '                                                                                                                                                                       https://www.excelforum.com/excel-programming-vba-macros/1286750-getting-2d-array-from-a-closed-workbook-using-executeexcel4macro.html
    Sub Testie()
     Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!R1C1"
     Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
     Workbooks("macro.xlsm").Worksheets.Item(1).Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
     ThisWorkbook.Worksheets.Item(1).Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
     Let ThisWorkbook.Worksheets.Item(1).Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
    
    End Sub
    _b) Lets say that I want to get the information from a "2D array" in the closed workbook. Lets say I want the information from the first 2 columns, and first 5 rows, like
    R1C1:R5C2 or $A$1:$B$5

    I can do this using closed workbook references. One way is to use the same fixed vector reference ** within a closed workbook reference applied across some spare range in the open workbook, macro.xlsm.

    Lets say I want to put the reference into the first 2 columns and first 5 rows of the open workbook, macro.xlsm.
    The fixed vector reference to apply to this range will be that, from a worksheets first cell to the worksheets first cell. In other words , A1 or R[0]C[0], ( or RC, as R defaults to R[0] etc.. ). I apply that reference to the first two columns and first 5 rows in the workbook, A1:B5

    So if this is my closed workbook:
    Closed_xlsm (_b).JPG : https://imgur.com/3f9U6M1
    Attachment 2372

    then either of the following simple code lines in the following macro will return me the range of values from the range in the closed workbook, into the first two columns and first 5 rows in macro.xlsm

    So after running the macro, Sub TestieClosedArrayRangeReference() , my first worksheet in macro.xlsm , will look like this
    ClosedRefsInCellsA1toB5.JPG https://imgur.com/SzvBlea

    Code:
    Sub TestieClosedArrayRangeReference() '          http://www.excelfox.com/forum/showthread.php/2355-Tests-and-Notes-on-Range-Referrencing?p=11431&viewfull=1#post11431
     Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!R[0]C[0]"
     Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!RC"
     Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
     Workbooks("macro.xlsm").Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
     ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
     Let ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
    
    End Sub

    Get 2D array from closed workbook
    Finally, if I want to put that range of values into an array, then I simply use the available single line capture of a range of values using the .Value Property "Method" applied to that range in the macro.xlsm. ( After this I can delete, ( Clear ) , the temporary range of values).
    As example: The following macro is in the uploaded file macro.xlsm. Download both macro.xlsm and Closed.xlsm into the same Folder.
    Open just macro.xlsm. Leave Closed.xlsm closed. Run Sub Get2DArrayfromClosedWorkbook()
    The values in range A1:B5 from the closed workbook, Closed.xlsm, will be placed in the array, MyArr()
    Code:
    Sub Get2DArrayfromClosedWorkbook()
     Let ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
    
    Dim MyArr() As Variant
     Let MyArr() = ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value
     ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Clear
    
     MsgBox Prompt:="As example of my array content, here is value from B4 in closed workbook:" & vbCrLf & "MyArr(4, 2) value is    """ & MyArr(4, 2) & """"
    End Sub
    
    
    
    
    
    
    
    
    
    Ref
    fixed vector reference ** In simple terms , this means simply a range reference in relative terms, ( so neglecting the $ in Column Letter and row number notation,  or including the [ ] in row number and column  number notation:  https://teylyn.com/2017/03/21/dollarsigns/#comment-191 
    
    



    Ref
    fixed vector reference ** In simple terms , this means simply a range reference in relative terms, ( so neglecting the $ in Column Letter and row number notation, or including the [ ] in row number and column number notation: https://teylyn.com/2017/03/21/dollarsigns/#comment-191


    Attached Images Attached Images
    Attached Files Attached Files
    A Folk, A Forum, A Fuhrer ….

  2. #42
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10

    Get 2D array from closed workbook using closed workbook reference

    I have a closed workbook, Closed.xlsm. ( It is the same folder as an open workbook, so I can get the path to it from a code line in the open workbook like ThisWorkbook.Path )

    ExecuteExcel4Macro
    _a) From that closed workbook (Closed.xlsm) I can get a value, using ExecuteExcel4Macro , from a single cell like this
    somevalue = application.ExecuteExcel4Macro("'C\somepath\[Closed.xlsm]SomeSheet'!R1C1")
    somevalue = application.ExecuteExcel4Macro("'" & ThisWorkbook.Path & "\[Closed.xlsm]SomeSheet'!R1C1")

    _b) I cannot do something similar, using ExecuteExcel4Macro , to return a 2D array from Closed.xlsm like
    some2Darray() =Application.ExecuteExcel4Macro("'C\path\[Closed.xlsm]somesheet'!R1C1:R5C2")
    That last code line will not work. It will error!

    However, I can do something similar to _a) and _b) using a similar approach with a simple closed workbook reference.

    Closed Workbook Reference
    _a) A simple formula , placed in an arbitrary spare cell , say the first, A1 , will return the value from a cell in a closed workbook
    So for example, to return the value from the first cell in a closed workbook into the first cell of an open workbook, a simple code line like this can be used in the open workbook:
    Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet' R1C1"

    This is what the closed workbook, Closed.xlsm, looks like
    Closed_xlsm.JPG : https://imgur.com/CBbun1h
    Attachment 2370
    If Closed.xlsm is closed and in the same folder as another workbook, macro.xlsm, then either of the following simple code lines in the following macro will return me the value from the first cell in the closed workbook, into the first cell in macro.xlsm

    So after running the simple macro I will get this:
    ClosedRefInCellA1.jpg : https://imgur.com/6N25bDe
    Attachment 2371

    Code:
    '
    '                                                                                                                                                                       https://www.excelforum.com/excel-programming-vba-macros/1286750-getting-2d-array-from-a-closed-workbook-using-executeexcel4macro.html
    Sub Testie()
     Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!R1C1"
     Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
     Workbooks("macro.xlsm").Worksheets.Item(1).Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
     ThisWorkbook.Worksheets.Item(1).Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
     Let ThisWorkbook.Worksheets.Item(1).Range("A1").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!$A$1"
    
    End Sub
    _b) Lets say that I want to get the information from a "2D array" in the closed workbook. Lets say I want the information from the first 2 columns, and first 5 rows, like
    R1C1:R5C2 or $A$1:$B$5

    I can do this using closed workbook references. One way is to use the same fixed vector reference ** within a closed workbook reference applied across some spare range in the open workbook, macro.xlsm.

    Lets say I want to put the reference into the first 2 columns and first 5 rows of the open workbook, macro.xlsm.
    The fixed vector reference to apply to this range will be that, from a worksheets first cell to the worksheets first cell. In other words , A1 or R[0]C[0], ( or RC, as R defaults to R[0] etc.. ). I apply that reference to the first two columns and first 5 rows in the workbook, A1:B5

    So if this is my closed workbook:
    Closed_xlsm (_b).JPG : https://imgur.com/3f9U6M1
    Attachment 2372

    then either of the following simple code lines in the following macro will return me the range of values from the range in the closed workbook, into the first two columns and first 5 rows in macro.xlsm

    So after running the macro, Sub TestieClosedArrayRangeReference() , my first worksheet in macro.xlsm , will look like this
    ClosedRefsInCellsA1toB5.JPG https://imgur.com/SzvBlea

    Code:
    Sub TestieClosedArrayRangeReference() '          http://www.excelfox.com/forum/showthread.php/2355-Tests-and-Notes-on-Range-Referrencing?p=11431&viewfull=1#post11431
     Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!R[0]C[0]"
     Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!RC"
     Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
     Workbooks("macro.xlsm").Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
     ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
     Let ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
    
    End Sub

    Get 2D array from closed workbook
    Finally, if I want to put that range of values into an array, then I simply use the available single line capture of a range of values using the .Value Property "Method" applied to that range in the macro.xlsm. ( After this I can delete, ( Clear ) , the temporary range of values).
    As example: The following macro is in the uploaded file macro.xlsm. Download both macro.xlsm and Closed.xlsm into the same Folder.
    Open just macro.xlsm. Leave Closed.xlsm closed. Run Sub Get2DArrayfromClosedWorkbook()
    The values in range A1:B5 from the closed workbook, Closed.xlsm, will be placed in the array, MyArr()
    Code:
    Sub Get2DArrayfromClosedWorkbook()
     Let ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value = "='" & ThisWorkbook.Path & "\[Closed.xlsm]somesheet'!A1"
    
    Dim MyArr() As Variant
     Let MyArr() = ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Value
     ThisWorkbook.Worksheets.Item(1).Range("A1:B5").Clear
    
     MsgBox Prompt:="As example of my array content, here is value from B4 in closed workbook:" & vbCrLf & "MyArr(4, 2) value is    """ & MyArr(4, 2) & """"
    End Sub
    
    
    
    
    
    
    
    
    
    Ref
    fixed vector reference ** In simple terms , this means simply a range reference in relative terms, ( so neglecting the $ in Column Letter and row number notation,  or including the [ ] in row number and column  number notation:  https://teylyn.com/2017/03/21/dollarsigns/#comment-191 
    
    



    Ref
    fixed vector reference ** In simple terms , this means simply a range reference in relative terms, ( so neglecting the $ in Column Letter and row number notation, or including the [ ] in row number and column number notation: https://teylyn.com/2017/03/21/dollarsigns/#comment-191


    Attached Images Attached Images
    Attached Files Attached Files
    A Folk, A Forum, A Fuhrer ….

  3. #43
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Postz for later use

  4. #44
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Postz for later use

  5. #45
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Postz for later use

  6. #46
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Postz for later use

  7. #47
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Postz for later use

  8. #48
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Postz for later use

  9. #49
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    Item way of thinking as alternative to conventional row column looping


    Current conventional way of thinking
    This Blog post assumes that you have some basic Excel VBA programming experience, and that you are familiar with this general idea used typically when filling or getting data from cells
    __For Rw = 1 To maximumRows
    ____For Clm = 1 To maximumColumns
    _____( Rw, Clm )
    ____Next Clm
    __ Next Rw

    This basic process you should make sure you fully understand before reading further.
    This Blog post suggest a slightly different way about thinking about that process

    Introduction
    Excel Spreadsheets can be turned into pretty pictures, computer graphics and games and “dashboards”.
    But they are usually not the best at doing those things.
    They are best for doing things with 2 dimensional “tables” of data.
    They bridge the gap between
    _ dead stores of data or long fast streams of string data,
    and
    _ relatively slow and static living humans interacting with them

    I expect we are not a long way from interacting in a 3 dimensional way, but it will settle down, I think, to be useful in a limited way. More than 3 dimensions are difficult for humans to react easily with.
    A 2 dimensional desktop is particularly convenient for us.
    Smartphones are a trend that I think might go back a bit to being just glorified telephones, walkmans, live cameras and simple computer games.

    We are basically talking in this Thread about organising a string of data travelling at the speed of light into a static line or row of the data, then carrying the rest of the data back, feeding in another row or line or “going down”, then doing the same again.

    I think for Excel formulas and VBA we should not take the core of the thinking much further than this.
    For furthering the study and manipulation of static table data something else , another concept, such as Power Query / Get and Transform might be more appropriate.

    Looping along columns for every row The conventional way
    __For Rw = 1 To maximumRows
    ____For Clm = 1 To maximumColumns
    _____( Rw, Clm )
    ____Next Clm
    __ Next Rw

    This is what we have got used to, from reading books, to the workings of old printers, to how a screen is filed with data. It is an efficient and logical consequence that a spreadsheet, or a rectangular range in a spreadsheet, is filled and otherwise updated in the same way.
    Usually we look at a cell when doing formula calculations, but if we increase the exposed area during a screen update, we can get an efficient, almost simultaneous, calculation across an entire range. This latter is the basis of the workings of the so called array formulas, ( “CSE” stuff ). We will come on to that in the next post

    Looping the Items
    Many things in VBA, and computers generally, are actually ordered somewhere by something similar to a unique item number , 1 2 3 4 5 6 … etc. ( Note: in some computer conventions the first may start at 0 so we have 0 1 2 3 4 5… etc. )
    Conventionally, the Range Item numbers are also so organised, and further follow the generally accepted convention of
    Across
    _____down
    back

    Across
    _____down
    back

    ………etc
    .

    ( Conventionally in computer stream strings we may refer to those
    backs as carriage returns, vbCr ,
    and
    the downs as line feeds, vbLf.
    These may be referred to as “invisible characters”, as we do not typically see them, but they must be there in the electronic string stream to indicate the start of a new line or new row
    )

    For example for a 2 row x 3 column range, this is where the item numbers are/ what cells the item number refers to
    1 2 3
    4 5 6

    Furthermore, for many things that do not have such an item number they may have a consistent order such that we can assume or assign some simple count mechanism to give them an index that we can use in a similar way to the Item number

    The crux of my suggestion in this Thread is to think of the basic process shown at the start of this Blog, in a slightly different way.
    A couple of reasons I can think of for doing this
    _ It may be helpful to think back to the more fundamental single string idea of what computers are, as the table type arrangement is only an interface for us, and is less representative of what is actually going on in a computer
    _ In some situations the alternative looping may have some advantages. For example we would not be limited to a rectangular range.

    Some simple required maths
    What we want to do is this sort of thing as an alternative to the basic process
    __For Itm = 1 To maximumItems
    ___( Row, Column)
    __Next Itm

    Before looking at the actual solution, lets simply put in words a simple solution idea, and consider again a 2 row, 3 column range
    1 2 3
    4 5 6

    In this case, our maximumRows will be 2 and the maximumColumns will be 3
    We have those sequentially numbers, and we want to convert then to row and column indices that would give us that spreadsheet range.

    Row Number from Item Number
    If we were to divide the actual item number by the maximumColumns , 3 , and take the integer of the result we would almost be there. But that would actually give us
    0 0 1
    1 1 2

    If we were to first subtract 1 from the Item number, then do the same again, divide that modified number by the maximumColumns , 3, and take the integer of the result we would have,
    Integer ( ( ItmNumber -1 ) / maximumColumns ) =
    0 0 0
    1 1 1

    So we now see that we only have to add 1 to the final results and we have the row number what we want.
    RowNumber = [ Integer ( ( ItmNumber -1 ) / maximumColumns ) ] + 1
    1 1 1
    2 2 2


    Column Number from Item Number
    This is slightly more tricky. But not much. I do this second as we need some of the ideas from the previous maths.
    An observation:
    The second row needs to be reduce by 1 x maximumColumns , and
    if we had a third row the item number would need to be reduced by 2 x maximumColumns … and so on.
    It also fits the same logic to apply a reduction of 0 x maximumColumns to the first row.
    So we are looking for a multiplication factor based on the Item number which gives us that sort of number sequence like 0 0 0 ; 1 1 1 ; 2 2 2 etc. sequence
    We can see from one of the stages in the maths from the last section that we did actually already have it at this stage:
    Integer ( ( ItmNumber -1 ) / maximumColumns ) =
    0 0 0
    1 1 1

    and that would have further gone on for more rows to
    0 0 0 ; 1 1 1 ; 2 2 2 ; 3 3 3 … etc.

    So we can see one formula possibility:
    ItemNumber - [ Integer ( ( ItmNumber -1 ) / maximumColumns ) X maximumColumns ]
    This would result in our required column Indicies of
    1 2 3
    1 2 3


    _.______

    Simple VBA macro example
    Code:
    Sub ItmNumbersLoopToRng()
    Dim Ws1 As Worksheet: Set Ws1 = ThisWorkbook.Worksheets("Sheet1")
    Dim ItmIndx As Long
    Dim maxClms As Long, maxRws As Long: Let maxClms = 3: Let maxRws = 2
        For ItmIndx = 1 To 5
        Dim Rw As Long, Clm As Long
         Let Rw = Int((ItmIndx - 1) / maxClms) + 1
         Let Clm = ItmIndx - (Int((ItmIndx - 1) / maxClms) * maxClms)
         Let Ws1.Cells.Item(Rw, Clm).Value = " (" & Rw & ", " & Clm & ")"
        Next ItmIndx
    End Sub
    
    Sub ItmNumbersLoopToRng2()
    Dim Ws1 As Worksheet: Set Ws1 = ThisWorkbook.Worksheets("Sheet1")
    Dim ItmIndx As Long
    Dim maxClms As Long, maxRws As Long: Let maxClms = 3: Let maxRws = 2
        For ItmIndx = 1 To 5
    '    Dim Rw As Long, Clm As Long
    '     Let Rw = Int((ItmIndx - 1) / maxClms) + 1
    '     Let Clm = ItmIndx - (Int((ItmIndx - 1) / maxClms) * maxClms)
         Let Ws1.Cells.Item(Int((ItmIndx - 1) / maxClms) + 1, ItmIndx - (Int((ItmIndx - 1) / maxClms) * maxClms)).Value = " (" & Int((ItmIndx - 1) / maxClms) + 1 & ", " & ItmIndx - (Int((ItmIndx - 1) / maxClms) * maxClms) & ")"
        Next ItmIndx
    End Sub
    
    __Result:_____
    _____ Workbook: OekyDoekyAmelynn.xls ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    D
    1
    (1, 1) (1, 2) (1, 3)
    2
    (2, 1) (2, 2)
    3
    Worksheet: Sheet1
    _.____




    There is one interesting observation already that may help us understand some of the secret workings of Excel:
    Note that we have not needed to use the parameter of maximumRows
    Now, Consider a range object of 3 columns x 2 rows anywhere in a spreadsheet
    x x x
    x x x

    Reasonably we would say that has 6 items, and conventionally we have, and we could confirm by experiment, that we have item 6 as shown here, X:
    x x x
    x x X

    What is somewhat surprising perhaps, is that we can go on to reference any further item without causing any errors, and we remain in the maximum columns restriction but seem to have no limit, ( within reason ), in the rows.
    For example, referring to item 12, would get us here X:
    x x x
    x x x

    x x x
    x x
    X

    This could be by design, or might just be an accident due to some internal calculations having a similar form to those I have suggested and discussed, and they do not have any consideration of maximum row




    I intend adding some further post on this thread later to discuss the ideas and develop them further.

  10. #50
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,458
    Rep Power
    10
    This is post #43 on Page 5 https://www.excelfox.com/forum/showt...ing/page5#post 15722
    https://http://www.excelfox.com/forum/showth...ing/page5#post 15722



    In support of this Thread: http://www.excelfox.com/forum/showth...6398#post16398




    Hello Amelynn
    Welcome to ExcelFox , the thinking man’s Excel forum


    Quote Originally Posted by Amelynn View Post
    Who can help me, ....
    I think, therefore I am , and able to help you.

    Quote Originally Posted by Amelynn View Post
    ...... I don't understand how the line ".List
    = Application.Index(rng, Evaluate("ROW(1:" & rng.Rows.Count & ")"), Array(1, 3))
    " works and I can't stay So....
    To Explain

    Array(1, 3)
    It is like –
    1 3
    ( Sometime we write in excel spreadsheet convention {1, 3} , but this usually means the same thing )

    Evaluate("ROW(1:" & rng.Rows.Count & ")")
    Quote Originally Posted by Amelynn View Post
    ..... evaluate (…….. used to occupy excel formulas in vba) with row,
    Correct! Good! - Because you understand this, it makes the explanation for me much easier.
    We are really only interested in understanding what is , =ROW(1:" & rng.Rows.Count & ")" , in excel spreadsheet formula

    For your range, rng = B4:D8 , rng.Rows.Count = 5
    =ROW(1: " & rng.Rows.Count & " )
    =ROW(1: " & 5 & " )
    =ROW(1:5)

    ( Green is Excel Spreadsheet, Blue is VBA in string “ “ in Evaluate( “ “ ) )


    The excel spreadsheet Row( ) function is usually like for
    Row( A1) = 1
    but can also return a “vertical” array of values like
    Row( A1:A2) = 1
    Row( A1:A2) = 2
    In VBA Evaluate we find that we are decoupled from spreadsheet absolute values, and so in VBA Evaluate
    Row(A1:A2) = Row( 1:2) = 1
    Row(A1:A2) = Row( 1:2) = 2

    ROW(1:5)
    It is like
    1
    2
    3
    4
    5

    ( Sometimes we may write
    {1
    2
    3
    4
    5}

    or sometimes we may write in excel spreadsheet convention , ={1;2;3;4;5} , but usually this means the same thing
    )
    Note: For “vertical” array some excel use ; but some Excel use \ – So sometime you may need ={1\2\3\4\5}




    Sometimes if we are lucky, Excel will try to do array calculations and return you an array.
    Like
    Code:
    	{	1	2		X	{	6	8		  =	{	1, 6	2, 8	
    		3	4	}			7	9	}			3, 7	4, 9	}
    But if you ask it to do = Index __ ROW(1:5)__ ,__ Array{1 , 3} it tries to look at columns and rows not specified.
    Code:
    =  {	1	?			   {	1	3	
    	2	?				?	?	
    	3	?		X		?	?	
    	4	?				?	?	
    	5	?	}			?	?	}
    ???? So we have a problem ?,
    But we can be luckyagain, because then Excel will guess to see this instead
    ( This is because of Excel VBA Interception and Implicit Intersection )
    Code:
    =  {	1	1			   {	1	3	
    	2	2				1	3	
    	3	3		X		1	3	
    	4	4				1	3	
    	5	5	}			1	3	}
    Index will try to give us
    Code:
    _______    Index     (		1, 1	1, 3	
    				2, 1	2, 3	
    				3, 1	3, 3	
    				4, 1	4, 3	
    				5, 1	5, 3	  )
    So if your range is
    Code:
    			
    	 rng = B4:D8 , rng.Rows.Count = 5		
    			
    	_B4	_C4	_D4
    	_B5	_C5	_D5
    	_B6	_C6	_D6
    	_B7	_C7	_D7
    	_B8	_C8	_D8
    then Application.Index(rng, Evaluate("=ROW(1:" & rng.Rows.Count & ")"), Array(1, 3))
    -__ =Application.Index(rng, Evaluate("=ROW(1:5)"), Array(1, 3))
    -__ =Application.Index(rng, Evaluate("={1;2;3;4;5}"), Array(1, 3))
    -__ =Application.Index(rng, Evaluate("={1;2;3;4;5}"), Evaluate("={1, 3}"))

    Code:
    	  =     _B4	_D4
    		_B5	_D5
    		_B6	_D6
    		_B7	_D7
    		_B8	_D8


    Quote Originally Posted by Amelynn View Post
    ........use an array to determine the rows as well......
    Sure, this is no problem:
    One way, for example, for just 1st 3rd and 5th row
    Change
    Evaluate("={1;2;3;4;5}")
    to
    Evaluate("={1;3;5}")



    Here a demo macro for you
    Put some arbitrary values in your range "B4:D8" , then run this macro:
    Code:
    Sub Test()
    Dim Rng As Range
     Set Rng = Worksheets("Sheet1").Range("B4:D8")
    Dim RwsCnt As Long
     Let RwsCnt = Rng.Rows.Count '  is = 5
    
    Dim arr_List() As Variant
     Let arr_List() = Application.Index(Rng, Evaluate("=ROW(1:" & Rng.Rows.Count & ")"), Array(1, 3))
     Let arr_List() = Application.Index(Rng, Evaluate("=ROW(1:" & RwsCnt & ")"), Array(1, 3))
     Let arr_List() = Application.Index(Rng, Evaluate("=ROW(1:5)"), Array(1, 3))
     Let arr_List() = Application.Index(Rng, Evaluate("={1;2;3;4;5}"), Array(1, 3))
     Let arr_List() = Application.Index(Rng, Evaluate("={1;2;3;4;5}"), Evaluate("={1,3}"))
    
     Let Worksheets("Sheet1").Range("A40").Resize(UBound(arr_List(), 1), UBound(arr_List(), 2)).Value = arr_List()
    
    ' To only select 1st 3rd and 5th row
     Let arr_List() = Application.Index(Rng, Evaluate("={1;3;5}"), Evaluate("={1,3}"))
     Let Worksheets("Sheet1").Range("A47").Resize(UBound(arr_List(), 1), UBound(arr_List(), 2)).Value = arr_List()
    
    End Sub



    Alan

Similar Threads

  1. Replies: 114
    Last Post: 03-04-2024, 02:39 PM
  2. Tests and Notes on Range Referrencing
    By DocAElstein in forum Test Area
    Replies: 70
    Last Post: 02-20-2024, 01:54 AM
  3. Tests and Notes for EMail Threads
    By DocAElstein in forum Test Area
    Replies: 29
    Last Post: 11-15-2022, 04:39 PM
  4. Some Date Notes and Tests
    By DocAElstein in forum Test Area
    Replies: 0
    Last Post: 11-23-2021, 10:40 PM
  5. Notes tests. Excel VBA Folder File Search
    By DocAElstein in forum Test Area
    Replies: 39
    Last Post: 03-20-2018, 04:09 PM

Posting Permissions

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