SunQuest
           Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old July 16th, 2003, 03:53 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 6
set font style of a cell in excel

Hi all,

I' ve tried to set the font style of cell in excell to bold but everytime an error occurs.
Code:
  For Each aCell In Selection.Cells
    If aCell.Value = "bla" Then
      Range(aCell).Select
      Selection.FontStyle = "Font"
    End If
  Next


The error message is:
Code:
Run-time error '1004':

Method 'Range' of Object '_Global failed'

I appreciate every help.

Reply With Quote
  #2  
Old August 4th, 2003, 05:40 AM
Silian's Avatar
Silian Silian is offline
Gogga
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 198 Silian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
You have propably figured this out by now, but if you havn't:
remember that aCell in your procedure is already a range, meaning you can't say range(aCell), just say aCell

It isn't necessary to select a cell to change it's font.
Code:
Dim aCell
For Each aCell In Selection.Cells
    
    If aCell.Value = "bla" Then
      aCell.Font.Bold = True
    End If
  Next


But anyway, I didn't know that you can loop through a selection the way you did, so thanks for the post

Reply With Quote
  #3  
Old August 5th, 2003, 06:24 AM
james_skeggs james_skeggs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 11 james_skeggs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Your code is great, but how do you get it to apply to a different cell eg, if aCell refers to column A how do you get it to change the font of column C?

Reply With Quote
  #4  
Old August 5th, 2003, 08:28 AM
Silian's Avatar
Silian Silian is offline
Gogga
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 198 Silian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
aCell in the previous example refers to all the cells in the range that someone has selected. (And it only changes the fond for the cells that have a specific tests (in this example "bla") If you want it 2 change everything, take out the if statement)

If you want it 2 change the font 4 everything in a specific column, you can either select the column, e.g.
Code:
    Columns("C:C").Select
    Dim aCell
    For Each aCell In Selection.Cells
    
    If aCell.Value = "bla" Then
      aCell.Font.Bold = True
    End If
    Next

or just loop through the column, instead of the selection, e.g.
Code:
    Dim aCell
    For Each aCell In Columns("C:C").Cells
    
    If aCell.Value = "bla" Then
      aCell.Font.Bold = True
    End If
    Next

Or you can do a specific number of rows in the column, e.g.
Code:
   for i = 1 to 8
     cells(i, 3).font.bold = true
   next i

This changes the font of the first 8 rows in column 3 (C)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > set font style of a cell in excel


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway