Results 1 to 10 of 10

Thread: [RESOLVED] How can I maipulate this basic code without going into a loop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Resolved [RESOLVED] How can I maipulate this basic code without going into a loop

    VB Code:
    1. Sheets("Output1").Copy

    Hi
    I want to copy a worksheet called "output1" but only row 2 downwards to the "USED Range. Therefore skipping out row 1.

    I then want to paste the contents in a template excel file called "Master" starting at cell A5.

    I tried a loop copying eachcell in the used range in an array but its wayyyy to slow for something simple.

    Please can somone advise the quickest method.

    Cheers,

    Boris.

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: How can I maipulate this basic code without going into a loop

    One way that can be quicker is to assign the cells of interest directly to an array. You can then assign the array to the destination cells. See attached link for further explanation.

    VB Code:
    1. Dim iArray As Variant
    2.     Dim nRows As Integer, nCols As Integer
    3.    
    4.     Application.ScreenUpdating = False
    5.     With ActiveWorkbook
    6.         'assign cells to an array
    7.         iArray = .Sheets("Input").Range("A2:M15000")
    8.         nRows = UBound(iArray, 1)
    9.         nCols = UBound(iArray, 2)
    10.         'assign array to cells
    11.         .Sheets("Output").Range("A2:M15000") = iArray                   'method 1
    12.         .Sheets("Output").Range("A5").Resize(nRows, nCols) = iArray     'method 2
    13.     End With
    14.     Application.ScreenUpdating = True

    http://www.vbforums.com/showthread.php?t=391261

    In the above snippet, you'll need to be careful with nRows & nCols. I can't remember off the top of my head whether the array is 0 or 1 based. Also, turning screen updating off can be a big time saver.
    Last edited by VBAhack; Jul 17th, 2006 at 08:29 PM.

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: How can I maipulate this basic code without going into a loop

    This may help ... from the previous example ... assign only the used cells to your array:
    Code:
        'Somewhere do this
        Dim aSht As Worksheet
        Set aSht = ActiveWorkbook.Sheets("Input")
        '     ...
        'assign cells to an array - from "A2" to Last Used Cell
        iArray = aSht.Range(aSht.Cells(2, "A"), aSht.Cells.SpecialCells(xlCellTypeLastCell))
    Use "method 2" from the previous example. Thanks and credits to VBAhack.
    Last edited by Webtest; Jul 18th, 2006 at 07:35 AM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: How can I maipulate this basic code without going into a loop

    VB Code:
    1. Sheets("Output1").Rows("2:" & ActiveSheet.UsedRange.Count).Select
    2. Selection.Copy
    3. Workbooks("Master").Sheets(1).Select
    4. Range("A5").Select
    5. ActiveSheet.Paste
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: How can I maipulate this basic code without going into a loop

    Static:

    You probably mean:
    Code:
    Sheets("Output1").Rows("2:" & ActiveSheet.UsedRange.Rows.Count).Select
    Nice approach ... the "UsedRange" limits the selection to the proper number of columns.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: How can I maipulate this basic code without going into a loop

    Hi Guys,

    What a fine bit of coding. This is trully excellent.

    I must admit the LBOUND is over my head in term of technicality. Don't reallly understand this. I might stick with the used range.

    Thanks for the advice, this is great post in term of usefulness.

    Borris.

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: [RESOLVED] How can I maipulate this basic code without going into a loop

    Just type "lbound" into an otherwise empty Macro, put the cursor somewhere in the word, and hit the <F1> key. The HelpHeap will pop up an explanation of the Function. LBound is just the lowest index number for an array ... generally zero, sometimes 1.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: [RESOLVED] How can I maipulate this basic code without going into a loop

    and to add to that.. UBOUND() is the upper index of an array

    Example:
    VB Code:
    1. Dim sArray(1 to 10) as String
    2. Msgbox LBound(sArray)
    3. Msgbox UBound(sArray)
    the first msgbox will give you "1"
    the second will give you "10"
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: [RESOLVED] How can I maipulate this basic code without going into a loop

    VB Code:
    1. nRows = UBound(iArray, 1)
    2.         nCols = UBound(iArray, 2)
    3.         'assign array to cells
    4.         .Sheets("Output").Range("A2:M15000") = iArray                   'method 1
    5.         .Sheets("Output").Range("A5").Resize(nRows, nCols) = iArray

    Hey guys,

    Does the 1 and 2 of UBOUND's formulas refer to the inteligence if telling the computer set the highest index of 1 = rows, 2 = columns.


    Is this siimilar principle to:-

    Dim A(1 To 100, 0 To 3)

    1to 100 = rows, 0 to 3 = columns.


    What about resize?

    Thanks,
    Borris



    Dim

  10. #10
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: [RESOLVED] How can I maipulate this basic code without going into a loop

    Your question should probably should be a separate thread, but:

    VB Code:
    1. Dim intArray() As Integer
    2.    
    3.     ReDim intArray(0 To 5) As Integer                       'values initialized to 0
    4.     intArray(1) = 23
    5.     Debug.Print UBound(intArray), intArray(1)               '==> 5  23
    6.    
    7.     ReDim intArray(1 To 10) As Integer                      'values initialized to 0
    8.     Debug.Print UBound(intArray), intArray(1)               '==> 10 0
    9.    
    10.     ReDim intArray(0 To 5, 0 To 10) As Integer              'values initialized to 0
    11.     Debug.Print UBound(intArray, 1), UBound(intArray, 2)    '==> 5 10
    12.    
    13.     ReDim intArray(1 To 10, 5 To 20) As Integer             'values initialized to zero
    14.     Debug.Print UBound(intArray, 1), UBound(intArray, 2)    '==> 10 20
    15.     Debug.Print intArray(1, 5)                              '==> 0
    16.    
    17.     intArray(1, 5) = 16
    18.     ReDim Preserve intArray(1 To 10, 5 To 50) As Integer    'values NOT initialized
    19.     Debug.Print UBound(intArray, 1), UBound(intArray, 2)    '==> 10 50
    20.     Debug.Print intArray(1, 5)                              '==> 16
    21.    
    22. '    ReDim Preserve intArray(1 To 30, 5 To 50) As Integer   'error - can't change 1st dimension
    Last edited by VBAhack; Jul 20th, 2006 at 10:51 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
  •  



Click Here to Expand Forum to Full Width