?
Solved

VB Macro [Excel]

Posted on 2004-04-07
11
Medium Priority
?
278 Views
Last Modified: 2010-05-02
Does anyone know a small example of a script for macro'ing (VB Macro Script in Excel) that can just make every other cell bold.

A1 = Bold.False
A2 = Bold.True
A3 = Bold.False
A4 = Bold.True

Or something to that effect?

thanks
0
Comment
Question by:Narusegawa
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 10774818
Something like:

Sheet1.Range("A1").Font.Bold = True
    Sheet1.Range("A3").Font.Bold = True

?
0
 
LVL 9

Expert Comment

by:p_sie
ID: 10774828
Something like:

Dim i as integer
Dim xlobj as Excel.Application

for i = 1 to NumberOfRows   <--- fill this in yourself
    If int(i/2) <> i/2 Then
         'Odd number, do nothing
    Else
         xlobj.Range("A" & i).Select
         xlobj.Selection.Font.Bold = True
    End If
Next

0
 

Author Comment

by:Narusegawa
ID: 10774871
p_sie

That pretty much works. Would be nice to get the number of Rows Dynamically.

Dim i As Integer
Sub Macro1()
For i = 1 To 456
    If Int(i / 2) <> i / 2 Then
         Range("A" & i).Select
         Selection.Font.Bold = True
    Else
         Range("A" & i).Select
         Selection.Font.Size = 8
    End If
Next
End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Accepted Solution

by:
p_sie earned 200 total points
ID: 10775146
You could first count them by doing

Dim j as Long

For j = 1 to 65000
    If Range("A" & i).value = "" then exit for
next

For i = 1 To j
    If Int(i / 2) <> i / 2 Then
         Range("A" & i).Select
         Selection.Font.Bold = True
    Else
         Range("A" & i).Select
         Selection.Font.Size = 8
    End If
Next
0
 
LVL 1

Expert Comment

by:doubleglazing2
ID: 10788189
TRY EITHER:---

For c = 1 To 65536
If Range("A" & c).Row Mod 2 = 1 Then
    Range("A" & c).Font.Bold = True
Else
    Range("A" & c).Font.Size = 8
End If
If Range("A" & c).Value <> "" Then Exit For   '>Exits if an empty cell is reached going down
Next c

OR :---

For c = 1 To 65536
If Range("A" & c).Row Mod 2 = 1 Then
    Range("A" & c).Font.Bold = True
Else
    Range("A" & c).Font.Size = 8
End If
If Range("A65536").End(xlUp).Row = c Then Exit For   '>Exits at last data entry in column

Next c
0
 
LVL 1

Expert Comment

by:doubleglazing2
ID: 10788230
If you just wanted the odd numbered rows to be bold, try:

For c = 1 To 65535 Step 2
    Range("A" & c).Font.Bold = True
    If Range("A65536").End(xlUp).Row >= c Then Exit For
Next c
0
 
LVL 1

Expert Comment

by:doubleglazing2
ID: 10791531
BTW, my code is more efficient because it doesn't select the range it is manipulating. Selecting the range wastes time, increases inefficiency, is more prone to errors e.g. someone might click the screen, and at times stalls the program if virtual memory is low.

The last post I added above is also improved (in the case of just boldening the text at every odd numbered row) because not only doesn't select the ranges it is manipulating, but it also halfs the calculations and formatting time required and therefore doubles the efficiency. It does this by simply completely ignoring even numbers (the counter doesn't even take-on an even value!).

This might see a little trite, but if you're program is large, or there are other things going on in your computer, it makes a hell of a lot of difference!

(:D)
0
 
LVL 9

Expert Comment

by:p_sie
ID: 11257242
Asker was happy with one of my first answers.
0
 

Author Comment

by:Narusegawa
ID: 11257664
Doh, thought I'd accepted that one already. Sorry
0
 
LVL 9

Expert Comment

by:p_sie
ID: 11257891
Doesn't matter!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question