View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default Trouble with 2 range definitions ...

Give this a try... I declared the rest of your varaibles and objects and
moved the end with to the very bottom of the code (that is why you are
getting the unqualified reference error)... The . is not referenced by the
with statement any longer. Also be sure to add Option Explicit to the top of
your code modules to require variable declarations. Check out this link...

http://www.cpearson.com/excel/DeclaringVariables.htm

Option Explicit

Sub BlankNums()
Dim rng As Range, rng1 As Range
Dim rng2 As Range, rng2A As Range
Dim rng3 As Range, rng3A As Range
Dim rng4 As Range
Dim cell As Range, cellA As Range
Dim icol As Integer, jcol As Integer

Sheets("Input 502 & 504").Select
With Worksheets("Input 502 & 504")
ActiveSheet.UsedRange
Set rng = .Range(.Cells(4, 4), _
..Cells(4, 256).End(xlToLeft))
Set rng2A = rng.Find(What:="Code", After:=rng(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=True)
icol = rng2A.Column

Set rng1 = .Range(.Cells(6, 3), _
..Cells(Rows.Count, 3).End(xlUp))
Set rng2 = .Range(.Cells(6, icol), _
..Cells(Rows.Count, icol).End(xlUp))
Set rng3A = rng.Find(What:="Detailed Description", After:=rng(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=True)
jcol = rng3A.Column


For Each cell In rng1
If IsEmpty(cell) Then
Set rng3 = Range(Cells(cell.Row, 4), .Cells(cell.Row, (jcol - 1)))
If Not IsEmpty(rng3) Then
cell.Interior.ColorIndex = 6
End If
End If
For Each cellA In rng2

If IsEmpty(cellA) Then
Set rng4 = .Range(.Cells(cellA.Row, (cellA.Column + 1)), .Cells(cellA.Row,
Cells(Columns.Count).End(xlToLeft)))
If Not IsEmpty(rng4) Then
cellA.Interior.ColorIndex = 6
End If
End If
If cell = "Code" Then
cell.Interior.ColorIndex = xlNone
End If
Next
Next
End With

End Sub

--
HTH...

Jim Thomlinson


"Celt" wrote:


Hi Jim,
I just realized I had those two lines in the wrong place (I am still
getting my feet wet with VB). Sorry for the confusion.

Yes they are range objects. The macro runs up until the first instance
of ".Cells" when I Set rng3, then I get an "Invaild ir unqualified
reference" message.

Here is my whole macro.

Sub BlankNums()
Dim rng As Range, rng1 As Range
Dim rng2 As Range, rng2A As Range
Dim rng3 As Range, rng3A As Range
Dim cell As Range, cellA As Range

Sheets("Input 502 & 504").Select
With Worksheets("Input 502 & 504")
ActiveSheet.UsedRange
Set rng = .Range(.Cells(4, 4), _
.Cells(4, 256).End(xlToLeft))
Set rng2A = rng.Find(What:="Code", After:=rng(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=True)
icol = rng2A.Column

Set rng1 = .Range(.Cells(6, 3), _
.Cells(Rows.Count, 3).End(xlUp))
Set rng2 = .Range(.Cells(6, icol), _
.Cells(Rows.Count, icol).End(xlUp))
Set rng3A = rng.Find(What:="Detailed Description", After:=rng(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=True)
jcol = rng3A.Column


End With
For Each cell In rng1
If IsEmpty(cell) Then
Set rng3 = .Range(.Cells(cell.Row, 4), .Cells(cell.Row, (jcol - 1)))
If Not IsEmpty(rng3) Then
cell.Interior.ColorIndex = 6
End If
End If
For Each cellA In rng2

If IsEmpty(cellA) Then
Set rng4 = .Range(.Cells(cellA.Row, (cellA.Column + 1)),
.Cells(cellA.Row, Cells(Columns.Count).End(xlToLeft)))
If Not IsEmpty(rng4) Then
cellA.Interior.ColorIndex = 6
End If
End If
If cell = "Code" Then
cell.Interior.ColorIndex = xlNone
End If
Next
Next
End Sub


--
Celt
------------------------------------------------------------------------
Celt's Profile: http://www.excelforum.com/member.php...o&userid=19413
View this thread: http://www.excelforum.com/showthread...hreadid=529338