+ Reply to Thread
Results 1 to 10 of 10

Delete/Move rows

  1. #1
    Registered User
    Join Date
    01-10-2007
    Posts
    3

    Delete/Move rows

    I have a sheet that holds my inventory and has 3 colums. The colums are serialnumber, techid and date. I also have a sheet for each techid. Is there a way to delete/move a row of data if the serialnumber is entered on another sheet.

    example.

    I have a serialnumber added to my first sheet. I go to a techid sheet and enter that same serial number. Is there a way to copy the entire row the same serialnumber is on and copy to the current sheet? Possibly a search function where I enter a serialnumber and it finds the same thing on the main sheet and copies the rows into the current sheet.



    Thanks for any help.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Response

    Try pasting the following into the ThisWorkbook tab in the VBA editor (Alt F11)

    Dim Working As Boolean

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Working = True Then Exit Sub
    Working = True
    TechID = Sh.Name
    For Each Cell In Target
    If Application.CountIf(Sheets("Sheet1").Columns(1), Cell) > 0 Then
    Sheets("Sheet1").Rows(Sheets("Sheet1").Columns(1).Find(Cell, , xlValues, xlWhole).Row).Copy Destination:=Cell.EntireRow
    End If
    Next Cell
    Working = False
    End Sub



    I've assumed that you main data is stored on Sheet1
    Martin

  3. #3
    Registered User
    Join Date
    01-10-2007
    Location
    NJ
    Posts
    19

    Try VLOOKUP

    If you have the techid list on another tab, you could also make use VLOOKUP to cross reference the technician. If you copy the VLOOKUP over onto several columns, you can then pull in several fields of related data from the techID table.

  4. #4
    Registered User
    Join Date
    01-10-2007
    Posts
    3
    Wow your good!!

    I changed the copy command to cut but is there a way to make it remove the blank row in sheet1?
    Also is there a way to allow me to add them back into sheet1?

    I removed the techID column as I have a sheet for each tech. Seemed pointless to have it.

    And thanks alot, this should help out quite a bit.

    I tried using VLOOKUP but I wasnt having much luck.
    Last edited by ganjo; 01-10-2007 at 04:36 PM.

  5. #5
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Interested Observer

    Quote Originally Posted by mrice
    For Each Cell In Target
    If Application.CountIf(Sheets("Sheet1").Columns(1), Cell) > 0 Then
    Sheets("Sheet1").Rows(Sheets("Sheet1").Columns(1).Find(Cell, , xlValues, xlWhole).Row).Copy Destination:=Cell.EntireRow
    End If
    Next Cell
    Hi MRice, I'm a newbie at VBA and am learning through this board. I'm trying to figure out how this would work...
    Does Target refer to Sheet 1 or the Sheet you're on (TechID)?
    The "xlValues" is automatically known from the present cell in TechID?
    When would the "CountIf" statement give you a value of "0"?

    Thanks
    ChemistB

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Response

    Target actually refers to the cells that have had their values changed. I used a for-each loop just in case multiple cells were filled at the same time.

    The countif function could give a zero if the value for the serial number wasn't on the first sheet at all. In this case the subsequent lines where it tries to find the row on which the serial number occurs would go into error. It's just a bit of code to make things a bit more robust.

    Xlvalues tells the FIND function to do a match on the cells value as opposed to formula or comment.

    Hope this helps.

    PS. Interested in the user name - I'm a chemist as well.

  7. #7
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Response

    To delete the source row, you need to keep a record of the row number

    Dim Working As Boolean

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Working = True Then Exit Sub
    Working = True
    TechID = Sh.Name
    For Each Cell In Target
    If Application.CountIf(Sheets("Sheet1").Columns(1), Cell) > 0 Then
    SourceRow = Sheets("Sheet1").Columns(1).Find(Cell, , xlValues, xlWhole).Row
    Sheets("Sheet1").Rows(SourceRow).Copy Destination:=Cell.EntireRow
    Sheets("Sheet1").Rows(SourceRow).Delete
    End If
    Next Cell
    Working = False
    End Sub

  8. #8
    Registered User
    Join Date
    01-10-2007
    Posts
    3
    Thanks again for the help.

    Is it possible to make this work on any sheet opposed to just sheet1? Then I could have another sheet for all completed work and move them out of the tech sheet.

  9. #9
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Delete/Move Rows

    Thanks Martin,

    I'm going to play around with this a bit until I understand it.

    ChemistB
    Yes, I also am a chemist, working for a J&J company in a QA Lab.

  10. #10
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Response

    Yes - just substitute the name of the sheet that you want to use between the quotation marks.

+ 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