+ Reply to Thread
Results 1 to 10 of 10

Looping in Macro help.

Hybrid View

  1. #1
    Registered User
    Join Date
    05-09-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    26

    Looping in Macro help.

    I am trying to write a macro that will start on the the cell and column entered from user input and loop number of times entered by user. When you enter the starting point it starts on the next row not the initial cell entered from the prompts.

    Example if you enter everything to start on B4 it actually starts on B5 then loops the correct number of times.

    Sub pivot_extract()
    '
    ' pivot_extract Macro
    '
    
    '
    Dim I
    Col = InputBox("Column to start on?")
    Cell = InputBox("Cell to start on?")
    L = InputBox("How many times to repeat?")
    For I = 1 To L
    
        Application.ScreenUpdating = False
        Cell = Cell + 1
        Range(Col & Cell).Select
        Selection.ShowDetail = True
        Sheets("ILCC Pivots").Select
    
    Next I
    End Sub
    That is the code I have thus far. But can't seem to enter the correct code to get it to repeat. Any help would be greatly appreciated.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Looping in Macro help.

    L = Val(InputBox("How many times to repeat?"))
    inputbox returns a string
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    05-09-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Looping in Macro help.

    Quote Originally Posted by patel45 View Post
    L = Val(InputBox("How many times to repeat?"))
    inputbox returns a string
    not sure what you are asking but with i change that line. it still does the same. starts on the cell after the one entered in the input.

  4. #4
    Forum Contributor
    Join Date
    04-25-2013
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: Looping in Macro help.

    Try this:

    Sub pivot_extract()
    Dim i, l As Integer
    Dim col, cell As String
    
    col = InputBox("Column to start on?")
    cell = InputBox("Cell to start on?")
    l = InputBox("How many times to repeat?")
    Range(col & cell).Offset(1, 0).Select
    
    Application.ScreenUpdating = False
    
    i = 1
    Do Until i = l
        Selection.ShowDetail = True
        Sheets("ILCC Pivots").Select
        activecell.offset(1,0).select
        i = i + 1
    Loop
    
    Application.ScreenUpdating = True
    
    End Sub
    edit: oops, saw a mistake
    Last edited by djdjdj; 10-22-2013 at 09:37 AM. Reason: Mistake

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Looping in Macro help.

    what do you mean for "Cell to start on?" ? you asked for column, then you need Row.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Looping in Macro help.

    Hi, mike760534211,

    maybe use Application.InputBox where you may state if you want a string or a number returned. Here is a sample wihout error handling (in case that cancel was pressed):
    Sub pivot_extract()
    '
    ' pivot_extract Macro
    '
    
    '
    Dim I As Long, col As String, rw As Long, L As Long
    col = InputBox("Column to start on?")
    rw = InputBox("Cell to start on?")
    L = InputBox("How many times to repeat?")
    Application.ScreenUpdating = False
    For I = 1 To L
      With Sheets("ILCC Pivots")
        .Range(col & rw).ShowDetail = True
        rw = rw + 1
      End With
    Next I
    Application.ScreenUpdating = True
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Looping in Macro help.

    I think you need to change:

    Sub pivot_extract()
    '
    ' pivot_extract Macro
    '
    
    '
    Dim I
    Col = InputBox("Column to start on?")
    Cell = InputBox("Cell to start on?")
    L = InputBox("How many times to repeat?")
    For I = 1 To L
    
        Application.ScreenUpdating = False
        Cell = Cell + 1
        Range(Col & Cell).Select
        Selection.ShowDetail = True
        Sheets("ILCC Pivots").Select
    
    Next I
    End Sub
    To this:

    Sub pivot_extract()
    '
    ' pivot_extract Macro
    '
    
    '
    Dim I
    Col = InputBox("Column to start on?")
    Cell = InputBox("Cell to start on?")
    L = InputBox("How many times to repeat?")
    For I = 1 To L
    
        Application.ScreenUpdating = False
        Range(Col & Cell).Select
        Selection.ShowDetail = True
        Sheets("ILCC Pivots").Select
        Cell = Cell + 1
    
    Next I
    End Sub

  8. #8
    Registered User
    Join Date
    05-09-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Looping in Macro help.

    Quote Originally Posted by JOHN H. DAVIS View Post
    I think you need to change:

    Sub pivot_extract()
    '
    ' pivot_extract Macro
    '
    
    '
    Dim I
    Col = InputBox("Column to start on?")
    Cell = InputBox("Cell to start on?")
    L = InputBox("How many times to repeat?")
    For I = 1 To L
    
        Application.ScreenUpdating = False
        Cell = Cell + 1
        Range(Col & Cell).Select
        Selection.ShowDetail = True
        Sheets("ILCC Pivots").Select
    
    Next I
    End Sub
    To this:

    Sub pivot_extract()
    '
    ' pivot_extract Macro
    '
    
    '
    Dim I
    Col = InputBox("Column to start on?")
    Cell = InputBox("Cell to start on?")
    L = InputBox("How many times to repeat?")
    For I = 1 To L
    
        Application.ScreenUpdating = False
        Range(Col & Cell).Select
        Selection.ShowDetail = True
        Sheets("ILCC Pivots").Select
        Cell = Cell + 1
    
    Next I
    End Sub
    That works. thank you. I had the + 1 in the wrong place.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Looping in Macro help.

    You're welcome. Glad to help out, and thanks for the feedback. In accordance with Forum Rule No. 9 please mark this thread as solved.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Looping in Macro help.

    Hi, mike760534211,

    maybe have a look at http://www.excelforum.com/excel-prog...ml#post3447339 and the code in there - same advice, isnīt it?

    And please do not quote full posts when answering (Forum Rule #12).

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro Looping help???
    By pcm68827 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2013, 09:30 AM
  2. A looping macro
    By Aland2929 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-21-2011, 06:17 AM
  3. Help with Macro looping
    By KevinThomas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-02-2010, 05:37 AM
  4. Looping macro
    By DaSuPeR in forum Excel Programming / VBA / Macros
    Replies: 44
    Last Post: 06-02-2008, 01:25 PM
  5. Macro:Looping
    By cart0250 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2006, 04:47 PM

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