+ Reply to Thread
Results 1 to 8 of 8

Issues with row not moving into new worksheet when status is set.

  1. #1
    Registered User
    Join Date
    12-14-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Issues with row not moving into new worksheet when status is set.

    I am trying to set up a macro that takes a row that has a status of "Released", "Crushed", or "Sold" and moves that row to it's appropriate worksheet. I was working with some code I have found, and have gotten the row to move to the worksheet "Released" but it places it in the 33rd row and when you try to run another row it doesn't paste it under it. Anyway theres something going on in my code and I can't figure it out. I need help please!

    I also am not sure if I need to create 3 separate macro's for each status type or if all three can be used in a single macro....

    My Status column in "A" and my data goes all the way to "Y" to be copied.


    Sample Macro:


    Sub Released_Macro1()
    'Note: This macro use the function LastRow
    'Important: The DestSh must exist
    Dim My_Range As Range
    Dim DestSh As Worksheet
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim FilterCriteria As String
    Dim CCount As Long
    Dim rng As Range

    'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
    'and the header of the first column, D is the last column in the filter range.
    'You can also add the sheet name to the code like this :
    'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
    'No need that the sheet is active then when you run the macro when you use this.
    Set My_Range = Range("A3:Y" & LastRow(ActiveSheet))
    My_Range.Parent.Select

    'Set the destination worksheet
    'Note: the sheet "RecordsOfTheNetherlands" must exist in your workbook
    Set DestSh = Sheets("Released")

    If ActiveWorkbook.ProtectStructure = True Or _
    My_Range.Parent.ProtectContents = True Then
    MsgBox "Sorry, not working when the workbook or worksheet is protected", _
    vbOKOnly, "Copy to new worksheet"
    Exit Sub
    End If

    'Change ScreenUpdating, Calculation, EnableEvents, ....
    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    ActiveSheet.DisplayPageBreaks = False

    'Firstly, remove the AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Filter and set the filter field and the filter criteria :
    'This example filter on the first column in the range (change the field if needed)
    'In this case the range starts in A so Field 1 is column A, 2 = column B, ......
    'Use "<>Netherlands" as criteria if you want the opposite
    My_Range.AutoFilter Field:=1, Criteria1:="=Released"

    'If you want to filter on a cell value you can use this, use "<>" for the opposite
    'This example uses the activecell value
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value

    'This will use the cell value from A2 as criteria
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & Range("A2").Value

    ''If you want to filter on a Inputbox value use this
    'FilterCriteria = InputBox("What text do you want to filter on?", _
    ' "Enter the filter item.")
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & FilterCriteria


    'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
    CCount = 0
    On Error Resume Next
    CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
    On Error GoTo 0
    If CCount = 0 Then
    MsgBox "There are more than 8192 areas:" _
    & vbNewLine & "It is not possible to copy the visible data." _
    & vbNewLine & "Tip: Sort your data before you use this macro.", _
    vbOKOnly, "Copy to worksheet"
    Else
    'Copy the visible data and use PasteSpecial to paste to the Destsh
    With My_Range.Parent.AutoFilter.Range
    On Error Resume Next
    ' Set rng to the visible cells in My_Range without the header row
    Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
    .SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not rng Is Nothing Then
    'Copy and paste the cells into DestSh below the existing data
    rng.Copy
    With DestSh.Range("A3" & LastRow(DestSh) + 1)
    ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
    ' Remove this line if you use Excel 97
    .PasteSpecial Paste:=8
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    End With
    'Delete the rows in the My_Range.Parent worksheet
    rng.EntireRow.Delete
    End If
    End With
    End If

    'Close AutoFilter
    My_Range.Parent.AutoFilterMode = False

    'Restore ScreenUpdating, Calculation, EnableEvents, ....
    ActiveWindow.View = ViewMode
    Application.GoTo DestSh.Range("A1")
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = CalcMode
    End With

    End Sub


    Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
    After:=sh.Range("A3"), _
    Lookat:=xlPart, _
    LookIn:=xlValues, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Row
    On Error GoTo 0
    End Function
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Issues with row not moving into new worksheet when status is set.

    Correct me if I am wrong, but you just want to select a status in Cell A3 of the "Main List" worksheet and then based on that status selection copy the whole row to the corresponding worksheet in the next available row?

    Edit: If i am right try this

    Please Login or Register  to view this content.
    Last edited by stnkynts; 12-14-2012 at 11:35 AM.

  3. #3
    Registered User
    Join Date
    12-14-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Issues with row not moving into new worksheet when status is set.

    Yes, But the list will grow so I could have multiple rows in there at a time. So I need a range throughout the whole worksheet based on the A column status. I want it to copy to row then delete it. But I got the delete part to work.

  4. #4
    Registered User
    Join Date
    12-14-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Issues with row not moving into new worksheet when status is set.

    What you gave me copys instantly into the worksheets I have. If I select "Released", "Crushed", or "Sold" it copys appropriatly. However how do I get it to delete it from the main list? Also I am concerned because multiple "new" users will be using this DB so if they accidently click on released or crushed or sold it will auto populate into those worksheets.

  5. #5
    Registered User
    Join Date
    12-14-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Issues with row not moving into new worksheet when status is set.

    Can it have a popup window that makes them confirm this selection just in case?

  6. #6
    Registered User
    Join Date
    12-14-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Issues with row not moving into new worksheet when status is set.

    I got the range figured out:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Now how can I get the row I select to delete? I am super new to VBA programming so I need my hand held

  7. #7
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Issues with row not moving into new worksheet when status is set.

    Try this:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    12-14-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Issues with row not moving into new worksheet when status is set.

    Wow this works perfect!!!!!

    Thank you so much for your help man. :D

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1