le thi thuy 3013
Thành viên mới
- Tham gia
- 16/10/18
- Bài viết
- 19
- Được thích
- 1
Nhờ các cao thủ sửa giúp em với. Please Please!!! Em muốn tìm kiếm trong cột A của sheet 1 các mã hàng trong sheet 2 rồi đưa các mã này sang cột B. Em chạy thử thì chạy được vài dòng rồi báo lỗi "Application-defined or object defined error"
Sub Macro1()
'
' Keyboard Shortcut: Ctrl+w
'
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb = Application.ActiveWorkbook
Set ws1 = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
Dim i As String
Dim Rng, LastCell As Range
Dim FirstAddress As String
For k = 2 To 10
i = ws2.Cells(k, 1).Value
Set LastCell = ws1.Cells(Selection.Cells.Count)
Set Rng = ws1.Cells.Find(What:=i, After:=LastCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Rng.Offset(0, 1).Value = i
Do
ws1.Columns("A:A").Select
Set Rng = Selection.FindNext(Rng)
Rng.Offset(0, 1).Value = i
Loop While FirstAddress <> Rng.Address
End If
Next k
End Sub
Sub Macro1()
'
' Keyboard Shortcut: Ctrl+w
'
Dim wb As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb = Application.ActiveWorkbook
Set ws1 = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
Dim i As String
Dim Rng, LastCell As Range
Dim FirstAddress As String
For k = 2 To 10
i = ws2.Cells(k, 1).Value
Set LastCell = ws1.Cells(Selection.Cells.Count)
Set Rng = ws1.Cells.Find(What:=i, After:=LastCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Rng.Offset(0, 1).Value = i
Do
ws1.Columns("A:A").Select
Set Rng = Selection.FindNext(Rng)
Rng.Offset(0, 1).Value = i
Loop While FirstAddress <> Rng.Address
End If
Next k
End Sub