Bỏ qua nội dung chính

Làm cách nào để trích xuất văn bản dựa trên màu phông chữ từ một ô trong Excel?

Nếu bạn có danh sách dữ liệu với một số văn bản màu đỏ trong mỗi ô trong Excel như được hiển thị như ảnh chụp màn hình bên dưới và bạn có biết cách chỉ trích xuất văn bản màu đỏ không? Bây giờ tôi sẽ giới thiệu một cách nhanh chóng để giải câu đố trích xuất văn bản dựa trên màu phông chữ từ một ô trong Excel.

doc-extract-text-color-1

Trích xuất văn bản dựa trên màu phông chữ từ mỗi ô


mũi tên màu xanh bong bóng bên phải Trích xuất văn bản dựa trên màu phông chữ từ mỗi ô

Trong Excel, bạn chỉ có thể sử dụng hàm đã xác định để trích xuất văn bản dựa trên màu phông chữ.

1. nhấn Alt + F11 các phím với nhau để mở cửa sổ Microsoft Visual Basic for Applications.

2. nhấp chuột Chèn > Mô-đun và sao chép mã VBA theo sau vào cửa sổ bật ra.

VBA: Trích xuất văn bản dựa trên màu phông chữ

Function GetColorText(pRange As Range) As String
'UpdatebyExtendoffice20220621
Dim xOut As String
Dim xValue As String
Dim i As Long
Dim TextColor
TextColor = RGB(255, 0, 0) 'colorindex RGB
xValue = pRange.Text
For i = 1 To VBA.Len(xValue)
  If pRange.Characters(i, 1).Font.Color = TextColor Then
  xOut = xOut & VBA.Mid(xValue, i, 1)
  End If
Next
GetColorText = xOut
End Function

3. Sau đó lưu và đóng hộp thoại, đồng thời chọn một ô trống bên cạnh danh sách dữ liệu, nhập công thức này = GetColorText (A1) (A1 cho biết ô bạn muốn trích xuất văn bản), nhấn đăng ký hạng mục thi để nhận văn bản cần thiết, sau đó kéo nút điều khiển tự động điền để điền công thức vào phạm vi bạn muốn.

Bây giờ bạn có thể thấy tất cả các văn bản màu đỏ được trích xuất.

doc-extract-text-color-2

Mẹo: Trong mã VBA ở trên, bạn có thể thay đổi màu rgb trong tập lệnh này TextColor = RGB (255, 0, 0) thành màu khác để đáp ứng nhu cầu của bạn.


Các bài báo tương đối:

Công cụ năng suất văn phòng tốt nhất

🤖 Trợ lý AI của Kutools: Cách mạng hóa việc phân tích dữ liệu dựa trên: Thực thi thông minh   |  Tạo mã  |  Tạo công thức tùy chỉnh  |  Phân tích dữ liệu và tạo biểu đồ  |  Gọi các hàm Kutools...
Các tính năng phổ biến: Tìm, đánh dấu hoặc xác định các bản sao   |  Xóa hàng trống   |  Kết hợp các cột hoặc ô mà không làm mất dữ liệu   |   Vòng không có công thức hữu ích. Cảm ơn !
Siêu tra cứu: Nhiều tiêu chí VLookup    VLookup Nhiều Giá Trị  |   VLookup trên nhiều trang tính   |   Tra cứu mờ ....
Danh sách thả xuống nâng cao: Tạo nhanh danh sách thả xuống   |  Danh sách thả xuống phụ thuộc   |  Danh sách thả xuống nhiều lựa chọn ....
Trình quản lý cột: Thêm một số cột cụ thể  |  Di chuyển cột  |  Chuyển đổi trạng thái hiển thị của các cột ẩn  |  So sánh dãy và cột hữu ích. Cảm ơn !
Các tính năng nổi bật: Tiêu điểm lưới   |  Chế độ xem thiết kế   |   Thanh công thức lớn    Trình quản lý sổ làm việc & trang tính   |  Thư viện tài nguyên (Văn bản tự động)   |  Bảng chọn ngày   |  Kết hợp các bảng tính   |  Mã hóa/Giải mã ô    Gửi email theo danh sách   |  Siêu lọc   |   Bộ lọc đặc biệt (lọc in đậm/nghiêng/gạch ngang...) ...
15 bộ công cụ hàng đầu12 bản văn CÔNG CỤ (thêm văn bản, Xóa ký tự,...)   |   50 + Biểu đồ Các loại (Biểu đồ Gantt,...)   |   40+ Thực tế Công thức (Tính tuổi dựa trên ngày sinh,...)   |   19 chèn CÔNG CỤ (Chèn mã QR, Chèn ảnh từ đường dẫn,...)   |   12 Chuyển đổi CÔNG CỤ (Số thành từ, Chuyển đổi tiền tệ,...)   |   7 Hợp nhất & Tách CÔNG CỤ (Các hàng kết hợp nâng cao, Chia ô,...)   |   ... và nhiều hơn nữa

Nâng cao kỹ năng Excel của bạn với Kutools for Excel và trải nghiệm hiệu quả hơn bao giờ hết. Kutools for Excel cung cấp hơn 300 tính năng nâng cao để tăng năng suất và tiết kiệm thời gian.  Bấm vào đây để có được tính năng bạn cần nhất...

Mô tả


Tab Office mang lại giao diện Tab cho Office và giúp công việc của bạn trở nên dễ dàng hơn nhiều

  • Cho phép chỉnh sửa và đọc theo thẻ trong Word, Excel, PowerPoint, Publisher, Access, Visio và Project.
  • Mở và tạo nhiều tài liệu trong các tab mới của cùng một cửa sổ, thay vì trong các cửa sổ mới.
  • Tăng 50% năng suất của bạn và giảm hàng trăm cú nhấp chuột cho bạn mỗi ngày!
Comments (14)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I have a particular text in a cell seperated by Comma and Having colour for each text. While using Delimiter funtion,I am unable to retain the font colour of text .Appraciate your help!
This comment was minimized by the moderator on the site
Hi, jdhjjd, I do not understand your question clearly. Do you want to extract the specific text from cells and keep its font color as below screenshot shown?
https://www.extendoffice.com/images/stories/comments/sun-comment/split_and_keep_font_color.png
or split cells and keep each text font color?
https://www.extendoffice.com/images/stories/comments/sun-comment/split_and_keep_font_color-2.png
This comment was minimized by the moderator on the site
I was still unable to extract the blue text (#1166BB). Is there a way to use the Hex: #1166BB to get the color text I need?
This comment was minimized by the moderator on the site
Hi, Robert E Perez, I have updated the VBA code, now, it supports to get colored text by rgb code. You can try the code again.
This comment was minimized by the moderator on the site
The code works well if the red strings are continuous. In case they are separated in the source cell, they are glued to each other in output cell. I mean there is no space in the output if the red text strings are away from each other. Can you please provide a solution for this?
This comment was minimized by the moderator on the site
Try this, it should work
Function GetColorText(pRange As Range) As String
'Updatedtogetmultiple
Dim xOut As String
Dim xValue As String
Dim i As Long
Dim wasRed As Boolean
xValue = pRange.Text

For i = 1 To VBA.Len(xValue)

If pRange.Characters(i, 1).Font.Color = vbRed Then
xOut = xOut & VBA.Mid(xValue, i, 1)
wasRed = True
ElseIf wasRed = True Then
wasRed = False
xOut = xOut & ";"
End If

Next

GetColorText = xOut
End Function

This comment was minimized by the moderator on the site
Thanks a ton it worked perfectly well. You are a savior Tim :-) 
This comment was minimized by the moderator on the site
thanks, very interesting code.
This comment was minimized by the moderator on the site
dimenticavo io ho Excel 2010
This comment was minimized by the moderator on the site
Buonasera, grazie mille un'ottima funzione. Però ho un problema, quando val nel foglio ed inserisco es. = GetColorText (A1) mi restituisce il valore giusto però come riavvio il file mi da errore #VALORE!, se poi clicco 2 volte sopra mi da il valore corretto e così via. che posso fare?
This comment was minimized by the moderator on the site
Hey, Thanks for the Tip. Works great with Red and Black.
What if I want to parse "Purple"? changing it vbPurple does not work.
Thanks
This comment was minimized by the moderator on the site
The code works well if the red strings are continuous. In case they are separated in the source cell, they are glued to each other in output cell. I mean there is no space in the output if the red text strings are away from each other. Can you please provide a solution for this?
This comment was minimized by the moderator on the site
Hello, when i tried using vbBlue, it did not work. Help please? Thanks!
This comment was minimized by the moderator on the site
Give the font color to the exct blue color.
Because there are many type of blue color "light blue, sky blue, dark blue"
vba match only exct color.??????
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations