View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.misc
Lars-Åke Aspelin[_2_] Lars-Åke Aspelin[_2_] is offline
external usenet poster
 
Posts: 913
Default Visual Basic: Get row count of a sheet in Excel

On Mon, 6 Jul 2009 09:07:39 -0700 (PDT), Curious
wrote:

Thanks to all who provided their help! Now I have the following script
and got an "application-defined or object-defined" error at the line
marked below:

Sub MatchShortTermLimits()

myrowcount = Worksheets("LongTermLimits").UsedRange.Rows.Count

Sheets("LongTermLimits").Select
Dim r As Range, cell As Range
With ActiveSheet
Set r = .Range(.Cells(2, "D"), .Cells(myrowcount + 1, "D").End
(xlToLeft))
End With

i = 2
Do While i <= myrowcount + 1
If ActiveSheet.Cells(E, i) = "S" Then ' "application-
defined or object-defined" error
ActiveSheet.Cells(J, i).Value = Max(ActiveSheet.Cells(D,
i), ActiveSheet.Cells(H, i))
Else
If ActiveSheet.Cells(E, i) = "B" Then
ActiveSheet.Cells(J, i).Value = Min(ActiveSheet.Cells
(D, i), ActiveSheet.Cells(H, i))
End If
End If
i = i + 1
Loop

ActiveSheet.Save

End Sub

It seems that "ActiveSheet.Cells(E, i)" causes the problem. Any input?



Change
Cells(E,i) to Cells(i,"E")
Cells(J,i) to Cells(i,"J")
Cells(D,i) to Cells(i,"D")
Cells(H,i) to Cells(i,"H")
etc

Hope this helps / Lars-Åke