Results 1 to 3 of 3

[SOLVED]Help with VBA Macro - Returning Entire Row

Threaded View

  1. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help with VBA Macro - Returning Entire Row

    Hello BamBamMoneyBags,

    This revision of the macro will copy the rows to "Sheet2" column "A:D". The macro has been added to the attached workbook.
    Sub Button1_Click()
    
        Dim Data As Variant
        Dim n As Integer
        Dim nMax As Double
        Dim nMin As Double
        Dim r As Long
        Dim Rng As Range
        Dim RngEnd As Range
        Dim RngOut As Range
        Dim Wks As Worksheet
        
        
            ReDim Data(1 To 4, 1 To 1)
            
            Set RngOut = Worksheets("Sheet2").Range("A1:D1")
            Set Wks = Worksheets("Sheet1")
            
            Set Rng = Wks.Range("A5")
            Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
            
            If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd).Resize(ColumnSize:=5)
            
            
            nMin = Wks.Cells(19, "B")
            nMax = Wks.Cells(20, "B")
            
                For r = 1 To Rng.Rows.Count - 1 Step 2
                    If Rng.Item(r, 5) >= nMin And Rng.Item(r + 1, 5) <= nMax Then
                        
                        n = n + 1
                        ReDim Preserve Data(1 To 4, 1 To n)
                        Data(1, n) = Rng.Item(r, 1)
                        Data(2, n) = Rng.Item(r + 1, 1)
                        Data(3, n) = Rng.Item(r, 3)
                        Data(4, n) = Rng.Item(r, 5)
                        
                        n = n + 1
                        ReDim Preserve Data(1 To 4, 1 To n)
                        Data(1, n) = Rng.Item(r, 1)
                        Data(2, n) = Rng.Item(r + 1, 1)
                        Data(3, n) = Rng.Item(r + 1, 3)
                        Data(4, n) = Rng.Item(r + 1, 5)
                        
                    End If
                Next r
            
            Data = Application.Transpose(Data)
            RngOut.Resize(UBound(Data), 4).Value = Data
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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