font color vba question

pong

New Member
Joined
Jun 18, 2011
Messages
25
I am trying to build a sub to check if a workbook is opened by calling a function
and tell if the workbook on the next cell
but if the user doesnt enter a bookname, "Please enter a bookname"t would just show on the next cell
The thing is i wanna change color of "please enter a bookname" into red and turn the message into bold
so what should i do in the code ?

Code:
Sub ChecktheworkbookOpen()
Dim IsOpen As Boolean
Dim BookName As String
Dim i As Integer
For i = 1 To 10
If Cells(i, 2) = "" Then
Cells(i, 2).Offset(, 1) = "Please enter a bookname"

Else
BookName = Cells(i, 2).Value
IsOpen = Check_workbook_open(BookName)

If IsOpen = True Then
Cells(i, 2).Offset(, 1) = "Opened"
Else
Cells(i, 2).Offset(, 1) = "Not yet opened"
End If
End If
Next
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try

Code:
With Cells(i, 2).Offset(, 1)
    .Value = "Please enter a bookname"
    With .Font
        .Color = RGB(255, 0, 0)
        .Bold = True
    End With
End With
 
Upvote 0
I substituted Cells(i, 2).Offset(, 1) = "Please enter a bookname" with your code
it changed the color of the "Please enter a bookname" into red
however, it also changed the color "opened " and "not yet opened " message
is there anyway that i can change only the 'please enter a bookname' color
without affecting other 2 ?
 
Upvote 0
Please post the actual code that you used. As I intended the code to be used it should not do that.
 
Upvote 0
This is the original code that i was using
and the color was not changed
Code:
Function Check_workbook_open(ByVal Bk As String) As Boolean
Dim T As Excel.Workbook
Err.Clear
On Error Resume Next
Set T = Application.Workbooks(Bk)
Check_workbook_open = Not T Is Nothing
Err.Clear
On Error GoTo 0

End Function

Sub ChecktheworkbookOpen()
Dim IsOpen As Boolean
Dim BookName As String
Dim i As Integer
For i = 1 To 10
If Cells(i, 2) = "" Then
Cells(i, 2).Offset(, 1).Value = "Please enter a bookname"

Else
BookName = Cells(i, 2).Value
IsOpen = Check_workbook_open(BookName)

If IsOpen = True Then
Cells(i, 2).Offset(, 1) = "Opened"
Else
Cells(i, 2).Offset(, 1) = "Not yet opened"
End If
End If
Next

End Sub

Then , i substituted the coloring code
it worked but it also changed the color of the "opened " and "not yet opened "
Code:
Sub ChecktheworkbookOpen()
Dim IsOpen As Boolean
Dim BookName As String
Dim i As Integer
For i = 1 To 10
If Cells(i, 2) = "" Then
With Cells(i, 2).Offset(, 1)
    .Value = "Please enter a bookname"
    With .Font
        .Color = RGB(255, 0, 0)
        .Bold = True
    End With
End With

Else
BookName = Cells(i, 2).Value
IsOpen = Check_workbook_open(BookName)

If IsOpen = True Then
Cells(i, 2).Offset(, 1) = "Opened"
Else
Cells(i, 2).Offset(, 1) = "Not yet opened"
End If
End If
Next


Anyway thank you very much for your help
 
Upvote 0
try this

Code:
Sub ChecktheworkbookOpen()
Dim IsOpen As Boolean
Dim BookName As String
Dim i As Integer
For i = 1 To 10
    If Cells(i, 2) = "" Then
        Cells(i, 2).Offset(, 1).Value = "Please enter a bookname"
    Else
        BookName = Cells(i, 2).Value
        IsOpen = Check_workbook_open(BookName)
        If IsOpen = True Then
            Cells(i, 2).Offset(, 1) = "Opened"
        Else
            Cells(i, 2).Offset(, 1) = "Not yet opened"
        End If
    End If
    If Cells(i, 2).Value = "Please enter a bookname" Then
        Cells(i, 2).Color = RGB(255, 0, 0)
        Cells(i, 2).Bold = True
    End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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