Results 1 to 3 of 3

problem with for next loop

Threaded View

  1. #1
    Registered User
    Join Date
    02-22-2014
    Location
    india
    MS-Off Ver
    Excel 2010
    Posts
    2

    Unhappy problem with for next loop

    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
    Last edited by ravi007008; 02-22-2014 at 11:59 AM. Reason: code tags added. Read forum rules

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Vlookup problem in a loop with cell property and variable cell problem (long title sry)
    By ExcelsiorLux in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 10:38 AM
  2. [SOLVED] Loop inside a loop problem!
    By questionguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2012, 12:54 PM
  3. Loop problem
    By pushkinpassey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2012, 06:36 AM
  4. 'Loop Until' Problem
    By Iwasiw35 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-08-2011, 07:57 PM
  5. [SOLVED] Problem adding charts using Do-Loop Until loop
    By Chris Bromley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2005, 09:06 AM

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