Solved

VB Macro [Excel]

Posted on 2004-04-07
11
272 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
11 Comments
 
LVL 50

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Accepted Solution

by:
p_sie earned 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
message box in access 4 46
VB6 - Convert HH:MM into Decimal 8 54
clicking a shape in a frame array vb6 3 45
Macro Excel - Multiple If conditions 2 71
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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

786 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