VBA checking colour macro

  • Thread starter Thread starter Legacy 93538
  • Start date Start date
L

Legacy 93538

Guest
Hi

Does anyone know how to write an if statement which checks the colour of a cell range?

I ahve a loop which loops through a cell range but its not checking the right colour. I know RGA codes for the colour i want it to check but i am not sure how to use RGA in this loop.

Code:
    If Selection.Interior.Color = 65535 Then
        Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "True"
    Else: Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "False"
    End If

Can anyone help me?

Thanks

Jessicaseymour
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi

I had a go at creating a loop if statement which should look at the cell formatting and determine what whether its a number or a percentage but it doesn't it just returns an error saying "else without if"

Code:
For Each cell In Sheets("PPIIIFORM").Range("F4:U644")
    If cell.NumberFormat = "%" Then Sheets("Input_Reference_Table").Cells(Nrow, 8).Value = "%10.0%%"
    Else: If cell.NumberFormat = "o" Then Sheets("Input_Reference_Table").Cells(Nrow, 8).Value = "%10.0%"
    Else: Sheets("Input_Reference_Table").Cells(Nrow, 8).Value = "false"
    Nrow = Nrow + 1
    Application.StatusBar = cell.Address
Next cell
 
Upvote 0
Perhaps like this

Rich (BB code):
For Each cell In Sheets("PPIIIFORM").Range("F4:U644")
    If Right(cell.NumberFormat, 1) = "%" Then
        Sheets("Input_Reference_Table").Cells(Nrow, 8).Value = "%10.0%%"
    ElseIf Right(cell.NumberFormat, 1) = "0" Then
        Sheets("Input_Reference_Table").Cells(Nrow, 8).Value = "%10.0%"
    Else
        Sheets("Input_Reference_Table").Cells(Nrow, 8).Value = False
    End If
    Nrow = Nrow + 1
    Application.StatusBar = cell.Address
Next cell
 
Upvote 0
Hi

Working now but hwot to say if its a percentage and has a decimal place and then if it does have the value "%10.(whatever the percentage is)%%"
 
Upvote 0
See if this helps - you might want to code something like this as a subroutine to save having oodles of code within your lop

Code:
Sub test()
Dim Nfmt As String, NDec As Long
Nfmt = "0.000%"
If Nfmt Like "*.*" Then ' percentage with decimals
    NDec = Len(Nfmt) - InStr(Nfmt, ".") - 1
    MsgBox "Format is " & NDec & " decimals"
Else
    MsgBox "No decimals"
End If
End Sub
 
Upvote 0
Like this:

Code:
For Each cell In Sheets("PPIIIFORM").Range("F4:U644")
    Sheets("Input_Reference_Table").Cells(Nrow, 8).Value = ParseNFmt(cell.NumberFormat)
    Nrow = Nrow + 1
    Application.StatusBar = cell.Address
Next cell

then after End Sub

Code:
Function ParseNFmt(NFmt As String) As String
Dim NDec As Long, NType As String
NType = IIf(Right(NFmt, 1) = "%", "Percent", "Number")
If NFmt Like "*.*" Then ' has decimals
    NDec = Len(NFmt) - InStr(NFmt, ".")
    If NType = "Percent" Then NDec = NDec - 1
Else
    NDec = 0
End If
ParseNFmt = NType & " " & NDec & " decimals"
End Function

Incidentally, why are you checking columns F to U but returning all the results to column H of the other sheet?
 
Upvote 0
Hi

I have it working now thanks it works perfectly!!!

Thank you so much for all your help its greatly appricated!!!

:biggrin: :) :biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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