vumian
Mỗi bậc thang là mỗi Cell
- Tham gia
- 12/3/07
- Bài viết
- 267
- Được thích
- 186
- Nghề nghiệp
- employee only, not a boss
công thức tại C4: =COUNTIF(E$4:E$17,B4) :so sánh từng cell trong range1 có tồn tại trong rang2 hay không ?Như đã mô tả trong file, làm cách nào so sánh được 2 range như vậy,
- range1 có số dòng không bằng range2
- so sánh từng cell trong range1 có tồn tại trong rang2 hay không ?
- số dòng trong range1 được dao động
thấy dùng VBA có lẽ là tốt nhất
[b]Đã được trích dẫn hoàn toàn bên dưới bỡi tác giả đề xướng đề mục ni [/b]
Sub CompareWorksheetRanges(rng1 As Range, rng2 As Range)
Dim r As Long, c As Integer
Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim rptWB As Workbook, DiffCount As Long
If rng1 Is Nothing Or rng2 Is Nothing Then Exit Sub
If rng1.Areas.Count > 1 Or rng2.Areas.Count > 1 Then
MsgBox "Can't compare multiple selections!", _
vbExclamation, "Compare Worksheet Ranges"
Exit Sub
End If
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Set rptWB = Workbooks.Add
Application.DisplayAlerts = False
While Worksheets.Count > 1
Worksheets(2).Delete
Wend
Application.DisplayAlerts = True
With rng1
lr1 = .Rows.Count
lc1 = .Columns.Count
End With
With rng2
lr2 = .Rows.Count
lc2 = .Columns.Count
End With
maxR = lr1
maxC = lc1
If maxR < lr2 Then maxR = lr2
If maxC < lc2 Then maxC = lc2
If lr1 <> lr2 Or lc1 <> lc2 Then
If MsgBox("The two ranges you want to compare are of different size!" & _
Chr(13) & "Do you want to continue anyway?", _
vbQuestion + vbYesNo, "Compare Worksheet Ranges") = vbNo Then Exit Sub
End If
DiffCount = 0
For c = 1 To maxC
Application.StatusBar = "Comparing cells " & _
Format(c / maxC, "0 %") & "..."
For r = 1 To maxR
cf1 = ""
cf2 = ""
On Error Resume Next
cf1 = rng1.Cells(r, c).FormulaLocal
cf2 = rng2.Cells(r, c).FormulaLocal
On Error GoTo 0
If cf1 <> cf2 Then
DiffCount = DiffCount + 1
Cells(r, c).Formula = "'" & cf1 & " <> " & cf2
End If
Next r
Next c
Application.StatusBar = "Formatting the report..."
With Range(Cells(1, 1), Cells(maxR, maxC))
.Interior.ColorIndex = 19
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error Resume Next
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error GoTo 0
End With
Columns("A:IV").ColumnWidth = 20
rptWB.Saved = True
If DiffCount = 0 Then
rptWB.Close False
End If
Set rptWB = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox DiffCount & " cells contain different formulas!", _
vbInformation, "Compare Worksheet Ranges"
End Sub
Sub TestCompareWorksheetRanges()
' compare two ranges in the active worksheet in the active workbook
CompareWorksheetRanges Range("A1:A100"), Range("B1:B100")
' compare two ranges in two different worksheets in the active workbook
CompareWorksheetRanges Worksheets(1).Range("A1:A100"), _
Worksheets(2).Range("B1:B100")
' compare two ranges in two different worksheets in two different workbooks
CompareWorksheetRanges ActiveWorkbook.Worksheets(1).Range("A1:A100"), _
Workbooks("WorkBookName.xls").Worksheets(1).Range("B1:B100")
End Sub
SA_DQ đã viết:Bạn tham khảo nhé (còn 1 xíu chưa đúng, bạn tự sửa nha!)Mã:Option Explicit: Option Base 1 [B]Sub SSanh2Vung() [/B] Const lRow1 As Long = 20: Const lRow2 As Long = 30 Dim lZ As Long, lW As Long, StrC As String Dim Rng As Range, Rng0 As Range ReDim MNew(lRow1): Dim BDaCo As Boolean 1[COLOR=blue]' Xếp 2 cột dữ liệu [/COLOR] For lZ = 1 To 2 If lZ = 1 Then Set Rng = Range("E3:E" & CStr(lRow2)): StrC = "E4" Else Set Rng = Range("B3:B" & CStr(lRow1)): StrC = "B4" End If Rng.Select Selection.Sort Key1:=Range(StrC), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal Next lZ lZ = 0: Set Rng = Nothing 2[COLOR=blue]' Dò tìm ở 2 cột; đưa vô biến ~ DL thiếu [/COLOR] For Each Rng In Range("B3:B" & CStr(lRow1)) For Each Rng0 In Range("E3:E" & CStr(lRow2)) If Rng.Value = Rng0.Value Then BDaCo = Not BDaCo: Exit For End If Next Rng0 If BDaCo = True Then BDaCo = False Else lZ = lZ + 1: MNew(lZ) = Rng.Value End If Next Rng 3[COLOR=blue]' Chép từ biến mảng DL vô CSDL [/COLOR] lZ = Range("E32555").End(xlUp).Row For lW = 1 To lRow1 If Len(MNew(lW)) < 1 Then Exit For StrC = "E" & CStr(lZ + lW): Range(StrC).Value = MNew(lW) Next lW [B]End Sub [/B]
Lỗi đó là nó chép luôn tên trường của cột 'B3:B20'code của bạn mình chạy chưa tìm được lỗi, . . .
Bạn có thấy mục 3 có 2 vòng lặp & sử dụng 2 biến này trong các vòng lặp lồng nhau; Nếu bạn thấy còn ít thì khai thêm biến nữa! (&ui nha!)vấn đề là mình hôg hiểu bạn đặt biến lZ,LW để làm gì,
Vòng lặp để xếp lần lược cột B & cột E;đoạn for lZ=1 to 2 để làm gì vậy ? nếu lZ=1 thì range(E3:E30), khúc này mình hiểu, còn khúc strC="E4" để làm gì vậy, còn else ?
Làm sao để biết từng nội dung trong cột Number có tồn tại trong cột Database hay chưa ?
Nếu chưa có, hiện msgbox thông báo đang thiếu số nào hay tự động thêm vào cũng được