Consulting

Results 1 to 13 of 13

Thread: Group cells

  1. #1
    VBAX Contributor
    Joined
    May 2008
    Posts
    109
    Location

    Group cells

    Well, to group sheets in excel I select the first sheet in the workbook, hold the Shift key and click the last sheet tab in the workbook.
    This way I can enter my data in other sheets but the same cell adresses.
    Can someone help me with a macro that group worksheets automatically and allow me to select the cell adresses where I want my data appear om other sheets.

    Thanks in advance for any help


    Sasa



  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I strongly suspect you HAVE to let the VBA do some selecting. In the code below add the names of the sheets you want to change to the list (array) in the first line:
    [vba] Sheets(Array("Sheet1", "Sheet3")).Select
    Range("B17").Select
    ActiveCell.FormulaR1C1 = "blah"
    [/vba]
    Last edited by p45cal; 01-15-2012 at 08:46 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Contributor
    Joined
    May 2008
    Posts
    109
    Location
    Quote Originally Posted by p45cal
    I strongly suspect you HAVE to let the VBA do some selecting. In the code below add the names of the sheets you want to change to the list (array) in the first line:
    [vba] Sheets(Array("Sheet1", "Sheet3")).Select
    Range("B17").Select
    ActiveCell.FormulaR1C1 = "blah"
    [/vba]
    Thanks, but this way I can not input my data on Sheet1 and copy these,where I prefer on sheet3

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Something like
    [VBA]Option Explicit

    Sub MultipleCopy()
    Dim r As Range, cel As Range
    Dim arr()
    Dim sh As Worksheet
    Set r = Selection

    arr = Array("Sheet1", "Sheet2", "Sheet3")

    For Each sh In Sheets(arr)
    For Each cel In r
    cel.Copy sh.Range(r.Address)
    Next
    Next
    End Sub
    [/VBA]
    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
    Joined
    May 2008
    Posts
    109
    Location
    Pls, how I have to set the input and output data ?
    For example from A1 on sheet1 to B7 on sheet 3 ?

    With regard
    Sasa

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    In Sheet3 B7 enter =Sheet1!A1
    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
    Joined
    May 2008
    Posts
    109
    Location
    After an accurate analysis of my problem , I realized that I need another type of Macro.

    I need a Macro that can receive in input , always, 8 cells and sort them in another sheet in this order:

    For Example: I've selected 8 cells " A1, A2, A3 , A4 , A5, A6, A7, A8" and I want my macro to copy them in another sheet in this order :
    x y
    1 A1 A2
    2 A3 A4
    3 A5 A6
    4 A7 A8

    It would be great if I could select more the one row, so 16 or 24 or 32 cells and so on, and sort the data just under the other already sorted.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Transfer()
    Dim r As Range, cel As Range
    Dim i As Long
    Set r = Selection
    i = 1
    For Each cel In r
    i = i + 1
    Sheets(2).Cells(i \ 2, (i Mod 2) + 1) = cel
    Next
    End Sub[/VBA]
    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'

  9. #9
    VBAX Contributor
    Joined
    May 2008
    Posts
    109
    Location
    Can you give me another little help, to realize a new order of the list ?

    For Example: I've selected 8 cells " A1, A2, A3 , A4 , A5, A6, A7, A8" and I want my macro to copy them in another sheet in this new order :
    x y
    1 A1 A5
    2 A2 A6
    3 A3 A7
    4 A4 A8

    It would be great if I could select more the one row, so 16 or 24 or 32 cells and so on, and sort the data just under the other already sorted.

    Local Time: 03:24 AM
    Local Date: 01-17-2012
    Location:


  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The purpose of this site is provide assistance, not to do your work. Let us see your own best attempt at a solution.
    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'

  11. #11
    VBAX Contributor
    Joined
    May 2008
    Posts
    109
    Location
    [VBA]Sub Transfer1()
    Dim r As Range, cel As Range
    Dim i As Long
    Dim cellCount As Long
    Dim myRow As Long
    Dim myCell As Long
    Set r = Selection
    cellCount = r.Count
    i = 0
    For Each cel In r
    i = i + 1
    myRow = (i Mod (Int(cellCount / 2)))
    If myRow = 0 Then myRow = Int(cellCount / 2)
    myColumn = Int((i - 1) / Int(cellCount / 2)) + 1
    Sheets(2).Cells(myRow, myColumn) = cel
    Next
    End Sub[/VBA]

    ok....
    but how to group four by four the way to have this output:

    x y
    1 A1 A5
    2 A2 A6
    3 A3 A7
    4 A4 A8

    5 A9 A13

    6 A10 A14

    7 A11 A14

    8 A12 A15

    and so on

    Thanks

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Transfer1()
    Dim r As Range, Rw As Long, Col As Long
    Set r = Selection
    For Each cel In r
    i = i + 1
    Rw = 1 + ((i - 1) Mod 4) + ((i - 1) \ 8) * 4
    Col = 1 + ((i - 1) \ 4) Mod 2
    Sheets(1).Cells(Rw, Col) = cel
    Next
    End Sub
    [/VBA]
    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'

  13. #13
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    A flexible solution will involve supplying:
    1. The source (single column assumed).
    2. The destination region (or its top left cell) including sheet info.
    3. How high in numbers of rows you want each grouping. (You said 4)
    4. How wide in numbers of columns you want each grouping (you said 4, but you showed 2!)
    [vba]Sub Transfer1()
    Dim r As Range, DestTopLeft As Range
    Dim ClusterHeight As Long, DestWidth As Long, rw As Long, colm As Long, DestRow As Long

    'The 4 starting data:
    Set r = Selection
    Set DestTopLeft = Sheets("Sheet14").Range("B3") 'the Destination (includes sheet ref.)
    ClusterHeight = 4
    DestWidth = 2

    For rw = 1 To r.Rows.Count Step ClusterHeight
    colm = ((rw - 1) / ClusterHeight) Mod DestWidth + 1
    DestRow = Int((rw - 1) / (ClusterHeight * DestWidth)) * ClusterHeight + 1
    r.Cells(rw, 1).Resize(ClusterHeight).Copy DestTopLeft.Cells(DestRow, colm)
    Next rw
    End Sub
    [/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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