Delete cell in worksheet based on selected Row in Listbox

zubin

New Member
Joined
Sep 15, 2019
Messages
47
Hi everyone....
I Have a userform with a listbox which is populated with row source property.

After selecting a row in the Userform, A delete Key should remove the selected Row in the list box and the corresponding worksheet cell values in the range "B4 to B14" and "E4 to E14"
and cells values below (If at all present) these should shift up...... and NOT the whole column.

I am so far able to delete only values from one column ie "B" and that too the whole column beyond "B14" shifts up, this should not happen as there is another table below this range.

Columns "C", "D" and "F" in the worksheet Range B4:F17 have formulas and are populated as per the values of Column "B" and "E".
Currently I am using this code.....

Please Help....


VBA Code:
Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub CommandButton2_Click()

    
    Dim i As Long
        
    For i = 0 To Me.ListBox1.ListCount - 1
            
        If Me.ListBox1.Selected(i) = True Then
          
           Range("B" & i + 4).Delete Shift:=xlUp
          
         End If
        
    Next i

End Sub




Book1.xlsm
ABCDEFGHI
1
2
3S.No.BarCodeItemUnit PriceQty.Total
428
533
625
754
853
932
1021
1132
1289
1378
14
15
16
17
18
19
20582
2187
2278
2389
2497
2568
2615
2782
28
29
30
31
Sheet1
 
Correction :

VBA Code:
Call subDeleteAndMoveUp(ActiveSheet.Range("B" & intRow))
Your Question.....(When the Delete button is selected how do you ascertain the appropriate row number.?)
I am not sure...but something like...
VBA Code:
For i = 0 To Me.ListBox1.ListCount - 1    
        If Me.ListBox1.Selected(i) = True Then
Please give me the correct form.....of putting this statement
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Your Question.....(When the Delete button is selected how do you ascertain the appropriate row number.?)
I am not sure...but something like...
VBA Code:
For i = 0 To Me.ListBox1.ListCount - 1   
        If Me.ListBox1.Selected(i) = True Then
Please give me the correct form.....of putting this statement
Initially I was using the below code, but this way all the cell values below the range B14 are also shifted up,


Code:
Private Sub CommandButton2_Click()

    
    Dim i As Long
        
    For i = 0 To Me.ListBox1.ListCount - 1
            
        If Me.ListBox1.Selected(i) = True Then
          
           Range("B" & i + 4).Delete Shift:=xlUp
          
         End If
        
    Next i

End Sub
 
Upvote 0
So substitute your code for this :

I've just added in the function calls for columns Band E and commented out the line
Range("B" & i + 4).Delete Shift:=xlUp

Let me know if this does the job.

Use a copy of the data though.

VBA Code:
Private Sub CommandButton2_Click()
Dim i As Long
        
    For i = 0 To Me.ListBox1.ListCount - 1
            
        If Me.ListBox1.Selected(i) = True Then
          
           ' Range("B" & i + 4).Delete Shift:=xlUp
           
            Call subDeleteAndMoveUp(ActiveSheet.Range("B" & i + 4))
          
            Call subDeleteAndMoveUp(ActiveSheet.Range("E" & i + 4))

         End If
        
    Next i

End Sub
 
Upvote 0
So substitute your code for this :

I've just added in the function calls for columns Band E and commented out the line
Range("B" & i + 4).Delete Shift:=xlUp

Let me know if this does the job.

Use a copy of the data though.

VBA Code:
Private Sub CommandButton2_Click()
Dim i As Long
       
    For i = 0 To Me.ListBox1.ListCount - 1
           
        If Me.ListBox1.Selected(i) = True Then
         
           ' Range("B" & i + 4).Delete Shift:=xlUp
          
            Call subDeleteAndMoveUp(ActiveSheet.Range("B" & i + 4))
         
            Call subDeleteAndMoveUp(ActiveSheet.Range("E" & i + 4))

         End If
       
    Next i

End Sub
Thanks for your time Herakles......Its working beautiful.....minor changes needed...
1. The row below the selected row is getting deleted,
2. and say for example there are 7 rows filled from top ie B4:B10 and E4:E10 and say i select any row from the last 3 rows ie row 8,9 or 10 then all other data below the 14th row is getting pulled up otherwise every thing is fine.
 
Upvote 0
Thanks for your time Herakles......Its working beautiful.....minor changes needed...
1. The row below the selected row is getting deleted,
2. and say for example there are 7 rows filled from top ie B4:B10 and E4:E10 and say i select any row from the last 3 rows ie row 8,9 or 10 then all other data below the 14th row is getting pulled up otherwise every thing is fine.
Ok sorry... the first problem was an error in the named range.....It was "=Sheet1!$B$3:$F$17" instead of "=Sheet1!$B$4:$F$17" Now the selected row is being deleted correctly.
But However the second problem is still persisting.
 
Upvote 0
Ok sorry... the first problem was an error in the named range.....It was "=Sheet1!$B$3:$F$17" instead of "=Sheet1!$B$4:$F$17" Now the selected row is being deleted correctly.
But However the second problem is still persisting.
Also I realized that if there is any value in cells B16 to B18 and E16 to E18 are also been pulled up irrespective of which row i am deleting....
 
Upvote 0
Also I realized that if there is any value in cells B16 to B18 and E16 to E18 are also been pulled up irrespective of which row i am deleting....
In short....No value present in cells 15th row and below should get pulled up
 
Upvote 0
This should fix the problem.

VBA Code:
Public Sub subDeleteAndMoveUp(rngToDelete As Range)
Dim rng As Range
Dim intlastRow As Integer
Dim i As Integer

    intlastRow = 14
   
    For i = 1 To 11
        If IsEmpty(Cells(i + 3, rngToDelete.Column)) Then
            intlastRow = i + 3 - 1
            Exit For
        End If
    Next i
        
    Set rng = Cells(4, rngToDelete.Column).Resize(intlastRow - 3, 1)
    
    If rngToDelete.Row = intlastRow Then
            
        rngToDelete.Value = ""
        
    Else
        
        Set rng = Cells(4, rngToDelete.Column).Resize(intlastRow - 3, 1).Range(rng.Cells(rngToDelete.Row - 5, 0), rng.Cells(8, 0))
                        
        rng.Offset(-1, 0).Value = rng.Value
        
        Cells(intlastRow, rngToDelete.Column).Value = ""

    End If
        
End Sub
 
Upvote 0
Solution
This should fix the problem.

VBA Code:
Public Sub subDeleteAndMoveUp(rngToDelete As Range)
Dim rng As Range
Dim intlastRow As Integer
Dim i As Integer

    intlastRow = 14
  
    For i = 1 To 11
        If IsEmpty(Cells(i + 3, rngToDelete.Column)) Then
            intlastRow = i + 3 - 1
            Exit For
        End If
    Next i
       
    Set rng = Cells(4, rngToDelete.Column).Resize(intlastRow - 3, 1)
   
    If rngToDelete.Row = intlastRow Then
           
        rngToDelete.Value = ""
       
    Else
       
        Set rng = Cells(4, rngToDelete.Column).Resize(intlastRow - 3, 1).Range(rng.Cells(rngToDelete.Row - 5, 0), rng.Cells(8, 0))
                       
        rng.Offset(-1, 0).Value = rng.Value
       
        Cells(intlastRow, rngToDelete.Column).Value = ""

    End If
       
End Sub
Herakles.....I don't know how to thank you, Perfect...the way i wanted and even better.
Problem solved'
Thanks a Ton
 
Upvote 0
Finding the last non-blank cell in a range proved to be unreliable using suggested methods hence the loop.

Another option would have been to clear the range and re-write it by looping through the unselected items in the listbox.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top