?
Solved

VB Macro [Excel]

Posted on 2004-04-07
11
Medium Priority
?
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 53

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 Month14 days, 12 hours left to enroll

770 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