Loncat ke daftar isi utama

Bagaimana cara mengubah nilai berdasarkan warna sel di Excel?

Artikel ini membahas tentang mengubah nilai sel secara otomatis berdasarkan warna latar belakangnya di Excel. Misalnya, jika sel yang diisi dengan warna latar belakang merah, isi sel tersebut dengan angka 1, dan untuk sel warna latar belakang biru, isikan angka 0.

Ubah nilai berdasarkan warna sel dengan kode VBA


Ubah nilai berdasarkan warna sel dengan kode VBA

Anda dapat menjalankan kode VBA di bawah ini untuk mengubah nilai berdasarkan warna sel di Excel. Silakan lakukan sebagai berikut.

1. Pilih kisaran yang Anda perlukan untuk mengubah nilai berdasarkan warna latar belakang, lalu tekan lain + F11 tombol secara bersamaan untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. Dalam Microsoft Visual Basic untuk Aplikasi window, klik Menyisipkan > Modul, lalu salin dan tempel kode VBA di bawah ini ke jendela Modul.

Kode VBA: Ubah nilai berdasarkan warna sel di Excel

Sub ChangeValueBasedOnCellColor()
    Dim rg As Range
    Dim xRg As Range
    Set xRg = Selection.Cells
    Application.DisplayAlerts = False
    For Each rg In xRg
        With rg
            Select Case .Interior.Color
                Case Is = 255 'Red
                    .Value = 1
                Case Is = 15773696 'Blue
                    .Value = 0
            End Select
        End With
    Next
    Application.DisplayAlerts = False
End Sub

3. tekan F5 kunci untuk menjalankan kode, maka Anda dapat melihat semua sel darah merah dalam rentang yang dipilih diisi dengan angka 1, dan sel biru diisi dengan angka 0 seperti gambar di bawah ini.

 

Alat Produktivitas Kantor Terbaik

馃 Kutools AI Ajudan: Merevolusi analisis data berdasarkan: Eksekusi Cerdas   |  Hasilkan Kode  |  Buat Rumus Khusus  |  Analisis Data dan Hasilkan Grafik  |  Aktifkan Fungsi Kutools...
Fitur Populer: Temukan, Sorot, atau Identifikasi Duplikat   |  Hapus Baris Kosong   |  Gabungkan Kolom atau Sel tanpa Kehilangan Data   |   Putaran tanpa Formula ...
Pencarian Super: VLookup Beberapa Kriteria    VLookup Nilai Berganda  |   VLookup di Beberapa Lembar   |   Pencarian Fuzzy ....
Daftar Drop-down Lanjutan: Buat Daftar Drop Down dengan Cepat   |  Daftar Drop Down yang Bergantung   |  Multi-pilih Drop Down List ....
Manajer Kolom: Tambahkan Jumlah Kolom Tertentu  |  Pindahkan Kolom  |  Alihkan Status Visibilitas Kolom Tersembunyi  |  Bandingkan Rentang & Kolom ...
Fitur Unggulan: Fokus Kisi   |  Tampilan Desain   |   Bar Formula Besar    Manajer Buku Kerja & Lembar   |  Perpustakaan Sumberdaya (Teks otomatis)   |  Pemetik tanggal   |  Gabungkan Lembar Kerja   |  Enkripsi/Dekripsi Sel    Kirim Email berdasarkan Daftar   |  Filter Super   |   Filter Khusus (filter tebal/miring/coret...) ...
15 Perangkat Teratas12 Teks Tools (Tambahkan Teks, Hapus Karakter, ...)   |   50 + Grafik jenis (Gantt Chart, ...)   |   40+ Praktis Rumus (Hitung usia berdasarkan ulang tahun, ...)   |   19 Insersi Tools (Masukkan Kode QR, Sisipkan Gambar dari Jalur, ...)   |   12 Konversi Tools (Angka ke Kata, Konversi Mata Uang, ...)   |   7 Gabungkan & Pisahkan Tools (Lanjutan Gabungkan Baris, Pisahkan Sel, ...)   |   ... dan banyak lagi

Tingkatkan Keterampilan Excel Anda dengan Kutools for Excel, dan Rasakan Efisiensi yang Belum Pernah Ada Sebelumnya. Kutools for Excel Menawarkan Lebih dari 300 Fitur Lanjutan untuk Meningkatkan Produktivitas dan Menghemat Waktu.  Klik Di Sini untuk Mendapatkan Fitur yang Paling Anda Butuhkan...

Deskripsi Produk


Tab Office Membawa antarmuka Tab ke Office, dan Membuat Pekerjaan Anda Jauh Lebih Mudah

  • Aktifkan pengeditan dan pembacaan tab di Word, Excel, PowerPoint, Publisher, Access, Visio, dan Project.
  • Buka dan buat banyak dokumen di tab baru di jendela yang sama, bukan di jendela baru.
  • Meningkatkan produktivitas Anda sebesar 50%, dan mengurangi ratusan klik mouse untuk Anda setiap hari!
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Is there a way to do this, but have it automatically go through multiple files?
For context, questionnaires were sent to a couple hundred facilities, their MC answer selected corresponds to a color indicating score (e.g. green is good, red is bad), and change the content of the cell (with a process like in this thread) to something that R or another software could read, and then use that to generate automatic reports. I could go through each of these files with this macro (or a completely different idea), but that would take a long time.
This comment was minimized by the moderator on the site
The code works fine for me, but I would need to rewrite the code to fit colours specific for my document. But I don麓t know what code "my colours" have, anyone who know where on can find codes for other colours?
This comment was minimized by the moderator on the site
I came across this lucky strike which was helpful but don't know where to find a full directory!
http://cdn-0.access-excel.tips/wp-content/uploads/2015/08/excel_vbcolor_10.png 
This comment was minimized by the moderator on the site
How do you run it, i run but nothing seems to happen
This comment was minimized by the moderator on the site
You can replace colors to values by simply using Find and Replace function. Go to the format function on the right side of the Find and Replace dialog box and then under "Fill" you can select the color that you have. Then on the "replace with" just write the value you want.
This comment was minimized by the moderator on the site
is it possible to adjust this code based on the cells conditional formatting rule?
This comment was minimized by the moderator on the site
Excel seems to crash whenever I try to run the VBA code. Seems like a bust.
This comment was minimized by the moderator on the site
Good day,
The code works well in my case. Can you tell me your Excel version? Thank you for your comment.
This comment was minimized by the moderator on the site
I'm in the same boat, trying to run this but the code seems to get hung. Excel 2016.
This comment was minimized by the moderator on the site
I'm looking to give a cell a name based on the colour within one worksheet. i.e. if a cell is red then it gets named "name", if it is orange then it gets names "surname" etc.



What code would need to change to have the range as the whole sheet/tab and a name instead of a value?
This comment was minimized by the moderator on the site
What changes to the code would be needed if you wanted this to apply to the text color of a cell, rather than the cell color?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations