Note that there are some explanatory texts on larger screens.

plurals
  1. POExcel 2010 VBA - Rows not appending
    primarykey
    data
    text
    <p>I am creating a Macro with VBA in Excel 2010 to move rows from one sheet to another based on the DOB and State cell (all the in same workbook). </p> <p>The macro checks the DOB against a "cut-off" date and if the row passes, the row should be appended to the TSP sheet and deleted from Sheet1.</p> <p>If it didn't pass, then it checks to see if a state sheet exists for the row's "State" cell. If it does, then the row should be appended to the end of that sheet and is deleted from Sheet1.</p> <p>If the row doesn't meet any of the two it is simply left to be manually checked as it either missing data or the data was entered incorrectly.</p> <p>All is working correctly except for the appending of the row to the sheet. It is simply replacing the last row of the sheet except for the OH sheet, which is working for whatever reason.</p> <p>My Macro:</p> <pre><code>Sub Move() ' ' Move Macro ' ' Keyboard Shortcut: Ctrl+Shift+M ' Declare and set variables Dim CBL_DATE Dim totalrows, c Set tsp_sheet = Sheets("TSP") Set people = Sheets("Sheet1") CBL_DATE = DateAdd("yyyy", -59.5, Date) ' Find total number of people to move totalrows = people.UsedRange.Rows.Count ' Step through each row For Row = totalrows To 2 Step -1 ' Do not do anything if row is 1 If Row &gt;= 2 Then ' Check for CBL cut off date and move to TSP sheet If Cells(Row, 3).Value &lt; CBL_DATE Then tsp_sheet.Rows(tsp_sheet.UsedRange.Rows.Count + 1).Value = people.Rows(Row).Value people.Rows(Row).Delete Else ' Now we check for the state and if that worksheet exists, we copy to it and delete original If SheetExists(Cells(Row, 2).Value) Then Set st_sheet = Sheets(Cells(Row, 2).Value) c = st_sheet.UsedRange.Rows.Count + 1 MsgBox Cells(Row, 2).Value &amp; " " &amp; c st_sheet.Rows(c).Value = people.Rows(Row).Value people.Rows(Row).Delete End If End If End If Next Row End Sub ' End Sub Move() </code></pre> <p>My Table for Sheet1</p> <pre> Sheet 1 Name |State |DOB -------------------------- Tim |MI |10/2/1978 Bob |MI |10/5/1949 Suesan |TN |10/8/1978 Debra |OH |10/8/1975 </pre> <p>All the other sheets are blank although I would love to start inserting at the second row (or count+1).</p> <p>EDIT: My SheetExists() function</p> <pre><code>' Public Function SheetExists Public Function SheetExists(SheetName As String) As Boolean Dim ws As Worksheet SheetExists = False For Each ws In ThisWorkbook.Worksheets If ws.Name = SheetName Then SheetExists = True Exit For End If Next ws End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload