Consulting

Results 1 to 4 of 4

Thread: copy help

  1. #1
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location

    copy help

    Hello

    i have this macro:
    [VBA]Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Integer
    Dim LastRow As Long
    Dim NextRow As Long
    Dim mnth As Integer

    With Sheets("sheet1")

    mnth = InputBox("Supply the required month number")
    If mnth > 0 And mnth <= 12 Then

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    NextRow = 5
    For i = 6 To LastRow
    H = 0

    If Month(.Cells(i, "AU").Value) = mnth Then

    NextRow = NextRow + 1

    .Cells(i, "AV").Resize(, 1).Copy Worksheets("Sheet2").Cells(NextRow, "D")


    End If
    Next i
    End If
    End With

    End Sub[/VBA]

    how can i get it to copy only 30 rows from latest date and up from the same month

    thanks

    Oleg

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Integer
    Dim LastRow As Long
    Dim NextRow As Long
    Dim Threshold As Long
    Dim mnth As Integer

    With Sheets("sheet1")

    mnth = InputBox("Supply the required month number")
    If mnth > 0 And mnth <= 12 Then

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    NextRow = 5
    For i = 6 To LastRow
    H = 0

    If Month(.Cells(i, "AU").Value) = mnth Then

    NextRow = NextRow + 1
    .Cells(i, "AV").Resize(, 1).Copy Worksheets("Sheet2").Cells(NextRow, "D")
    Threshold = Threshold + 1
    If Threshold > 30 Then Exit For
    End If
    Next i
    End If
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    Thanks for the replay

    the macro copies from the earliest date to the latest and
    i need 30 latest parts from same month


    thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Untesteds

    [vba]

    Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Integer
    Dim LastRow As Long
    Dim Threshold As Long
    Dim mnth As Integer

    With Sheets("sheet1")

    mnth = InputBox("Supply the required month number")
    If mnth > 0 And mnth <= 12 Then

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = LastRow To 6 Step -1
    H = 0

    If Month(.Cells(i, "AU").Value) = mnth Then

    Worksheets("Sheet2").Rows(5).Insert
    .Cells(i, "AV").Resize(, 1).Copy Worksheets("Sheet2").Range("D5")
    Threshold = Threshold + 1
    If Threshold > 30 Then Exit For
    End If
    Next i
    End If
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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