VBA IF statment

PerunPL

New Member
Joined
Jun 18, 2018
Messages
6
hello,
I 'm really knew to VBA coding, and I was wondering if you guys could help.
So I was wondering if anyone could help me with this code.
I'm trying to make the if statement code for VBA, it can be either sub or function.
ok so I'm trying to make if statement that says; " if active cell background color is RGB (0,176,240), and if cell B2 says "s-1" then copy the number form A2 to C2, if it Says "s-2" copy the number to D and if it says S-3 copy the number do E"
however if the cells are not manually colored than don't copy nothing and just leave it blank. I attached the table on how the results look like. if you guys could help me with this or give me some guidance I would be really thankful. [TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]#[/TD]
[TD]"s"[/TD]
[TD]S-1[/TD]
[TD]S-2[/TD]
[TD]S-3[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]S-1[/TD]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]S-3[/TD]
[TD][/TD]
[TD][/TD]
[TD]15[/TD]
[/TR]
[TR]
[TD]87[/TD]
[TD]S-2[/TD]
[TD][/TD]
[TD]87[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]150[/TD]
[TD]S-1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]78[/TD]
[TD]S-1[/TD]
[TD]78[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
try this:
Code:
Sub test3()
If ActiveCell.Interior.Color = RGB(0, 176, 240) Then
 If Cells(2, 2) = "s-1" Then
   Cells(2, 3) = Cells(2, 1)
 End If
 If Cells(2, 2) = "s-2" Then
   Cells(2, 4) = Cells(2, 1)
 End If
 If Cells(2, 2) = "s-3" Then
   Cells(2, 5) = Cells(2, 1)
 End If
End If
 
Upvote 0
I did try it, and for some reason it just gives me blank cell. is there a way to implement range in it. for example instead of putting active cell we could just put the range C2:E5 and if any cell in that range is blue to just preform this action, and past the write number to write column. I'm sorry if it sounds confusing but in all reality I'm just trying for easier way to do it.
 
Upvote 0
The code does exactly what you specified in your first post; if it is not doing what you expect then your specfication was wrong. If you are getting a blank cell , then either the active cell is the wrong color, it has got to be exactly RGB(0, 176, 240), or you have not got s-1, s-2 or s-3 in cell B2.
Run this macro to put the correct color in A10 ,(change the address of the macor if that is an unsuitable location) and select A10 before runnig the macro ( I assume you have got one of the correct strings in B2)

Code:
Sub test()

Cells(10, 1).Interior.Color = RGB(0, 176, 240)
End Sub


Your requirements in post 3 are much more difficult to implement and are not entirely clear. so I will ignore them at the moment

 
Last edited:
Upvote 0
How about
Code:
Sub Check()
   Dim Cl As Range
   Dim c As Long
   For Each Cl In Range("A3", Range("A" & Rows.count).End(xlUp))
      If Cl.Interior.Color = RGB(0, 176, 240) Then
         c = Right(Cl.Offset(, 1), 1) + 1
         Cl.Offset(, c).Value = Cl.Value
      End If
   Next Cl
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,028
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top