Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default For loop - update your expression to evaluate

Hello,

I am using a for loop to loop through the cells in a column until the last
row. When I hit a cell where the value changes, I want to insert a row above
that. This will cause the evaluation expression to not be correct. I have
tried a few different things, but nothing allows me to update that expression
after I insert a new row. So my loop stops short. I'm am attaching my code
for review. My code may not be the most efficient, because I'm just starting
and learning about Objects.

Sub NewImprovedAddRows()

Dim curCellCol As Integer
Dim curCellRow As Integer
Dim curCellVal As Integer
Dim curCellAddr As String

Dim nextCellCol As Integer
Dim nextCellRow As Integer
Dim nextCellVal As Integer
Dim nextCellAddr As String

Dim i As Integer
For i = 1 To ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Cells(i, "A").Activate
curCellVal = ActiveCell.Value
curCellRow = ActiveCell.Row
nextCellRow = Cells(i + 1, "A").Row
nextCellVal = Cells(i + 1, "A").Value
nextCellAddr = Cells(i + 1, "A").Address


If IsNull(nextCellVal) Then
MsgBox ("The next cell is empty")

ElseIf nextCellVal < curCellVal Then

Range(nextCellAddr, Range(nextCellAddr).End(xlToRight)).Activate

ActiveCell.EntireRow.Insert

i = ActiveCell.Row
Else
MsgBox ("help" & curCellRow & ", " & curCellVal & ", " & nextCellRow &
", " & nextCellVal)

End If

Next

End Sub


--
Michael Randall
Student
Keller Graduate School of Management
Masters of Science - Information Systems Management
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default For loop - update your expression to evaluate

Hi,

Try this

Sub insertrowifvalechg()
'insert a row at every name change in a column
Set sht = Sheets("Sheet1") ' change to suit
MyColumn = "A"
Dim x As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For x = Cells(Rows.Count, MyColumn).End(xlUp).Row To 2 Step -1
If Cells(x - 1, MyColumn) < Cells(x, MyColumn) Then Rows(x).Insert
Next x
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Michael_Randall" wrote:

Hello,

I am using a for loop to loop through the cells in a column until the last
row. When I hit a cell where the value changes, I want to insert a row above
that. This will cause the evaluation expression to not be correct. I have
tried a few different things, but nothing allows me to update that expression
after I insert a new row. So my loop stops short. I'm am attaching my code
for review. My code may not be the most efficient, because I'm just starting
and learning about Objects.

Sub NewImprovedAddRows()

Dim curCellCol As Integer
Dim curCellRow As Integer
Dim curCellVal As Integer
Dim curCellAddr As String

Dim nextCellCol As Integer
Dim nextCellRow As Integer
Dim nextCellVal As Integer
Dim nextCellAddr As String

Dim i As Integer
For i = 1 To ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Cells(i, "A").Activate
curCellVal = ActiveCell.Value
curCellRow = ActiveCell.Row
nextCellRow = Cells(i + 1, "A").Row
nextCellVal = Cells(i + 1, "A").Value
nextCellAddr = Cells(i + 1, "A").Address


If IsNull(nextCellVal) Then
MsgBox ("The next cell is empty")

ElseIf nextCellVal < curCellVal Then

Range(nextCellAddr, Range(nextCellAddr).End(xlToRight)).Activate

ActiveCell.EntireRow.Insert

i = ActiveCell.Row
Else
MsgBox ("help" & curCellRow & ", " & curCellVal & ", " & nextCellRow &
", " & nextCellVal)

End If

Next

End Sub


--
Michael Randall
Student
Keller Graduate School of Management
Masters of Science - Information Systems Management

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
evaluate a concatenate expression Russell.Ivory[_2_] Excel Worksheet Functions 3 November 4th 09 05:52 PM
How to evaluate string form of numeric expression in VBA? [email protected] Excel Programming 6 December 31st 07 05:23 PM
evaluate a year expression Clint Eastwood[_2_] Excel Programming 2 November 8th 07 04:50 PM
Evaluate Custom Expression Peter Excel Programming 7 April 3rd 06 01:18 PM
How to evaluate a text expression as formula ? Krzysztof Klimczak Excel Programming 0 August 29th 03 04:31 PM


All times are GMT +1. The time now is 06:35 AM.

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

About Us

"It's about Microsoft Excel"