+ Reply to Thread
Results 1 to 4 of 4

macro which takes 100 entries in a column and turns them into a 10x10 matrix

Hybrid View

intothewild macro which takes 100 entries... 09-25-2011, 12:41 PM
Andrew-R Re: macro which takes 100... 09-25-2011, 01:15 PM
intothewild Re: macro which takes 100... 09-25-2011, 06:38 PM
Andrew-R Re: macro which takes 100... 09-25-2011, 06:43 PM
  1. #1
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    macro which takes 100 entries in a column and turns them into a 10x10 matrix

    i need to write a macro which takes 100 entries in column in an excel spreadsheet and turns it into a 10x10 matrix on either the same or another sheet.

    in vba code:

    How do i read the first 10 entries in a column then cut and paste them into another column,
    then take the next 10 down of the 90 that are left and put them in the column to the right of
    the first 10 i took away.

    Then what happens when theres 110 in the column and i need to have a 11x10 matrix?

    cheers
    Last edited by intothewild; 09-25-2011 at 06:58 PM. Reason: clarity

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: macro which takes 100 entries in a column and turns them into a 10x10 matrix

    The following code will take any number of cells in column A of the active sheet and use them to make a matrix 10 rows high on the named sheet.

    Sub MakeGrid()
    
    Const sTARGET_SHEET = "Sheet2"
    Const lCOL_LENGTH = 10
    Const lFIRST_ROW = 1
    Const lSOURCE_COL = 1
    Const lTARGET_ROW=1
    
    Dim lLastRow As Long
    Dim lLoop As Long
    
    Sheets(sTARGET_SHEET).Cells.Clear
    
    With ActiveSheet
      lLastRow = .Cells(.Rows.Count, lSOURCE_COL).End(xlUp).Row
    
      For lLoop = 1 To WorksheetFunction.RoundUp((lLastRow - lFIRST_ROW) / lCOL_LENGTH, 0)
        .Range(.Cells((lLoop - 1) * lCOL_LENGTH + lFIRST_ROW, lSOURCE_COL), .Cells(lLoop * lCOL_LENGTH + lFIRST_ROW - 1, lSOURCE_COL)).Copy Destination:=Sheets(sTARGET_SHEET).Cells(lTARGET_ROW, lLoop)
      Next lLoop
      
    End With
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-29-2011
    Location
    Scotland
    MS-Off Ver
    Excel 2003
    Posts
    122

    Re: macro which takes 100 entries in a column and turns them into a 10x10 matrix

    cheers Andrew-R

    With ActiveSheet
      lLastRow = .Cells(.Rows.Count, lSOURCE_COL).End(xlUp).Row
    
      For lLoop = 1 To WorksheetFunction.RoundUp((lLastRow - lFIRST_ROW) / lCOL_LENGTH, 0)
        .Range(.Cells((lLoop - 1) * lCOL_LENGTH + lFIRST_ROW, lSOURCE_COL), .Cells(lLoop * lCOL_LENGTH + lFIRST_ROW - 1, lSOURCE_COL)).Copy Destination:=Sheets(sTARGET_SHEET).Cells(lTARGET_ROW, lLoop)
      Next lLoop
      
    End With


    ....can you explain a little how the 'With' section works im not so sure i follow it..
    Last edited by intothewild; 09-25-2011 at 06:41 PM.

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: macro which takes 100 entries in a column and turns them into a 10x10 matrix

    It just tells Excel which object you're working with, once you've made a with statement any properties or methods belonging to the object can be referred to by proceeding them with a period.

    So, for example:

    With Sheets(1).Range("A1")
      .Value=10
      .Interior.Color=vbRed
    End With

    Does the same as:

    Sheets(1).Range("A1").Value=10
    Sheets(1).Range("A1").Interior.Color=vbRed

    As I'm working with the active sheet the with statement probably wasn't needed, it was just force of habit.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1