Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - If Else problem - HELP PLEASE

I am trying to write some code where I start the cursor in a workbook,
and it loops down until the cell in that column is empty changing the
colours of the cells on the way depending what is in the cell. Here is
my attempted effort. Any help would be great, Thanks

Do

If IsEmpty(ActiveCell) = False Then

ElseIf ActiveCell = "zone total" Then
ActiveCell.Select
With Selection.Interior
ColorIndex = 12
Pattern = xlSolid
ActiveCell.Offset(1, 0).Select

ElseIf ActiveCell = "regional total" Then
ActiveCell.Select
With Selection.Interior
ColorIndex = 45
Pattern = xlSolid
ActiveCell.Offset(1, 0).Select


Else
ActiveCell.Select
With Selection.Interior
ColorIndex = 3
Pattern = xlSolid
ActiveCell.Offset(1, 0).Select

End If

Loop Until IsEmpty(ActiveCell) = True

End Sub:)


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Excel VBA - If Else problem - HELP PLEASE

A couple of options:

Sub ChangeColours()
Dim lCurrentColumn As Long ' store for current column number
Dim lCurrentRow As Long ' store for current row number
Dim lLastRow As Long ' store for last row number
Dim lCount As Long ' row counter
lCurrentColumn = ActiveCell.Column ' save current column
lCurrentRow = ActiveCell.Row ' save current row
lLastRow = Cells(Rows.Count, lCurrentColumn).End(xlUp).Row
'MsgBox "CC " & lCurrentColumn & _
" CR " & lCurrentRow & _
" LR " & lLastRow

Application.ScreenUpdating = False
For lCount = lCurrentRow To lLastRow
If IsEmpty(Cells(lCount, lCurrentColumn)) _
= True Then
' do nothing
ElseIf LCase(Cells(lCount, lCurrentColumn)) _
= "zone total" Then
With Cells(lCount, lCurrentColumn).Interior
.ColorIndex = 12
.Pattern = xlSolid
End With
ElseIf LCase(Cells(lCount, lCurrentColumn)) _
= "regional total" Then
With Cells(lCount, lCurrentColumn).Interior
.ColorIndex = 45
.Pattern = xlSolid
End With
Else
With Cells(lCount, lCurrentColumn).Interior
.ColorIndex = 3
.Pattern = xlSolid
End With
End If
Next 'lCount
Application.ScreenUpdating = True
End Sub

' Or with the Select Case approach ...
Sub ChangeColoursSC()
Dim lCurrentColumn As Long ' store for current column number
Dim lCurrentRow As Long ' store for current row number
Dim lLastRow As Long ' store for last row number
Dim lCount As Long ' row counter
lCurrentColumn = ActiveCell.Column ' save current column
lCurrentRow = ActiveCell.Row ' save current row
lLastRow = Cells(Rows.Count, lCurrentColumn).End(xlUp).Row
'MsgBox "CC " & lCurrentColumn & _
" CR " & lCurrentRow & _
" LR " & lLastRow

Application.ScreenUpdating = False
For lCount = lCurrentRow To lLastRow
Select Case Cells(lCount, lCurrentColumn)
Case Empty
' Do nothing
Case "zone total"
With Cells(lCount, lCurrentColumn).Interior
.ColorIndex = 12
.Pattern = xlSolid
End With
Case "regional total"
With Cells(lCount, lCurrentColumn).Interior
.ColorIndex = 45
.Pattern = xlSolid
End With
Case Else
With Cells(lCount, lCurrentColumn).Interior
.ColorIndex = 3
.Pattern = xlSolid
End With
End Select
Next 'lCount
Application.ScreenUpdating = True
End Sub


They don't select any cells so they should be quicker, particularly if there
are a lot of rows.

Regards

Trevor


"Xispo " wrote in message
...
I am trying to write some code where I start the cursor in a workbook,
and it loops down until the cell in that column is empty changing the
colours of the cells on the way depending what is in the cell. Here is
my attempted effort. Any help would be great, Thanks

Do

If IsEmpty(ActiveCell) = False Then

ElseIf ActiveCell = "zone total" Then
ActiveCell.Select
With Selection.Interior
ColorIndex = 12
Pattern = xlSolid
ActiveCell.Offset(1, 0).Select

ElseIf ActiveCell = "regional total" Then
ActiveCell.Select
With Selection.Interior
ColorIndex = 45
Pattern = xlSolid
ActiveCell.Offset(1, 0).Select


Else
ActiveCell.Select
With Selection.Interior
ColorIndex = 3
Pattern = xlSolid
ActiveCell.Offset(1, 0).Select

End If

Loop Until IsEmpty(ActiveCell) = True

End Sub:)


---
Message posted from http://www.ExcelForum.com/



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Colon at the end of excel file name(ex: problem.xls:1, problem.xls financeguy New Users to Excel 2 January 15th 10 01:15 AM
Problem viewing Excel 2003 Pivot Chart fields in Excel 2007 ronny B Charts and Charting in Excel 1 October 24th 08 10:08 PM
Weird problem with Excel 2000...Worksheets disappearing in a shared Excel file BrianL_SF Excel Discussion (Misc queries) 2 October 10th 06 08:27 PM
Started out as an Access problem. Now an Excel problem RobertM Excel Discussion (Misc queries) 2 April 26th 06 07:30 PM
Excel 97 chart opened in Excel 2003 - Source Data problem DHunt Charts and Charting in Excel 0 December 6th 04 08:05 PM


All times are GMT +1. The time now is 07:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"