Macro won't run with Option Explicit

Andrew B

New Member
Joined
Jan 21, 2011
Messages
27
I have a macro that changes the font color and font size of a cell. It is located in a module that contains several other macros. If I declare Option Explicit in that module, the macro will not run. I get an "compile error" message stating that the variable "font_size" is is not defined. If I remove Option Explicit, the macro runs, but my limited experience with VBA macros tells me that this isn't right, that I should fix the code so that it will run with Option Explicit declared. The problem is, I don't know how to fix it. Can anyone help me. Here is the code:
Code:
Sub BlackToRed()
Dim font_colour As Integer
Dim B As Variant
font_colour = 1
font_size = 10

ActiveSheet.Unprotect Password:="myPWD"
ActiveCell.Font.ColorIndex = font_colour
ActiveCell.Font.Size = font_size

If font_colour = 1 Then
font_colour = 3
End If

If font_size = 10 Then
font_size = 11
End If


Selection.Font.ColorIndex = font_colour
ActiveCell.Font.Size = font_size
ActiveSheet.Protect Password:="myPWD"
End Sub
I'm stumped!
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
Try

Rich (BB code):
Sub BlackToRed()
Dim font_colour As Integer, font_size As Integer
font_colour = 1
font_size = 10

ActiveSheet.Unprotect Password:="myPWD"
ActiveCell.Font.ColorIndex = font_colour
ActiveCell.Font.Size = font_size

If font_colour = 1 Then
font_colour = 3
End If

If font_size = 10 Then
font_size = 11
End If


Selection.Font.ColorIndex = font_colour
ActiveCell.Font.Size = font_size
ActiveSheet.Protect Password:="myPWD"
End Sub
 

Forum statistics

Threads
1,082,323
Messages
5,364,586
Members
400,808
Latest member
formulasataglance

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top