+ Reply to Thread
Results 1 to 15 of 15

Thread: Macro to delete the rows automatically

  1. #1
    Registered User
    Join Date
    06-08-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    8

    Macro to delete the rows automatically

    Hi I have a requirement
    when a particular row is deleted in sheet1, the corresponding row should be automatically deleted from sheet2. column A in both the sheets is ID field which is unique.Please help to solve this problem

  2. #2
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: Macro to delete the rows automatically

    in both sheets those unique ID is in the same row or before delete you have to check ID's position in sheet 2 ?

    Best Regards

  3. #3
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Macro to delete the rows automatically

    I don't think it's possible to trap a row being deleted manually so that code can automatically do what you are asking. The only way really would be to use the code to delete the rows on both sheets.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  4. #4
    Registered User
    Join Date
    06-08-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to delete the rows automatically

    the unique id in both the sheets is in different rows but they are in column A

  5. #5
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Cool Re: Macro to delete the rows automatically

    I hope solution for your problem is made - check attached file

    @Domski - never say never ;-)

    Best Regards
    Last edited by maczaq; 06-09-2011 at 07:10 AM.

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Macro to delete the rows automatically

    Good thinking although your workbook open event will fall over as Me.UsedRange is not valid. You would also need to trap for multiple rows being deleted.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  7. #7
    Registered User
    Join Date
    06-08-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to delete the rows automatically

    hi Maczaq...appreciate for your help..but it is throwing some error in Usedrange when the sheet is opened..can you please see to it

  8. #8
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: Macro to delete the rows automatically

    correct file attached

    error is solved
    multiply row delete now is available too (thanks Domski for info)

    Best Regards
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    06-08-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to delete the rows automatically

    thanks a lot...can you please tell me how to import this code so that it works perfectly

  10. #10
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: Macro to delete the rows automatically

    hi

    Press ALT+F11 in your document (VBA editor should appear)
    drop menu View -> Project Explorer (make sure it is open, should be on the left side)

    click twice on Sheet1 and paste there a code:
    'This will hold the number of rows used in the spreadsheet before the change.
    'We’ll compare it to the number of rows after the change to determine if one has been deleted.
    Public rowCount As Long
    Public rr, rc As Long
    
    Private tmpID() As String  ' memory for remember ID's of selected row
    
    Private Sub Worksheet_Activate()
        rowCount = Me.UsedRange.Rows.Count
    End Sub
     
    Private Sub Worksheet_Change(ByVal Target As Range)
            If Me.UsedRange.Rows.Count < rowCount Then
             Dim r As Long 'for index of row for ID value in sheet 2
             'dalete row with the same ID in sheet 2
             On Error Resume Next
              'find row(s) in sheet 2 to delete
              For i = 0 To UBound(tmpID)
                r = Sheet2.Range("A:A").Find(tmpID(i), SearchOrder:=xlByRows, Lookat:=xlWhole, SearchDirection:=xlNext).Row
                 Sheet2.Rows(r & ":" & r).Delete Shift:=xlUp 'delete in sheet 2
              Next i
            End If
      rowCount = Sheet1.UsedRange.Rows.Count 'set rowCount
    End Sub
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ReDim tmpID(Selection.Rows.Count)
        For i = 0 To Selection.Rows.Count - 1
          tmpID(i) = Cells(Target.Row + i, 1).Value
        Next i
    End Sub
    Then press twice on ThisWorkbook and past following code
    Private Sub Workbook_Open()
     rowCount = Sheet1.UsedRange.Rows.Count 'new rowCount
    End Sub
    Consider to mark this thread as solved and if you are satisfacted as well please click on my reputation's add button

    Best Regards

  11. #11
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Macro to delete the rows automatically

    Hi,

    I know I'm being picky and would have a go myself if I didn't have so much on today but I'm not sure that will work with multiple rows, especially if the user were to select more then one that were non-contiguous.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  12. #12
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: Macro to delete the rows automatically

    You have right Domski - provided solution works fine only for one deleted row or for selected rows (row by row)

    I will think about update to catch that

    Thanks for your ideas
    Best Regards

  13. #13
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Thumbs up Re: Macro to delete the rows automatically

    Hello All,

    I update the script, now it works for non-contiguous selections.

    I hope no more bugs

    Best Regards
    MaczaQ
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    06-08-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    8

    Thumbs up Re: Macro to delete the rows automatically

    hi Maczaq..Thanks a lot for your help..appreciate your efforts to solve the issue...

  15. #15
    Valued Forum Contributor
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    412

    Re: Macro to delete the rows automatically

    hi mohan308

    I see You have satisfaction and so have I.

    Please consider to mark this thread as SOLVED and if you want click on add button to add me reputations points

    Best Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0