C. Remarks (Một số lưu ý):
- The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you don’t specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method. If you don't, you run the risk of using the Find Method with settings you were not aware of.
Các thiết lập cho các đối số LookIn, LookAt, SearchOrder và MatchByte sẽ được lưu mỗi lần ta sử dụng phương thức này (phương thức Find). Nếu ta không khai báo giá trị cho các đối số vào lần sử dụng phương thức Find tiếp theo, Các giá trị thiết lập đã lưu trước đó sẽ được sử dụng. Việc thiết lập các đối số này làm thay đổi các tùy chọn thiết lập trong hộp thoại Find, và việc thay đổi các thiết lập trong hộp thoại Find sẽ làm thay đổi các giá trị đã lưu – là những giá trị được sử dụng nếu ta bỏ qua các đối số này. Để tránh xảy ra việc này, ta nên khai báo các đối số một cách rõ ràng/tường minh mỗi lần sử dụng phương thức Find này. Nếu các thiết lập được chỉ định không cụ thể, chính xác, bạn sẽ có một kết quả không như mong muốn.
- The After setting is also very important. Whichever cell is set here will be the last one searched and not the first as some may expect. For this reason, one should always set this explicitly each and every time you use the Find Method.
Yet another trap can be the incorrect use of the After:= setting. If the Range Object specified is NOT within the range you are using Find on, you will get an error. For example, if you wanted to find a value on another Worksheet (not the Active one), restrict the Find to, say Column A and then select the found cell, you could use;
Việc thiết lập cho tham số After cũng rất quan trọng. Bất kỳ ô nào được dùng làm tham số After cũng sẽ là ô tìm thấy lần cuối cùng của lần sử dụng trước đó, chứ không phải là ô tìm kiếm đầu tiên như nhiều người nghĩ. Vì vậy, bất cứ khi nào sừ dụng phương thức Find, bạn phải khai báo tường minh tham số này.
Một chỗ cần lưu ý khác khi thiết lập tham số After:= có thể khiến cho việc sử dụng Find bị lỗi: Nếu range được xác định trong After không nằm trong phạm vi bạn đang tìm, bạn sẽ nhận được một lỗi.
Ví dụ, nếu bạn muốn tìm một giá trị trên Worksheet khác (không phải là Sheet Active), giới hạn vùng tìm cụ thể là Cột A (Columns(1)), sau đó chọn các ô được tìm thấy, bạn dùng đoạn code sau:
Note the use of .Cells(1,1) as the After:= setting. If this was ANY cell NOT within Column A the code would normally bug-out. However, the use of On Error Resume Next prevents this. BUT, despite that you will not be taken to the cell.
Lưu ý việc sử dụng .Cells (1,1) trong phần After:=.Cells(1, 1).
Cells(1,1) luôn phải thuộc về phạm vi ta muốn tìm. Vì ta đang tìm trong sheet1, nên Cells(1,1) phải thuộc sheet1 nên có dấu chấm (.Cells (1,1)).
Hoặc thí dụ ta đang sử dụng pham vi tìm kiếm là .Columns(1) mà ta dùng After:=.Cells(1, 3); .Cells(1, 3) không thuộc Columns(1) kết quả lúc này báo lỗi.
Nếu sử dụng On Error Resume Next sẽ bỏ qua lỗi, nhưng sử dụng On Error Resume Next kết quả vẫn không được dẫn tới Cell đó.
- The example code below shows how we can use the Find Method on any Excel Worksheet to locate all occurrences of the Word "Cat" add a cell comment to each cell.
Code dưới đây chỉ cho chúng ta cách sử dụng các phương pháp Find trên bất kỳ Worksheet để xác định vị trí xuất hiện của tất cả các từ "Cat" và gán thêm 1 comment trên Cell tìm thấy.
- Note the use of the COUNTIF Worksheet Function to restrict the Find to the exact number of the times the word "Cat" appears in Column 1.
Note also the setting of a Range variable (rFoundCell) to the found cell each time the word "Cat" found. This same variable is then used in the After setting of the Find Method.
Lưu ý rằng việc sử dụng hàm COUNTIF của Excel có thể giới hạn số lần lặp cho việc tìm số lần chữ "Cát" xuất hiện ở trong cột 1.
Cũng lưu ý cách sử dụng một biến Range (rFoundCell) gán cho những ô mà từ "Cat" được tìm thấy. Biến như vậy cũng sẽ được sử dụng cho tham số After trong Phương pháp Find.
Giải thích đoạn code trên:
- To find cells that match more complicated patterns, use a For Each...Next statement with the Like operator. For example, the following code searches for all cells in the range A1:C5 that use a font whose name starts with the letters Cour. When Microsoft Excel finds a match, it changes the font to Times New Roman.
Để tìm kiếm ô khớp với các dạng thức phức tạp, hãy sử dụng vòng lặp For Each … Next với toán tử Like. Lấy ví dụ, đoạn code kèm theo sẽ tìm tất cả các ô trong vùng tìm kiếm từ A1:C5 có sử dụng font chữ có tên bắt đầu với chuỗi ký tự “Cour”. Khi Excel tìm thấy ô đó, nó sẽ đổi font của ô đó thành “Times New Roman”.
- The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you don’t specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method. If you don't, you run the risk of using the Find Method with settings you were not aware of.
Các thiết lập cho các đối số LookIn, LookAt, SearchOrder và MatchByte sẽ được lưu mỗi lần ta sử dụng phương thức này (phương thức Find). Nếu ta không khai báo giá trị cho các đối số vào lần sử dụng phương thức Find tiếp theo, Các giá trị thiết lập đã lưu trước đó sẽ được sử dụng. Việc thiết lập các đối số này làm thay đổi các tùy chọn thiết lập trong hộp thoại Find, và việc thay đổi các thiết lập trong hộp thoại Find sẽ làm thay đổi các giá trị đã lưu – là những giá trị được sử dụng nếu ta bỏ qua các đối số này. Để tránh xảy ra việc này, ta nên khai báo các đối số một cách rõ ràng/tường minh mỗi lần sử dụng phương thức Find này. Nếu các thiết lập được chỉ định không cụ thể, chính xác, bạn sẽ có một kết quả không như mong muốn.
- The After setting is also very important. Whichever cell is set here will be the last one searched and not the first as some may expect. For this reason, one should always set this explicitly each and every time you use the Find Method.
Yet another trap can be the incorrect use of the After:= setting. If the Range Object specified is NOT within the range you are using Find on, you will get an error. For example, if you wanted to find a value on another Worksheet (not the Active one), restrict the Find to, say Column A and then select the found cell, you could use;
Việc thiết lập cho tham số After cũng rất quan trọng. Bất kỳ ô nào được dùng làm tham số After cũng sẽ là ô tìm thấy lần cuối cùng của lần sử dụng trước đó, chứ không phải là ô tìm kiếm đầu tiên như nhiều người nghĩ. Vì vậy, bất cứ khi nào sừ dụng phương thức Find, bạn phải khai báo tường minh tham số này.
Một chỗ cần lưu ý khác khi thiết lập tham số After:= có thể khiến cho việc sử dụng Find bị lỗi: Nếu range được xác định trong After không nằm trong phạm vi bạn đang tìm, bạn sẽ nhận được một lỗi.
Ví dụ, nếu bạn muốn tìm một giá trị trên Worksheet khác (không phải là Sheet Active), giới hạn vùng tìm cụ thể là Cột A (Columns(1)), sau đó chọn các ô được tìm thấy, bạn dùng đoạn code sau:
PHP:
Sub FindCatOtherSheet()
Dim rFound As Range
On Error Resume Next
With Sheet1
Set rFound = .Columns(1).Find(What:="Cat", After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False , _
SearchFormat:=False)
On Error GoTo 0
If Not rFound Is Nothing Then Application.Goto rFound, True
End With
Lưu ý việc sử dụng .Cells (1,1) trong phần After:=.Cells(1, 1).
Cells(1,1) luôn phải thuộc về phạm vi ta muốn tìm. Vì ta đang tìm trong sheet1, nên Cells(1,1) phải thuộc sheet1 nên có dấu chấm (.Cells (1,1)).
Hoặc thí dụ ta đang sử dụng pham vi tìm kiếm là .Columns(1) mà ta dùng After:=.Cells(1, 3); .Cells(1, 3) không thuộc Columns(1) kết quả lúc này báo lỗi.
Nếu sử dụng On Error Resume Next sẽ bỏ qua lỗi, nhưng sử dụng On Error Resume Next kết quả vẫn không được dẫn tới Cell đó.
- The example code below shows how we can use the Find Method on any Excel Worksheet to locate all occurrences of the Word "Cat" add a cell comment to each cell.
Code dưới đây chỉ cho chúng ta cách sử dụng các phương pháp Find trên bất kỳ Worksheet để xác định vị trí xuất hiện của tất cả các từ "Cat" và gán thêm 1 comment trên Cell tìm thấy.
- Note the use of the COUNTIF Worksheet Function to restrict the Find to the exact number of the times the word "Cat" appears in Column 1.
Note also the setting of a Range variable (rFoundCell) to the found cell each time the word "Cat" found. This same variable is then used in the After setting of the Find Method.
Lưu ý rằng việc sử dụng hàm COUNTIF của Excel có thể giới hạn số lần lặp cho việc tìm số lần chữ "Cát" xuất hiện ở trong cột 1.
Cũng lưu ý cách sử dụng một biến Range (rFoundCell) gán cho những ô mà từ "Cat" được tìm thấy. Biến như vậy cũng sẽ được sử dụng cho tham số After trong Phương pháp Find.
PHP:
Sub Find_Bold_Cat()
Dim lCount As Long
Dim rFoundCell As Range
Set rFoundCell = Range("A1")
For lCount = 1 To WorksheetFunction.CountIf(Columns(1), "Cat")
Set rFoundCell = Columns(1).Find(What:="Cat", After:=rFoundCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
With rFoundCell
.ClearComments
.AddComment Text:="Cat lives here"
End With
Next lCount
End Sub
Sub Find_Bold_Cat()
‘Tìm những chữ Cat và thêm 1 comment là "Cat lives here"
‘’===========================================
‘Đếm những “Cat” trong cột A và xác định những lần tìm là kết quả đếm.
For lCount = 1 To WorksheetFunction.CountIf(Columns(1), "Cat")
‘Tìm trong cột A, xác định ô tìm thấy có chữ “Cat” gán vào biến rFoundCell
Set rFoundCell = Columns(1).Find(What:="Cat", After:=rFoundCell,
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
‘Tại Range tìm thấy, xóa các comment và gán Comment "Cat lives here"
With rFoundCell
.ClearComments
.AddComment Text:="Cat lives here"
End With
‘Tìm tiếp tục cho đến khi số lần tìm = lCount
Next
End Sub
- To find cells that match more complicated patterns, use a For Each...Next statement with the Like operator. For example, the following code searches for all cells in the range A1:C5 that use a font whose name starts with the letters Cour. When Microsoft Excel finds a match, it changes the font to Times New Roman.
Để tìm kiếm ô khớp với các dạng thức phức tạp, hãy sử dụng vòng lặp For Each … Next với toán tử Like. Lấy ví dụ, đoạn code kèm theo sẽ tìm tất cả các ô trong vùng tìm kiếm từ A1:C5 có sử dụng font chữ có tên bắt đầu với chuỗi ký tự “Cour”. Khi Excel tìm thấy ô đó, nó sẽ đổi font của ô đó thành “Times New Roman”.
PHP:
For Each Rng In [A1:C5]
If Rng.Font.Name Like "Cour*" Then
Rng.Font.Name = "Times New Roman"
End If
Next
Lần chỉnh sửa cuối: