For each fc in range.formatconditions fails. Why? - vba

The following code works for most sheets in my workbook:
Function IsHighlighted() As Boolean
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
Dim rUsedRange As Range
Set rUsedRange = wks.UsedRange
With rUsedRange
Dim bHighlighted As Boolean
Dim fc As FormatCondition
For Each fc In .FormatConditions
If fc.Interior.Color = RGB(255, 0, 0) And fc.Font.Color = RGB(255, 255, 0) Then
bHighlighted = True
Exit For
End If
Next fc
If bHighlighted = True Then
Exit For
End If
End With
Debug.Print (wks.Name & "," & rUsedRange.FormatConditions.count)
Next wks
IsHighlighted = bHighlighted
End Function
But it fails on the line For Each fc In .FormatConditions with the error message type mismatch on a worksheet that has rUsedRange.FormatConditions.Count = 34 out of which many are icon sets.
Why does the code fail on this sheet? How can I fix it?

The FormatConditions collection includes FormatCondition, DataBar, AboveAverage, ColorScale, UniqueValues, Top10 and IconSetCondition objects, not just FormatCondition objects, so you need to declare your fc variable as Object.

Related

VBA search for value on next sheet

is there I way for searching a value on the next sheet (ActiveSheet.Next.Activate) without jumping on to it?
Here the whole Code:
the problem is, it jumps to the next sheet even if there is no searched value.
Dim ws As Worksheet
Dim Loc As Range
Dim StrVal As String
Dim StrRep As String
Dim i As Integer
Private Sub CommandButton1_Click()
i = 1
Call Replacing
End Sub
Private Sub CommandButton2_Click()
i = 2
Call Replacing
End Sub
Public Sub Replacing()
StrVal = Userform1.Textbox1.Text
StrRep = Me.Textbox1.Text
if Trim(StrVal) = "" Then Exit Sub
Dim fstAddress As String
Dim nxtAddress As String
For Each ws In ThisWorkbook.Worksheets
With ws
Set Loc = .Cells.Find(what:=StrVal)
fstAddress = Loc.Address
If Not Loc Is Nothing Then
If Not StrRep = "" And i = 1 Then
Loc.Value = StrRep
Set Loc = .Cells.FindNext(Loc)
ElseIf i = 2 Then Set Loc = Range(ActiveCell.Address)
Set Loc = .Cells.FindNext(Loc)
nxtAddress = Loc.Address
If Loc.Address = fstAddress Then
ActiveSheet.Next.Activate '****Here it should jump only if found something on the next sheet****
GoTo repeat
nxtAddress = Loc.Address
End If
If Not Loc Is Nothing Then Application.Goto ws.Range(nxtAddress), False
End If
i = 0
End If
End With
Set Loc = Nothing
repeat:
Next ws
End Sub
the variable "i" which switches between the values 0, 1 and 2 is bound to two buttons. these buttons are "Replace" and "Skip (to next found value)".
This code asks on each occurrence of StrVal whether you want to replace the value or skip it.
I found a problem checking if Found_Address = First_Found_Address:
If you've replaced the value in in First_Found_Address it won't find that address again and miss the starting point in the loop.
Also the original source of the code stops at the last value using Loop While Not c Is Nothing And c.Address <> firstAddress. The problem here is that if the value in c is being changed eventually c will be Nothing but it will still try and check the address of c - causing an error (Range Find Method).
My solution to this is to build up a string of visited addresses on the sheet and checking if the current address has already been visited using INSTR.
I've included the code for calling from a button click or from within another procedure.
Private Sub CommandButton1_Click()
FindReplace Userform1.Textbox1.Text, 1
End Sub
Private Sub CommandButton2_Click()
FindReplace Userform1.Textbox1.Text, 1, Me.Textbox1.Text
End Sub
Sub Test()
FindReplace "cd", 1, "ab"
End Sub
Sub FindReplace(StrVal As String, i As Long, Optional StrRep As String = "")
Dim ws As Worksheet
Dim Loc As Range
Dim fstAddress As String
Dim bDecision As Variant
For Each ws In ThisWorkbook.Worksheets
'Reset the visited address list on each sheet.
fstAddress = ""
With ws
Set Loc = .Cells.Find(what:=StrVal, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
If Not Loc Is Nothing Then
Do
fstAddress = fstAddress & "|" & Loc.Address
Loc.Parent.Activate 'Activate the correct sheet.
Loc.Activate 'and then the cell on the sheet.
bDecision = MsgBox("Replace value?", vbYesNo + vbQuestion, "Replace or Select value?")
If bDecision = vbYes Then
Loc = StrRep 'Raise the blade, make the change.
'Re-arrange it 'til it's sane.
End If
Set Loc = .Cells.FindNext(Loc)
If Loc Is Nothing Then Exit Do
Loop While InStr(fstAddress, Loc.Address) = 0
End If
End With
Next ws
End Sub

Modifying shape colour based on cell value

Am looking to modify the shape colour based on a linked cell value ...
The shape is 'test' and the cell value "X11". I'm getting the error that the object does not support this property or method ...
Sub CChange()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
With ws.Shapes.Range(Array("test"))
If .Range("X11") = 1 Then
.Fill.ForeColor.RGB = RGB(18, 38, 43)
ElseIf .Range("X11") = 2 Then
.Fill.ForeColor.RGB = 0
End If
End With
End Sub
Change your code to this , your with statement is wrong.
You are not working with worksheet hence you cannot access range with .Range.
Sub CChange()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
With ws.Shapes.Range(Array("test"))
If ws.Range("X11") = 1 Then
.Fill.ForeColor.RGB = RGB(18, 38, 43)
ElseIf ws.Range("X11") = 2 Then
.Fill.ForeColor.RGB = 0
End If
End With
End Sub

Unable to update the value in combobox to sheet

I am new to VBA Coding.I have an userform which retrieves the value from excel sheet.There is a combobox which retrieves the value.But i want to change the combobox value & save it in excel.....
Image for Data in Excel
Dim temp As String
Dim findid As String
Dim lkrange As Range
Set lkrange = Sheet6.Range("A:D")
findid = TextBox1.Value
On Error Resume Next
temp = Application.WorksheetFunction.Vlookup(findid, lkrange, 1, 0)
If Err.Number <> 0 Then
MsgBox "ID not found"
Else
MsgBox "ID found"
Label5.Caption = Application.WorksheetFunction.Vlookup(findid, lkrange, 2, 0)
Label6.Caption = Application.WorksheetFunction.Vlookup(findid, lkrange, 3, 0)
ComboBox1.Value = Application.WorksheetFunction.Vlookup(findid, lkrange, 4, 0)
End If
End Sub
Private Sub CommandButton2_Click()
Dim fid As String
Dim rowc As Integer
Dim rowv As Integer
fid = TextBox1.Value
rowc = Application.WorksheetFunction.Match(fid, Range("A:A"), 0)
rowv = rowc - 1
Cells(rowv, 4).Values = marktable.ComboBox1.Value
End Sub
you could try the following
Option Explicit
Private Sub CommandButton1_Click()
Dim lkrange As Range
Dim rng As Range
Set lkrange = ThisWorkbook.Sheets("Sheet6").Range("A:A")
With Me
Set rng = MyMatch(.TextBox1.Value, lkrange)
If rng Is Nothing Then
MsgBox "ID not found"
Else
MsgBox "ID found"
.Label5.Caption = rng.Offset(0, 1)
.Label6.Caption = rng.Offset(0, 2)
.ComboBox1.Text = rng.Offset(0, 3)
End If
End With
End Sub
Private Sub CommandButton2_Click()
Dim lkrange As Range
Dim rng As Range
Set lkrange = ThisWorkbook.Sheets("Sheet6").Range("A:A")
With Me
Set rng = MyMatch(.TextBox1.Value, lkrange)
If Not rng Is Nothing Then rng.Offset(0, 3).Value = .ComboBox1.Text
End With
End Sub
Private Function MyMatch(val As Variant, rng As Range, Optional matchType As Variant) As Range
Dim row As Long
If IsMissing(matchType) Then matchType = 0
On Error Resume Next
row = Application.WorksheetFunction.Match(val, rng, matchType)
If Err = 0 Then Set MyMatch = rng.Parent.Cells(rng.Rows(row).row, rng.Column)
End Function
there were some errors:
Sheet6.Range("A:D") is not vaild
if you want to point to a sheet named "Sheet6" belonging to the Workbook where the macro resides, then you have to use ThisWorkbook.Sheets("Sheet6").Range("A:A")
Cells(...,...).Values =... is not valid
you must use Cells(...,...).Value =
but I think the following suggestions are more important:
Always use Option Explicit statement at the very beginning of every module
this will force you to explicitly declare each and every variable, but then it'll save you lots of time in debugging process
avoid/limit the use of On Error Resume Next statement
and, when used, make sure to have it followed as soon as possible by the "On Error GoTo 0" one. that way you have constant control on whether an error occurs and where
I confined it in a "wrapper" function (MyMatch()) only.
Always specify "full" references when pointing to a range
I mean, Cells(..,..) implictly points to the active sheet cells, which may not always be the one you'd want to point to.

FormatCondition error with Excel 2007

The following code works splendidly on Excel 2013. But on Excel 2007 I get problems.
Function IsMasked() As Boolean
Dim wksTables As Worksheet
Set wksTables = GetSheetByCodename(ThisWorkbook, "wTables")
Dim loMask As ListObject
Set loMask = wksTables.ListObjects("tMask")
Dim lor As ListRow
For Each lor In loMask.ListRows
Dim sWorksheetName As String
Dim sRangeAddress As String
Dim sCompleteAddress As String
sWorksheetName = Intersect(lor.Range, loMask.ListColumns("Worksheet").DataBodyRange)
sRangeAddress = Intersect(lor.Range, loMask.ListColumns("Range").DataBodyRange)
Dim R As Range
Set R = ThisWorkbook.Worksheets(sWorksheetName).Range(sRangeAddress)
With R
Dim bMasked As Boolean
Dim fc As Object
For Each fc In .FormatConditions
If TypeName(fc) = "FormatCondition" Then
If fc.Interior.Color = RGB(0, 0, 0) And fc.Font.Color = RGB(0, 0, 0) Then
'Range has mask if any formatcondition is mask
bMasked = True
Exit For
End If
End If
Next fc
If bMasked = False Then
'Mask is false if any specified range does not have mask
Exit For
End If
End With
Next lor
IsMasked = bMasked
End Function
If fc.Interior.Color = RGB(0, 0, 0) And fc.Font.Color = RGB(0, 0, 0) evaluates to true even when the interior color is set to nothing and when the font color is red.
When I look at fc in the locals window a lot of the properties say application-defined or object-defined error. I don't know if this is any clue.

Ignore items in a formatconditions collection that are not formatcondition?

The following code works for most sheets in my workbook:
Function IsHighlighted() As Boolean
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
Dim rUsedRange As Range
Set rUsedRange = wks.UsedRange
With rUsedRange
Dim bHighlighted As Boolean
Dim fc As Object
For Each fc In .FormatConditions
If fc.Interior.Color = RGB(255, 0, 0) And fc.Font.Color = RGB(255, 255, 0) Then
bHighlighted = True
Exit For
End If
Next fc
If bHighlighted = True Then
Exit For
End If
End With
Debug.Print (wks.Name & "," & rUsedRange.FormatConditions.count)
Next wks
IsHighlighted = bHighlighted
End Function
But some of my sheets contain icon sets.
Apparently:
The FormatConditions collection includes FormatCondition, DataBar, AboveAverage, ColorScale, UniqueValues, Top10 and IconSetCondition objects, not just FormatCondition objects.
For more context, see: For each fc in range.formatconditions fails. Why?
Some of these don't have .Interior.Color so the if statement throws an error.
What is a nice way I can skip all of these objects in the formatconditions collection that are not of the type formatcondition?
I tried using VarType(fc) to determine the obejct type, but it only retuns a 9 which says that fc is some kind of object.
Did you try the Typename function,
for example:
Dim str as string.
Msgbox Typename(str) ' ->>> "String"