Solved

VB in Excel

Posted on 2000-03-04
8
157 Views
Last Modified: 2010-05-02
I'm using Visual Basic in Excel and I have to color every other line in an Excel Worksheet by using Visual Basic.  What line(s) of code to I use to do this?
0
Comment
Question by:Artemis_23
[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
8 Comments
 

Expert Comment

by:soosairaj
ID: 2584253
Dear Friend ,  this will help you to make differentcolor for each line in excel through VB.

Dim App1 As Excel.Application

Set App1=CreateObject"Excel.Application")
        App1.Visible = True
        App1.Workbooks.Add


app1.Rows("2:2").Select 'for second line
   
    With Selection.Interior
        .ColorIndex = 6  'Yellow color
        .Pattern = xlSolid
    End With

app1.Rows("3:3").Select 'for third line
    With Selection.Interior
        .ColorIndex = 53  ' Brown color
        .Pattern = xlSolid
    End With

Thanks

Raj
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 2584668
HI Artemis 23,

From your question I understand you're running VBA in Excel. In that case, there is no need to set/call an Object.

Following little sub wil color all your lines with the color red:

Sub ColorLines
Dim Startline As Integer, Endline As Integer
Startline = 1
'To set the last line to line 200
EndLine=200
'To set the last line to be coloured
'To the last used line onyour sheet, use
'the comment on the next line (without the ' of core)
'Endline = Range("A1").SpecialCells(xlCellTypeLastCell).Row
With Rows(Startline & ":" & Endline).Interior
        .ColorIndex = 3
        .Pattern = xlSolid
    End With
End Sub

Good Luck

Calacuccia
0
 

Author Comment

by:Artemis_23
ID: 2584984
Okay, the thing is I can color all the rows fine, but i have to color every other row.  How do I do that?

Artemis_23
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 17

Accepted Solution

by:
calacuccia earned 100 total points
ID: 2584994
So you only want to color the even rows.

Here you go:

Sub ColorLines
Dim Startline As Integer, Endline As Integer
'For the odd lines, make Startline equal to 1
Startline = 2
'To set the last line to the last used line
Endline = Range("A1").SpecialCells(xlCellTypeLastCell).Row
For i = 2 to Endline Step 2
With Rows(i).Interior
        .ColorIndex = 3
        .Pattern = xlSolid
    End With
Next i
End Sub

Good Luck

Calacuccia
0
 

Author Comment

by:Artemis_23
ID: 2585005
If I want to make it rows 6 - 46 coloring every other....how do i do that?

Artemis_23
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 2585011
Hi Artemis,

just change the declarations of Startline & Endline to respectively 6 and 46. Like this:

Sub ColorLines
Dim Startline As Integer, Endline As Integer
'For the odd lines, make Startline equal to 1
Startline = 6
'To set the last line to the last used line
Endline = 46
For i = 2 to Endline Step 2
With Rows(i).Interior
        .ColorIndex = 3
        .Pattern = xlSolid
    End With
Next i
End Sub

0
 

Author Comment

by:Artemis_23
ID: 2585014
Thanks a lot.  I figured out how to start at 6 and end at 46.  I appreciate your help.  Thanks again.
                          Artemis_23
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 2585055
You're walcomen, Artemis.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

751 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