khibennhau
Thành viên mới

- Tham gia
- 7/1/12
- Bài viết
- 24
- Được thích
- 1
Chào các bác,
Giả sử mình có một sheet có tên "Data" dữ liệu nhập theo ngày tháng năm, ở sheet "report" mình muốn mọi dữ liệu trong khoảng thời gian mình chọn sẽ xuất hiện ở sheet này. Mình phải làm sao, mong mọi người giúp. Cảm ơn mọi người nhiều.
Dùng công thức thì thử cái này
sheet report
A5=IFERROR(INDIRECT("data!"&ADDRESS(SUMPRODUCT(LARGE((data!$A$3:$A$9>=$B$2)*(data!$A$3:$A$9<=$D$2)*ROW(data!$A$3:$A$9),ROWS($A$5:A5))),COLUMN())),"")
copy xuống dưới và sang ngang
Bạn có thể dùng công cụ Advanced Filter của Excel để trích lọc dữ liệu.
Nếu chưa biết sử dụng AF thì tham khảo link sau: http://www.giaiphapexcel.com/forum/...ìm-hiểu-trích-lọc-dữ-liệu-với-Advanced-Filter
Nếu muốn code thì thử đoạn này
Mã:Option Explicit Sub Loc() Application.ScreenUpdating = False Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date Dim r As Long, i As Long, j As Long Dk1 = Sheet2.[B2].Value Dk2 = Sheet2.[D2].Value DL = Sheet1.Range("A2:D65000") For r = 2 To UBound(DL) If DL(r, 1) >= Dk1 And Dk1 <> Empty And DL(r, 1) <= Dk2 And Dk2 <> Empty Then i = i + 1 For j = 1 To 4 kq(i, j) = DL(r, j) Next j End If Next r If i Then Sheet2.Range("A5:D65000").ClearContents Sheet2.Range("A5").Resize(i, 4) = kq Else Sheet2.Range("A5:D65000").ClearContents End If Application.ScreenUpdating = True End Sub
Bạn có thể giúp mình thêm xíu, cũng điều kiện lọc như trên, giờ mình muốn lọc thêm "venue" trong bảng đó thì phải sửa code như thế nào bạn. Cảm ơn bạn nhiều.Bạn có thể dùng công cụ Advanced Filter của Excel để trích lọc dữ liệu.
Nếu chưa biết sử dụng AF thì tham khảo link sau: http://www.giaiphapexcel.com/forum/...ìm-hiểu-trích-lọc-dữ-liệu-với-Advanced-Filter
Nếu muốn code thì thử đoạn này
Mã:Option Explicit Sub Loc() Application.ScreenUpdating = False Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date Dim r As Long, i As Long, j As Long Dk1 = Sheet2.[B2].Value Dk2 = Sheet2.[D2].Value DL = Sheet1.Range("A2:D65000") For r = 2 To UBound(DL) If DL(r, 1) >= Dk1 And Dk1 <> Empty And DL(r, 1) <= Dk2 And Dk2 <> Empty Then i = i + 1 For j = 1 To 4 kq(i, j) = DL(r, j) Next j End If Next r If i Then Sheet2.Range("A5:D65000").ClearContents Sheet2.Range("A5").Resize(i, 4) = kq Else Sheet2.Range("A5:D65000").ClearContents End If Application.ScreenUpdating = True End Sub
[GPECODE=vb]
Option Explicit
Sub Loc()
Application.ScreenUpdating = False
Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date
Dim r As Long, i As Long, j As Long, Dk3 As String
Dk1 = Sheet2.[B2].Value
Dk2 = Sheet2.[D2].Value
Dk3 = Sheet2.[F2].Value
DL = Sheet1.Range("A265000")
For r = 2 To UBound(DL)
If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 And DL(r, 3) = Dk3 Then
i = i + 1
For j = 1 To 4
kq(i, j) = DL(r, j)
Next j
End If
Next r
If i Then
Sheet2.Range("A565000").ClearContents
Sheet2.Range("A5").Resize(i, 4) = kq
Else
Sheet2.Range("A565000").ClearContents
End If
Application.ScreenUpdating = True
End Sub
[/GPECODE]
Nói vậy tôi không hiểu. Làm vào file. Gửi lên kèm giải thích thì mới có thể hiểu và giúp cho bạn dc
Ở sheet "data", cột "Date" có những ngày có nhiều sự kiện, mình "merge" những ngày đó lại. Khi chạy đoạn code thì nó chỉ hiện dữ liệu đầu tiên của ngày mà nó tìm thấy. Ví dụ: ngày 15/02/2015 có 4 sự kiện, khi chạy code thì nó chỉ hiện 1 sự kiện đầu tiên.
Mình muốn nó xuất dữ liệu giống như bên sheet "data" thì phải làm sao. Cảm ơn bạn.
Ở sheet "data", cột "Date" có những ngày có nhiều sự kiện, mình "merge" những ngày đó lại. Khi chạy đoạn code thì nó chỉ hiện dữ liệu đầu tiên của ngày mà nó tìm thấy. Ví dụ: ngày 15/02/2015 có 4 sự kiện, khi chạy code thì nó chỉ hiện 1 sự kiện đầu tiên.
Mình muốn nó xuất dữ liệu giống như bên sheet "data" thì phải làm sao. Cảm ơn bạn.
Option Explicit
Sub Loc()
Application.ScreenUpdating = False
Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date
Dim r As Long, i As Long, j As Long
Dk1 = Sheet2.[B2].Value
Dk2 = Sheet2.[D2].Value
With Sheet1
DL = .Range(.[a3], .[d60000].End(3))
End With
For r = 2 To UBound(DL)
If IsEmpty(DL(r, 1)) Then DL(r, 1) = DL(r - 1, 1)
If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 Then
i = i + 1
For j = 1 To 4
kq(i, j) = DL(r, j)
Next j
End If
Next r
If i Then
Sheet2.Range("A5:D65000").ClearContents
Sheet2.Range("A5").Resize(i, 4) = kq
Else
Sheet2.Range("A5:D65000").ClearContents
End If
Application.ScreenUpdating = True
End Sub
ngồi buồi quá, nhiều chuyện chút
code trong file bạn, chỉnh lại chút
Mã:Option Explicit Sub Loc() Application.ScreenUpdating = False Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date Dim r As Long, i As Long, j As Long Dk1 = Sheet2.[B2].Value Dk2 = Sheet2.[D2].Value With Sheet1 DL = .Range(.[a3], .[d60000].End(3)) End With For r = 2 To UBound(DL) If IsEmpty(DL(r, 1)) Then DL(r, 1) = DL(r - 1, 1) If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 Then i = i + 1 For j = 1 To 4 kq(i, j) = DL(r, j) Next j End If Next r If i Then Sheet2.Range("A5:D65000").ClearContents Sheet2.Range("A5").Resize(i, 4) = kq Else Sheet2.Range("A5:D65000").ClearContents End If Application.ScreenUpdating = True End Sub
ngồi buồi quá, nhiều chuyện chút
code trong file bạn, chỉnh lại chút
Mã:Option Explicit Sub Loc() Application.ScreenUpdating = False Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date Dim r As Long, i As Long, j As Long Dk1 = Sheet2.[B2].Value Dk2 = Sheet2.[D2].Value With Sheet1 DL = .Range(.[a3], .[d60000].End(3)) End With For r = 2 To UBound(DL) If IsEmpty(DL(r, 1)) Then DL(r, 1) = DL(r - 1, 1) If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 Then i = i + 1 For j = 1 To 4 kq(i, j) = DL(r, j) Next j End If Next r If i Then Sheet2.Range("A5:D65000").ClearContents Sheet2.Range("A5").Resize(i, 4) = kq Else Sheet2.Range("A5:D65000").ClearContents End If Application.ScreenUpdating = True End Sub
Người ta khoái cái "mẹc" đó.Mình muốn nó xuất dữ liệu giống như bên sheet "data" thì phải làm sao. Cảm ơn bạn.
ủa code trong file là của ai làm vậy Let' Gâu Gâu ?![]()
Có "buồn quá" thì làm theo yêu cầu này luôn đi cho "trót"
Người ta khoái cái "mẹc" đó.
Option Explicit
Sub Loc()
Application.ScreenUpdating = False
Dim DL, kq(1 To 65000, 1 To 4), Dk1, nextrow As Date, Dk2 As Date
Dim r As Long, i As Long, j As Long
Dk1 = Sheet2.[B2].Value
Dk2 = Sheet2.[D2].Value
With Sheet1
DL = .Range(.[a3], .[d60000].End(3))
End With
For r = 1 To UBound(DL)
If IsEmpty(DL(r, 1)) Then DL(r, 1) = DL(r - 1, 1)
If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 Then
i = i + 1
If DL(r, 1) <> nextrow Then kq(i, 1) = DL(r, 1)
For j = 2 To 4
kq(i, j) = DL(r, j)
Next j
End If
nextrow = DL(r, 1)
Next r
If i Then
Sheet2.Range("A5:D65000").ClearContents
Sheet2.Range("A5").Resize(i, 4) = kq
Else
Sheet2.Range("A5:D65000").ClearContents
End If
Application.ScreenUpdating = True
End Sub
Code thì hãy nói không với chuyện merge cell đi nha...
[GPECODE=vb]
Option Explicit
Sub Loc()
Application.ScreenUpdating = False
Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date
Dim r As Long, i As Long, j As Long, Dk3 As String
Dk1 = Sheet2.[B2].Value
Dk2 = Sheet2.[D2].Value
Dk3 = Sheet2.[F2].Value
DL = Sheet1.Range("A265000")
For r = 2 To UBound(DL)
If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 And DL(r, 3) = Dk3 Then
i = i + 1
For j = 1 To 4
kq(i, j) = DL(r, j)
Next j
End If
Next r
If i Then
Sheet2.Range("A565000").ClearContents
Sheet2.Range("A5").Resize(i, 4) = kq
Else
Sheet2.Range("A565000").ClearContents
End If
Application.ScreenUpdating = True
End Sub
[/GPECODE]
[GPECODE=vb]
Option Explicit
Sub Loc()
Application.ScreenUpdating = False
Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date
Dim r As Long, i As Long, j As Long, Dk3 As String
Dk1 = Sheet2.[B2].Value
Dk2 = Sheet2.[D2].Value
Dk3 = Sheet2.[F2].Value
With Sheet1
DL = .Range(.[A3], .[D65000].End(3))
End With
For r = 2 To UBound(DL)
If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 And (DL(r, 3) = Dk3 Or Dk3 = Empty) Then
i = i + 1
For j = 1 To 4
kq(i, j) = DL(r, j)
Next j
End If
Next r
With Sheet2
If i Then
.Range("A565000").ClearContents
.Range("A5").Resize(i, 4) = kq
Else
.Range("A565000").ClearContents
End If
End With
Application.ScreenUpdating = True
End Sub
[/GPECODE]
Public Sub hello()
With Worksheets("report")
.[H2].Value = "=AND( data!A3>=$B$2,data!A3<=$D$2,OR($F$2="""",data!C3=$F$2))"
Sheet1.Range("A2:D" & Sheet1.[C1000000].End(xlUp).Row).AdvancedFilter xlFilterCopy, _
.[H1:H2], .[A4:D4]
.[H2].ClearContents
End With
End Sub
[GPECODE=vb]
Option Explicit
Sub Loc()
Application.ScreenUpdating = False
Dim DL, kq(1 To 65000, 1 To 4), Dk1 As Date, Dk2 As Date
Dim r As Long, i As Long, j As Long, Dk3 As String
Dk1 = Sheet2.[B2].Value
Dk2 = Sheet2.[D2].Value
Dk3 = Sheet2.[F2].Value
With Sheet1
DL = .Range(.[A2], .[D65000].End(3))
End With
For r = 2 To UBound(DL)
If DL(r, 1) >= Dk1 And DL(r, 1) <= Dk2 And (DL(r, 3) = Dk3 Or Dk3 = Empty) Then
i = i + 1
For j = 1 To 4
kq(i, j) = DL(r, j)
Next j
End If
Next r
With Sheet2
If i Then
.Range("A565000").ClearContents
.Range("A5").Resize(i, 4) = kq
Else
.Range("A565000").ClearContents
End If
End With
Application.ScreenUpdating = True
End Sub
[/GPECODE]
code dữ quá hic
mình chỉ biết làm cách "yếu đuối" vầy thôi . hi hi
Mã:Public Sub hello() With Worksheets("report") .[H2].Value = "=AND( data!A3>=$B$2,data!A3<=$D$2,OR($F$2="""",data!C3=$F$2))" Sheet1.Range("A2:D" & Sheet1.[C1000000].End(xlUp).Row).AdvancedFilter xlFilterCopy, _ .[H1:H2], .[A4:D4] .[H2].ClearContents End With End Sub
chức năng của nó y chang #21 thôi . nhưng mà nên sử dụng đoạn code #21Bạn giải thích đoạn code này giúp mình với, mình không hiểu gì hết![]()