Setting font properties using function

I want to be able to set the properties of a cell font using a function.
I have tried the below but it errors in the function with

Error Number: 91
Error Description: Object variable or With block variable not set

Anyone not on holiday out there who can help me please!

happy new year

Rod


------------------------
'Calling code

xlSht.Cells(1, 1).Font = NameFormatType1(255)


Private Function NameFormatType1(NmCol As Long) As Excel.Font

    NameFormatType1.Color = RGB(NmCol, 0, 0)
    NameFormatType1.Bold = True

End Function 


0
Rod
12/31/2009 12:01:51 PM
excel.programming 6508 articles. 2 followers. Follow

6 Replies
883 Views

Similar Articles

[PageSpeed] 52

Try these changes


Sub test()
Set xlSht = ActiveSheet
Call NameFormatType1(xlSht.Cells(1, 1), 255)

End Sub


Private Sub NameFormatType1(ByRef cell As Range, _
ByVal NmCol As Long)

With cell.Font
.Color = RGB(0, 0, NmCol)
.Bold = True
End With

End Sub


-- 
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=166121

[url="http://www.thecodecage.com"]Microsoft Office Help[/url]

0
joel
12/31/2009 12:24:25 PM
OK, that works fine. Thanks

Out of curiosity can my way be made to work.
It compiled OK so I was hopeful.



"joel" <joel.44267x@thecodecage.com> wrote in message 
news:joel.44267x@thecodecage.com...
>
> Try these changes
>
>
> Sub test()
> Set xlSht = ActiveSheet
> Call NameFormatType1(xlSht.Cells(1, 1), 255)
>
> End Sub
>
>
> Private Sub NameFormatType1(ByRef cell As Range, _
> ByVal NmCol As Long)
>
> With cell.Font
> Color = RGB(0, 0, NmCol)
> Bold = True
> End With
>
> End Sub
>
>
> -- 
> joel
> ------------------------------------------------------------------------
> joel's Profile: 229
> View this thread: 
> http://www.thecodecage.com/forumz/showthread.php?t=166121
>
> [url=&quot;http://www.thecodecage.com&quot;]Microsoft Office Help[/url]
>
> 


0
Rod
12/31/2009 4:09:56 PM
> 'Calling code
>
> xlSht.Cells(1, 1).Font = NameFormatType1(255)
>
> Private Function NameFormatType1(NmCol As Long) As Excel.Font
>    NameFormatType1.Color = RGB(NmCol, 0, 0)
>    NameFormatType1.Bold = True
> End Function

There are two problems with your originally posted code... one easily 
solved, the second not. First, in your calling line of code, Font is an 
object, so you must use the Set keyword to assign your function's returned 
object to it. So that calling line should have looked like this...

Set xlSht.Cells(1, 1).Font = NameFormatType1(255)

Doing that, however, reveals the second problem... your function is declared 
as an object, but it has no reference. That is, it's a font object, but a 
font object to what? Obviously, you want it to reference A1 on xlSht, but 
how is it to know that? Before you can change properties of NameFormatType1, 
you first have to Set it to some object. In this case, you would need the 
equivalent of this...

Set NameFormatType1 = xlSht.Cells(1, 1)

so it would know what object is having its properties modified. Of course, 
having to do this would interfere with the approach you are trying to use.

-- 
Rick (MVP - Excel)


"Rod" <rodrodrodrod@hotmail.com> wrote in message 
news:OTO2xOjiKHA.2188@TK2MSFTNGP04.phx.gbl...
> OK, that works fine. Thanks
>
> Out of curiosity can my way be made to work.
> It compiled OK so I was hopeful.
>
>
>
> "joel" <joel.44267x@thecodecage.com> wrote in message 
> news:joel.44267x@thecodecage.com...
>>
>> Try these changes
>>
>>
>> Sub test()
>> Set xlSht = ActiveSheet
>> Call NameFormatType1(xlSht.Cells(1, 1), 255)
>>
>> End Sub
>>
>>
>> Private Sub NameFormatType1(ByRef cell As Range, _
>> ByVal NmCol As Long)
>>
>> With cell.Font
>> Color = RGB(0, 0, NmCol)
>> Bold = True
>> End With
>>
>> End Sub
>>
>>
>> -- 
>> joel
>> ------------------------------------------------------------------------
>> joel's Profile: 229
>> View this thread: 
>> http://www.thecodecage.com/forumz/showthread.php?t=166121
>>
>> [url=&quot;http://www.thecodecage.com&quot;]Microsoft Office Help[/url]
>>
>>
>
> 

0
Rick
12/31/2009 4:44:17 PM
Thanks
I understand the error message now.


"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message 
news:%232Fh6hjiKHA.5020@TK2MSFTNGP02.phx.gbl...
>> 'Calling code
>>
>> xlSht.Cells(1, 1).Font = NameFormatType1(255)
>>
>> Private Function NameFormatType1(NmCol As Long) As Excel.Font
>>    NameFormatType1.Color = RGB(NmCol, 0, 0)
>>    NameFormatType1.Bold = True
>> End Function
>
> There are two problems with your originally posted code... one easily 
> solved, the second not. First, in your calling line of code, Font is an 
> object, so you must use the Set keyword to assign your function's returned 
> object to it. So that calling line should have looked like this...
>
> Set xlSht.Cells(1, 1).Font = NameFormatType1(255)
>
> Doing that, however, reveals the second problem... your function is 
> declared as an object, but it has no reference. That is, it's a font 
> object, but a font object to what? Obviously, you want it to reference A1 
> on xlSht, but how is it to know that? Before you can change properties of 
> NameFormatType1, you first have to Set it to some object. In this case, 
> you would need the equivalent of this...
>
> Set NameFormatType1 = xlSht.Cells(1, 1)
>
> so it would know what object is having its properties modified. Of course, 
> having to do this would interfere with the approach you are trying to use.
>
> -- 
> Rick (MVP - Excel)
>
>
> "Rod" <rodrodrodrod@hotmail.com> wrote in message 
> news:OTO2xOjiKHA.2188@TK2MSFTNGP04.phx.gbl...
>> OK, that works fine. Thanks
>>
>> Out of curiosity can my way be made to work.
>> It compiled OK so I was hopeful.
>>
>>
>>
>> "joel" <joel.44267x@thecodecage.com> wrote in message 
>> news:joel.44267x@thecodecage.com...
>>>
>>> Try these changes
>>>
>>>
>>> Sub test()
>>> Set xlSht = ActiveSheet
>>> Call NameFormatType1(xlSht.Cells(1, 1), 255)
>>>
>>> End Sub
>>>
>>>
>>> Private Sub NameFormatType1(ByRef cell As Range, _
>>> ByVal NmCol As Long)
>>>
>>> With cell.Font
>>> Color = RGB(0, 0, NmCol)
>>> Bold = True
>>> End With
>>>
>>> End Sub
>>>
>>>
>>> -- 
>>> joel
>>> ------------------------------------------------------------------------
>>> joel's Profile: 229
>>> View this thread: 
>>> http://www.thecodecage.com/forumz/showthread.php?t=166121
>>>
>>> [url=&quot;http://www.thecodecage.com&quot;]Microsoft Office Help[/url]
>>>
>>>
>>
>>
>
> 


0
Rod
12/31/2009 6:03:41 PM
Here is another method closer to what you did.


Type MyFont
Color As Long
Bold As Boolean
End Type
Sub test()
Dim Mycell As MyFont

Set xlSht = ActiveSheet
Mycell = NameFormatType1(255)
xlSht.Cells(1, 1).Font.Color = Mycell.Color
xlSht.Cells(1, 1).Font.bold = Mycell.bold

End Sub

Private Function NameFormatType1(NmCol As Long) As MyFont

NameFormatType1.Color = RGB(NmCol, 0, 0)
NameFormatType1.bold = True

End Function


-- 
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=166121

[url=&quot;http://www.thecodecage.com&quot;]Microsoft Office Help[/url]

0
joel
12/31/2009 6:35:31 PM
OK thanks, I see now.

"joel" <joel.442nen@thecodecage.com> wrote in message 
news:joel.442nen@thecodecage.com...
>
> Here is another method closer to what you did.
>
>
> Type MyFont
> Color As Long
> Bold As Boolean
> End Type
> Sub test()
> Dim Mycell As MyFont
>
> Set xlSht = ActiveSheet
> Mycell = NameFormatType1(255)
> xlSht.Cells(1, 1).Font.Color = Mycell.Color
> xlSht.Cells(1, 1).Font.bold = Mycell.bold
>
> End Sub
>
> Private Function NameFormatType1(NmCol As Long) As MyFont
>
> NameFormatType1.Color = RGB(NmCol, 0, 0)
> NameFormatType1.bold = True
>
> End Function
>
>
> -- 
> joel
> ------------------------------------------------------------------------
> joel's Profile: 229
> View this thread: 
> http://www.thecodecage.com/forumz/showthread.php?t=166121
>
> [url=&quot;http://www.thecodecage.com&quot;]Microsoft Office Help[/url]
>
> 


0
Rod
1/2/2010 8:46:27 AM
Reply:

Similar Artilces:

setting up new outlook mail account
i can not set up a new account on my outlook, i keep saying establish network connection with a correct sign but searching for my mail account shows X can you please advise Are you trying to setup a POP3 mail account or an Exchange account? Also, what version of Outlook are you using? "rashid" <rashid@discussions.microsoft.com> wrote in message news:C59D04C0-C1C9-4BD1-AE00-2009D25B221D@microsoft.com... > i can not set up a new account on my outlook, i keep saying establish > network > connection with a correct sign but searching for my mail account shows X > ...

How do I set up an argument which asks for multiple criteria
I am trying to use an argument that asks for one of three criteria (2345P, 8319T, or 7026J) in cell C108, and if it finds it, enters the amount of yet another cell, G108 into cell K108, and if not enters 0 into cell K108. It seems there are too many arguments for the "if" argument, and I can't seem to get the "lookup" argument to work either. Is there another argument I should be working with? Thanks -- Diver Try: =IF(OR(C108="8319T",C108="2345P",C108="7026J"),G108,0) "bj" wrote: > try in K108 > =if(or(c108=&qu...

OWA Set Rule problem
I have successfully install the Exchange 2003 with SP1 in the Windows Server 2003. Anything alright exclude the rules function in OWA. I try to set the rules from any then "move to" or "copy to" specify folder are no problem, but "forward to" other email address, its don't work. I try to input the email address in the contact and then select from the contact, but the result also don't work. The rules can save and I can receive the incoming email in the Exchange 2003/Outlook or OWA, but it don't forward to my specify email address. I try to...

How do I set the 'Set Synchronizing Client' field
We have a number of outlook clients using CRM that have the 'Set Synchronizing Client' select box greyed out. We need to tick them so that they are able to synchronize tasks etc. Can any one help me find out why these are greyed out and then set them so that they can synchronize thanks In my experience, that checkbox is always greyed out for the first install that is performed for a user. The only time that checkbox becomes active is if a second copy of CRM is installed for a user who already has CRM installed on another PC.... Normally the box is checked and grey. If that is...

email fonts #2
Dear all Why do some of the emails I receive come over in Courier? And when I reply I can't change the font. I am on Outlook 2007. EGB Are you receiving then sending email in plain text mode ? kev "egb" <egb@nospam.co.uk> wrote in message news:OqK1qILtHHA.1204@TK2MSFTNGP03.phx.gbl... > Dear all > > Why do some of the emails I receive come over in Courier? And when I > reply I can't change the font. I am on Outlook 2007. > > EGB > > > > > ...

Remove unwanted fonts from drop down menu
I have a number of apparently Korean fonts such as @Batang, @Dotum, @Fang Song, etc. I want them removed. When I go to the Control Panel and the Font Icon, they are not listed. How do I remove them? ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane. http://www.micros...

How to set backcolour
Hello All I have a continuous form containing a number field (long integer) called [v_colour]. This field contains the number of a certain colour - as per the numbers that are used in the format properties for back colour etc. I would like to set the back colour for the form's [v_colour] text box to be the value of the field: e.g. If the value of [v_colour] in the first record is 255, I want the back colour of the textbox for that record to be bright red. I tried this as the OnLoad event for the form: [v_colour].backcolour = [v_colour].value .... but this resulted in the back colour of...

Applying Percent Decrease Across Set Periods
I have a column which includes dates when items were purchased. I have used the DATEDIF function to find the number of years between that date and today's date. I now want to decrease the purchase amount 10% for every year calculated via the DATEDIF function. Any ideas, is this possible, why can't I think...? -- Hunt&amp;Peck ------------------------------------------------------------------------ Hunt&amp;Peck's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=32961 View this thread: http://www.excelforum.com/showthread.php?threadid=527873 ...

using User properties in Inbox Rules
Enviroment: Exchange 2000 SP3 Outlook 2002 SP1 I like to setup a inbox rule based on the User property "department". The problem is, the user properties from the mails in the Inbox are always empty (in my active directory the user property "department" is filled with the correct departmentname. I think in Exchange 5.5 this functionality had worked. Any ideas to solve the requirement? ...

Set Regional Settings but not Language
Hi I have a multi-language APP en-US, es-ES, it-IT I usually change Languages and Locales by using the CultureInfo to switch Languages System.Globalization.CultureInfo myCI = new System.Globalization.CultureInfo("es-ES"); My issue is - what if the user wants to View App in english and use the Regional Options on a PC in say Spain ??? If I change the CultureInfo to en-US then they can view APP in English ( English satelite assemblies) but date and numbers will be in US format If I change the CultureInfo to es-ES then date and numbers will be in Spain forma...

Reading Pane Settings
I did a search for an answer to this question and have not found one. If I missed an answer already posted, please direct me to the right place. I have Outlook 2007 and set up the "right" reading pane for my e-mail. Now when I go to my calendar it is set up the same way - calendar on the left (same size as the inbox) and then there is a blank space to the right that is the size of the reading pane/preview. To the right is the to-do bar (which I have no problem with). I don't know why there is a reading/preview pane on the calendar when there is nothing to preview, and I...

Work Hours setting excludes specified hours from Scheduling
I am miffed by this problem. I specify a work hour schedule for a user. When I schedule a CRM User as a Resource for a Service in Scheduling of a Service Activity with Requested Time of ASAP and click Find Available Times, the values for Avilable Times returned are outside of the Work Hours I specified for the user and not within the work hours. When I drill into the Work Hours for this User, the Calendar view shows Cross Day rather than 8:00AM - 5:00PM I speciied for work hours and editing the hours reveal the correct hours in the Work Hours. What gives? Never mind. Figured out w...

Outlook "Arrange By" option does not retain its setting
In outlook 2007, I use a custom view for my inbox, and I have the Group By option checked to"Automatically group according to arrangement." This has been workn flawlessly until today, when I realized Outlook did not like a particular mouse click; perhaps I clicked on the border of two options or some other anomoly that caused the window toflash once. The end result was that my Arrange By option changed to "From" when I had previously set it to "Conversation." Now, whenever I hange it backto "Conversation," leave the inbox and come back, ...

How do I use Excel or Access or a combination of both to track an.
I would like to come up with a posting journal using either Excel or Access or a combination of both to track the dues of a non-profit organization. In using this I want to keep track of outstanding balance. I want to be able to use Word to prepare an invoice that I can either mail to the address or e-mail. I would like for the balance adjusted as each payment is made. Once all dues are paid then show it as paid in full. Thanks, Are you familiar with VBA - the office macro language? You can use this to automate word, excel, outlook, access, etc to talk to one another. Your question...

interactive buttons, multiple sets
hi folks I'm sure one of you good folks will save my bacon as usual I have a series of websites, all of which exhibit the same odd behavior with interactive buttons. I have a bunch of buttons in the left margin, each of which pulls up ( lets call it ) a chapter, and in each 'chapter' are a number of related pages. On the top of each page is an imbedded page which has 'page numbers' for all the 'pages' in that chapter. So, it's a grid where the 'vertical' buttons in the left margin and the 'horizontal' buttons at the top comb...

How do I set OOR
I have searched the help menu and have had no luck finding out how to set my outlook 200 to send an OOR (Out of Office Reply) Any assistance would be great If you are in an Exchange environment (ask your administrator if you do notknow) you can set the Out of the Office Assistant which is located under Tools. If you are not in an Exchange environment you can create a rule that replies on all e-mails. The downside of this is that you'll have to have your Outlook open all the time for the rules to process. Make sure you set some exceptions (for instance based on the subject field so ...

Set Focus
I have text box that I input data into and I was wondering if it is possible to set focus to the text box and avoid clicking on the box. I thought the term is Set Focus, but can't find any information or example that would help in doing so. Any help would be appreciated .. PPT 2003. Look at the following page for code to set focus on a text box: http://officeone.mvps.org/vba/activate_shape.html - Chirag PowerShow - View multiple PowerPoint slide shows simultaneously http://officeone.mvps.org/powershow/powershow.html "Phil K" <PhilK@discussions.microsof...

Missing Publisher fonts
I have many fonts in my control panel font folder that are not in my Publisher font list - how do access them from Publisher. These are all TT fonts and many from the same family eg. arial narrow will show in Publisher but not arial narrow bold. I am using Publisher 2003 (11.8103.8132) sp2. Thanks -- President Tom If you are creating a Web Page in Publisher there are only 11 fonts available. To get the full range of your fonts, Format, fonts, clear *Show only Web Fonts*. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "Pres...

Set published amount of calendar information
Hey guys! Is there any way I can have Exchange 2000/2003 force the amount of time published in the outlook calendar? I know that you can go into the the calendar options and set the amount you want published, but is there a way to have exchange require the amount that is published so that I could get everyone to publish a year's worth of calendar data instead of one person being set at 2 months and another at 6 months and another at 12? Thanks! Mike On Thu, 29 Jun 2006 06:18:02 -0700, Mike <Mike@discussions.microsoft.com> wrote: >Hey guys! > >Is there any way I c...

MS Windows Explorer programming using VC++ ???
Hi I am writing a simple application to set up custom button control in MS windows explorer... Its like I shall be clicking on the button control and it shall be activating my custom program on the files/directories currently under selection... Having searched through a lot of MSDN stuff,, i could not find a way good enought to support me in my programming... Kindly suggest what needs to be done.... I am building this application in Microsoft Visual C++ using MFC... Also my platform is X86 with Windows 2000 Thanks in advance.. Regards, a.a.cpp I suggest reading the The Comple...

Publisher 2003 Need to set business card to Avery 5371 setting
I need to set the print settings so the business cardd that I created will print on Avery 5371 business cards Okay.........and your question is? -- JoAnn Paules MVP Microsoft [Publisher] "Jonathan" <Jonathan @discussions.microsoft.com> wrote in message news:187615DD-9EF1-42A2-8436-78E6E6C6ACE4@microsoft.com... >I need to set the print settings so the business cardd that I created will > print on Avery 5371 business cards Page setup, business cards, click change copies per sheet, type > .5 top, .75 side, zero gaps. Avery 5371 is a standard business card s...

Setting the Settings
Really quickly: How (exactly) does one get Publisher to remember the page settings I want. ie. every time I open a new document, I get centimeters, and usually, I'd prefer inches. I have not been able to find a "Save Settings" option anywhere. Can anyone help? thomas In Publisher 2000 it's Tools Options General tab Measurements unit box - select inches. OK That should do it. -- Don -------- Vancouver, USA - One of the great cities in one of the 45+ countries in America! "ThomasL" <tlammert@netcom.ca> wrote in message ne...

Print Preview using CPreviewDC
Hi there, I have a problem trying to implement a print preview function. Yet the printing itself is no problem. I have this chart control with a method m_graph.PrintGraph(CDC *pDC) which outputs the data to the CDC object. Now I want to do Print Preview similarly using the CPreviewDC class mentioned in the MSDN Documentation instead of the CDC class. How do I setup the CPreviewDC object without a print dialog? And after I have printed the content into the CDC class how do I display the print preview? ...

Delete records using listbox
Hi, Does anyone know a way of deleting selected records in a multi-selection list box? Thanks On Mon, 17 Dec 2007 08:19:00 -0800, s4 <s4@discussions.microsoft.com> wrote: There is no built-in way to do this, so you would have to write the code yourself. Iterate over the SelectedItems collection, and call a Delete query for each record. Alternatively you can replace the listbox by a subform, which supports automatic deletion. -Tom. >Hi, >Does anyone know a way of deleting selected records in a multi-selection >list box? >Thanks Assuming that the bound column of your l...

"Object or class does not support the set of events" Quick Question
All, I built a MS Access database in MS Access 2003, which I have set up in a shared folder. I have a form with some standard components such as text boxs, bottons. When a user updates the form I wish to record the time/date and username in the record. I have posted a copy of the code I'm using for the "BeforeUpdate" event below. Now this works fine on my machine and updates without a problem. However when a user on another machine (also using MS Access 2003) tries to update the same field, he gets the following error: "The expression Before Update you entered as the eve...