Solved

VB in Excel

Posted on 2000-03-04
8
155 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
Independent Software Vendors: 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

Technology Partners: 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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

756 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