I am strugglig with the problem. Here is the image 1 after selecting one of element in listbox and then if I click delete material (commandbutton).
1.png
It has to delete the select material in sheet 2 and has to cut the element below and move to 1 row above as shown in figure 2.
2.png
At the same time it has to delete the entire respective column and remaining column has to be cut and place in column before in sheet 3 as shown in figure 3
3.png
For that I had wrote the program which is below
Private Sub CommandButton5_Click()
Dim i, j, k, m, n As Integer
i = ListBox1.ListIndex
j = Sheet2.Cells(1, 39).Value
k = j - i
For k = 1 To k
Sheet2.Cells(k + i + 1, 1) = Sheet2.Cells(k + i + 2, 1)
Sheet2.Cells(k + i + 1, 2) = Sheet2.Cells(k + i + 2, 2)
Next k
m = Worksheets("sheet3").Cells(1, i + 2).Value
For m = 1 To m + 2
Worksheets("sheet3").Cells(m, i + 2).ClearContents
Next m
n = Sheet3.Cells(1, i + 3).Value
For k = 1 To k
For n = 1 To n + 2
Sheet3.Cells(n, k + 1 + i) = Sheet3.Cells(n, k + 2 + i)
Next n
Next k
Sheet2.Cells(1, 39).Value = Sheet2.Cells(1, 39).Value - 1
End Sub
When I click the delete commandbutton5 the result is as shown in figure below
In sheet2
4.png
In sheet 3
5.png
For loop is running only one time after that it got terminated. If I split the 1st for loop and execute it.it is running nice what I meant to do. If I combine another for loop or If I add msgbox at top it misbehaving.
I tried with do while loop also it also doing same thing.
Anyone can help me what is the actual mistake I am doing here and how to get ride of this problem or suggest me the easier method than this it will be helpful. I am beginner to vba programming ,so if your answer is simple and more explanatory manner means very helpful.Thanks in advance
here is the sample file
Bookmarks