Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default How to get row number?

I use the "Do Until...Loop" in forms subroutine to insert a row in worksheet
at specified location. How do I get the row number of the new row?


Private Sub cmdInsertRow_Click()

Dim ws As Worksheet
Dim a As Long
Set ws = Worksheets("Sheet1")

a = 22 'data starts at row 22
Do Until Trim(ws.Cells(a, 1)) = ""
If ws.Cells(a, 1) = Me.cboRefValue.Value Then
ws.Cells(a + 1, 1).EntireRow.Insert
a = a + 2
Else
a = a + 1
End If
Loop

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default How to get row number?

Hi Sam,

After the code where you insert the row, the new row number should be a+1.

--
Regards,

OssieMac


"Sam Kuo" wrote:

I use the "Do Until...Loop" in forms subroutine to insert a row in worksheet
at specified location. How do I get the row number of the new row?


Private Sub cmdInsertRow_Click()

Dim ws As Worksheet
Dim a As Long
Set ws = Worksheets("Sheet1")

a = 22 'data starts at row 22
Do Until Trim(ws.Cells(a, 1)) = ""
If ws.Cells(a, 1) = Me.cboRefValue.Value Then
ws.Cells(a + 1, 1).EntireRow.Insert
a = a + 2
Else
a = a + 1
End If
Loop

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default How to get row number?

Yes! Thanks OssieMac :-)

Sam

"OssieMac" wrote:

Hi Sam,

After the code where you insert the row, the new row number should be a+1.

--
Regards,

OssieMac


"Sam Kuo" wrote:

I use the "Do Until...Loop" in forms subroutine to insert a row in worksheet
at specified location. How do I get the row number of the new row?


Private Sub cmdInsertRow_Click()

Dim ws As Worksheet
Dim a As Long
Set ws = Worksheets("Sheet1")

a = 22 'data starts at row 22
Do Until Trim(ws.Cells(a, 1)) = ""
If ws.Cells(a, 1) = Me.cboRefValue.Value Then
ws.Cells(a + 1, 1).EntireRow.Insert
a = a + 2
Else
a = a + 1
End If
Loop

End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to get row number?

Just as a follow up, you don't need to run a loop to do what your posted
code does. Give this a try (it will be more efficient than your loop method,
especially if the search list is long)...

Private Sub cmdInsertRow_Click()
Dim Result As Range
Dim NewRow As Long
Const DataStartRow As Long = 22
With Worksheets("Sheet1")
Set Result = .Cells(DataStartRow, "A").Resize(.Rows.Count - _
DataStartRow + 1, 1).Find(Me.cboRefValue.Value, _
LookIn:=xlValues)
End With
If Not Result Is Nothing Then
Result.EntireRow.Offset(1, 0).Insert
NewRow = Result.Row + 1
MsgBox "A blank row was added at Row #" & NewRow
Else
MsgBox "No row was added."
End If
End Sub

You can, of course, eliminate the two MessageBox statements (and the Else
block)... I just included them for demonstration purposes.

Rick


"Sam Kuo" wrote in message
...
Yes! Thanks OssieMac :-)

Sam

"OssieMac" wrote:

Hi Sam,

After the code where you insert the row, the new row number should be
a+1.

--
Regards,

OssieMac


"Sam Kuo" wrote:

I use the "Do Until...Loop" in forms subroutine to insert a row in
worksheet
at specified location. How do I get the row number of the new row?


Private Sub cmdInsertRow_Click()

Dim ws As Worksheet
Dim a As Long
Set ws = Worksheets("Sheet1")

a = 22 'data starts at row 22
Do Until Trim(ws.Cells(a, 1)) = ""
If ws.Cells(a, 1) = Me.cboRefValue.Value Then
ws.Cells(a + 1, 1).EntireRow.Insert
a = a + 2
Else
a = a + 1
End If
Loop

End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default How to get row number?

Thanks Rick. I've learnt something here :-)

Sam

"Rick Rothstein (MVP - VB)" wrote:

Just as a follow up, you don't need to run a loop to do what your posted
code does. Give this a try (it will be more efficient than your loop method,
especially if the search list is long)...

Private Sub cmdInsertRow_Click()
Dim Result As Range
Dim NewRow As Long
Const DataStartRow As Long = 22
With Worksheets("Sheet1")
Set Result = .Cells(DataStartRow, "A").Resize(.Rows.Count - _
DataStartRow + 1, 1).Find(Me.cboRefValue.Value, _
LookIn:=xlValues)
End With
If Not Result Is Nothing Then
Result.EntireRow.Offset(1, 0).Insert
NewRow = Result.Row + 1
MsgBox "A blank row was added at Row #" & NewRow
Else
MsgBox "No row was added."
End If
End Sub

You can, of course, eliminate the two MessageBox statements (and the Else
block)... I just included them for demonstration purposes.

Rick


"Sam Kuo" wrote in message
...
Yes! Thanks OssieMac :-)

Sam

"OssieMac" wrote:

Hi Sam,

After the code where you insert the row, the new row number should be
a+1.

--
Regards,

OssieMac


"Sam Kuo" wrote:

I use the "Do Until...Loop" in forms subroutine to insert a row in
worksheet
at specified location. How do I get the row number of the new row?


Private Sub cmdInsertRow_Click()

Dim ws As Worksheet
Dim a As Long
Set ws = Worksheets("Sheet1")

a = 22 'data starts at row 22
Do Until Trim(ws.Cells(a, 1)) = ""
If ws.Cells(a, 1) = Me.cboRefValue.Value Then
ws.Cells(a + 1, 1).EntireRow.Insert
a = a + 2
Else
a = a + 1
End If
Loop

End Sub



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
Sumif number is greater than a number but less than another number lulu151 Excel Discussion (Misc queries) 2 May 7th 10 07:12 PM
How can I click on a telephone number in an Excel 2002 spreadsheet, and have the number dialed? jbclem Excel Discussion (Misc queries) 2 August 13th 09 01:57 AM
How to calculate number of occurencies of a specific number number Stefan Excel Discussion (Misc queries) 4 September 8th 08 08:33 AM
find an exact number in a different sheet and use the cell containing the number to output information [email protected] Excel Programming 4 February 18th 07 08:59 PM
Rounding a number to a multiple quantity that adds to a fixed total number wjlo Excel Worksheet Functions 1 November 9th 04 04:43 PM


All times are GMT +1. The time now is 04:51 PM.

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"