Solved

VB in Excel

Posted on 2000-03-04
8
154 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

789 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