Solved

VB in Excel

Posted on 2000-03-04
8
150 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now