Results 1 to 9 of 9

Thread: copy cells

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    copy cells

    hi there

    in vba - how do i copy the cells from one sheet to another sheet?

    these cells are in groups, group contains 3 rows, i wanna copy each group into another sheet

    thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Just use the Copy method of the Range object, eg:

    Sheet1.Range("A1:A3").Copy Sheet2.Range("B5:B7")

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    wow many thanks for the speedy response! just what i need!

    i understand that, however, hmm hard to explain

    this sheet, has many groups of rows.

    each group has 3 values

    for every group in this sheet, i want to copy the values to another sheet but in one line. so each group values are put on a new line = see where im going?

    we wouldnt know, well we kinda do, where to begin or where to stop looking for the cells with entries.

    he has given me starting points,

    like:

    A8 + 3 rows
    B9 + ""
    C9 + ""
    ..
    ..
    F10 + 3 rows
    ..
    ..

    ? know what i mean?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I'm not sure what you mean.

    Do you mean you want the data formatted differently in the target location?

    Can you find the source cells?



    By the way, the best method for finding out what code you need (assuming you can do it in Excel manually) is to record a macro of the actions, then Excel writes the VB code for you.

    I know in this situation it probably wont help that much, but you can find part of the solution.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773


    yes, in the destination - the data is formatted differently

    how should i go about doing this?

    how do u place the copied cell values into the destination cells?
    (syntax)

    how do i make a loop which will detect if there is text in the next 3 cells/rows, if there is it will continue copying, otherwise if the next 3 cells have no text, it will stop copying

    ty

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    ok, to copy the data into a different cell arrangement:
    VB Code:
    1. Dim tmpArray
    2. 'copy values to an array
    3. tmpArray = Sheet1.Range("A1:A3").FormulaArray
    4.  
    5. 'put them back out again
    6. Sheet2.Range("B3") = tmpArray(0)
    7. Sheet2.Range("C4") = tmpArray(1)
    8. Sheet2.Range("D5") = tmpArray(2)


    As for detecting values, just loop until cells are empty.
    eg:
    VB Code:
    1. Dim iRowNum as Integer
    2. Dim iColNum as Integer
    3. Dim bGotNextCell as Boolean
    4.  
    5. iRowNum = 1
    6. iColNum = 1
    7. Do
    8.   '(copy code here)
    9.  
    10.   bGotNextCell = False
    11. 'basic example - only checks cell to the right or below
    12. '(might be better to put this into a For loop)
    13.   If Sheet1.Cells(iRowNum + 1, iColNum) <> "" Then
    14.     iRowNum = iRowNum + 1
    15.     bGotNextCell = True
    16.   ElseIf Sheet1.Cells(iRowNum, iColNum + 1) <> "" Then
    17.     iColNum = iColNum + 1
    18.     bGotNextCell = True
    19.   End If
    20. Loop While bGotNextCell

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    thanks very much, i will try to implement this, and i hope it works thanks!

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773
    im still annoyed and stressed at this, dont even know where to begin, sitting here for 3 hours looking at the screen dunno what to type. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    so i spoke to the guy again. and he says:

    for every group, each group consists of 3 rows, each of these 3 rows must be on 1 row with the project header, which is on row 1 of each group.

    so how do i go about do a loop which will take each cell value, and copy it to one row in different columns, until the counter has hit 3, which will move on to the next group? aaaaaaaaaaaaaaaaaaaaaaa

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    well merging my two previous examples and a little tweaking will give you this:
    VB Code:
    1. Dim iRowNum as Integer
    2. Dim iColNum as Integer
    3. Dim iOutputRow as Integer
    4. Dim bGotNextCell as Boolean
    5.  
    6. iRowNum = 2
    7. iColNum = 1
    8. iOutputRow = 1
    9. Do
    10.                               'find next cell group
    11.   bGotNextCell = False
    12. 'basic example - only checks cell to the right or below
    13. '(might be better to put this into a For loop)
    14.   If Sheet1.Cells(iRowNum + 1, iColNum) <> "" Then
    15.     iRowNum = iRowNum + 1
    16.     bGotNextCell = True
    17.   ElseIf Sheet1.Cells(iRowNum, iColNum + 1) <> "" Then
    18.     iColNum = iColNum + 1
    19.     bGotNextCell = True
    20.   End If
    21.  
    22.                               'copy to output sheet
    23.   If bGotNextCell Then  
    24.     Sheet2.Cells(iOutputRow, 1) = Sheet1.Cells(1, iColNum)
    25.     Sheet2.Cells(iOutputRow, 2) = Sheet1.Cells(iRowNum, iColNum)
    26.     Sheet2.Cells(iOutputRow, 3) = Sheet1.Cells(iRowNum + 1, iColNum)
    27.     Sheet2.Cells(iOutputRow, 4) = Sheet1.Cells(iRowNum + 2, iColNum)
    28.     iOutputRow = iOutputRow + 1
    29.   End If
    30. Loop While bGotNextCell
    If I've understood correctly, all you need to do is to improve the code that finds the groups of cells to be copied.

Posting Permissions

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



Click Here to Expand Forum to Full Width