LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default "Add to Selection" , SendKeys, Delete Nth Row


Thanks to both Tom and visdev1.

Tom, I implemented yours and it works well. It taught me "Union" an
"Is Nothing." I just made one small change so if the range is -not
empty, the deletion runs.

For those interested in a snippet, here's the revised code to "Delet
all rows except the Nth one":


Code
-------------------
Sub proDeleteRows()
'Note that the variables used to determine "varNthKeep" are designed for numerical simulation in this example, but varNthKeep could be assigned whatever way works best for your application.

Application.StatusBar = "Now Deleting Rows."

Dim varRecordInterval As Double 'Interval to Keep data (i.e. every 5 seconds)
Dim varTimeStep As Double 'Timestep used in the simulation
Dim varLastRow As Integer
varLastRow = 'Assign this somehow.

Dim varNthKeep As Integer 'Variable to store the "row keeping" interval
varNthKeep = varRecordInterval / varTimeStep ' Calculates N
Dim varNthCounter As Integer ' Counter used in For Loop
varNthCounter = 1
Dim varDeleteRange As Range

'Select the last row of the simulation and temporarily paste its values
'into row 3 so this last row is not lost during the deletion.

Rows(varLastRow).Select
Selection.Copy
Rows(3).Select
Selection.PasteSpecial Paste:=xlPasteValues


Dim i As Integer 'Counter for the FOR Loop, initialized at 6 (the first non-zero data row)
i = 6
Dim k As Integer 'Counts number of data rows remaining after deletion
k = 0

For i = 6 To varLastRow 'Loop from row 6 (first non-zero entry) to where the data should end, plus a buffer to handle any remainder from the division.

If varNthCounter < varNthKeep Then ' Check to see if the counter is on N
'If not, then this is a row to delete.

If varDeleteRange Is Nothing Then 'If the range is still empty (first i row)
Set varDeleteRange = Rows(i) 'Give the range a value
Else
Set varDeleteRange = Union(varDeleteRange, Rows(i)) 'Add this row to the range
End If

varNthCounter = varNthCounter + 1 'Increment the NthCounter

ElseIf varNthCounter = varNthKeep And Cells(k + 1, 1) < "" Then 'If it is on an N row and this is not the last row:
k = k + 1 'Don't mark for deletion, just increment the "number of data rows still left after deletion" counter
varNthCounter = 1 'Reset the N counter, and allow the loop to go to the next row.

End If

Next

If Not varDeleteRange Is Nothing Then
varDeleteRange.Delete
End If

'Paste that original last data row onto the end of the new data.
Rows(3).Select
Selection.Cut
Rows(k + 6).Select
ActiveSheet.Paste

End Su
-------------------



visdev, your method also looks like it would work if looped properly
and Union was used like in Tom's code. The trick is skipping every Nt
row in the deletion. I especially like using String as opposed t
Range. (Range variables look gigantic and confusing in the watc
window.)

Thanks again

--
EphesiansSi
-----------------------------------------------------------------------
EphesiansSix's Profile: http://www.excelforum.com/member.php...fo&userid=3572
View this thread: http://www.excelforum.com/showthread.php?threadid=56483

 
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
Excel "Move or Copy" and "Delete" sheet functions dsiama Excel Worksheet Functions 1 December 28th 07 01:57 PM
Icon for "Wrap" and for "Centre Across Selection" BeSmart Excel Worksheet Functions 2 November 16th 05 06:44 PM
sendkeys "^v" behavior changed with the office 2003 version? Tarzan of the VisualBasic Gorillas Excel Programming 0 September 3rd 04 05:02 PM
SENDKEYS change the "Number Lock" status Kenneth Lam Excel Programming 1 May 23rd 04 12:37 PM
Adding "New" "Insert" "Delete" into a workbook to change from data 1 to data 2 etc Bob Reynolds[_2_] Excel Programming 0 March 4th 04 08:52 PM


All times are GMT +1. The time now is 03: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"