简体   繁体   中英

Copy a certain range from a row if conditions are met (code is done but i can't find the correct vba syntax)

So i have 7 columns, column 5 (E) is date. I am searching for all rows for the date, and copying them to sheet 2 (from 1) based on my imput. Problem is i need to only copy A,B,C,D,E but leave out the last 2 columns. What is the correct syntax for this?

Atm i have:

Rows(i).Copy Destination:=Sheets(2).Rows(Lastrowa)

but this copies my entire row instead of just the first 5 cells of the row. Full code below

Sub Check_Dtaes()
'And Format
Application.ScreenUpdating = False
On Error GoTo M
Dim i As Long
Dim ans As Date
Dim anss As Date
Dim Lastrow As Long
Dim Lastrowa As Long
ans = InputBox("Start Date Is")
anss = InputBox("End  Date Is")
Lastrow = Sheets(1).Cells(Rows.Count, "E").End(xlUp).Row
Lastrowa = Sheets(2).Cells(Rows.Count, "E").End(xlUp).Row + 1
    For i = 1 To Lastrow
        If Cells(i, "E").Value >= ans And Cells(i, "E").Value <= anss Then
            Rows(i).Copy Destination:=Sheets(2).Rows(Lastrowa)
            Lastrowa = Lastrowa + 1
        End If
    Next
    
    Sheets(2).Range("E1:E" & Lastrowa).NumberFormat = "dd/mm/yyyy"
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "You entered a inproper date"
Application.ScreenUpdating = True
End Sub

Thanks in advance

Range("A" & i & ":E" & i) // Credit to BigBen, thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM