+ Reply to Thread
Results 1 to 6 of 6

Call a macro numerous times

Hybrid View

  1. #1
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Call a macro numerous times

    Hi, at the moment I have a macro that is assigned to a button. When clicked, it creates a newline, create and copy a column in another sheet. This code works fine

    Sub Insert_Site()
    
    Application.ScreenUpdating = False
    
    Current_row = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
    ActiveSheet.Range("C" & Current_row).Select
    current_num = Range("S" & Current_row + 1).Value
    ActiveCell.EntireRow.Insert
    Range("S" & Current_row + 2).Value = current_num + 1
    Application.ScreenUpdating = True
    
    
    
    ActiveWorkbook.Sheets(" WAN_OPT_Site_Specification").Activate
    
    
        Dim LastCol As Long
    
       Application.ScreenUpdating = False
       Columns(5).EntireColumn.Hidden = False
    
       'Find last available column
       LastCol = Cells(1, "V").End(xlToLeft).Column + 1
       Columns(5).Copy
       Cells(1, LastCol).Insert xlToLeft
       Cells(1, LastCol).PasteSpecial xlValue
       
       Columns(5).EntireColumn.Hidden = True
    
       Application.CutCopyMode = False
       Application.ScreenUpdating = True
    
    
    ActiveWorkbook.Sheets("Sites_WAN_Carriage").Activate
    
    End Sub
    What I now want to do is make a cell called "Total Number of Sites" and allow user to input data into "D3" manualy. eg if 3 sites, user input 3 in D3 so D3 = 3.

    Since total number of sites = 3, Instead of clicking that button I made 3 times, I want to call the macro 3 times using a nother button.

    However it doesn't work.
    This is what I used

    Sub Generate_Sites()
    
    Target.Address = "$D$3"
      
        Dim rng As Range, cnt As Integer, x
        
        Set rng = Range("D3")
        cnt = rng
        
        For x = 1 To cnt
            Call Insert_Site
        Next x
     
    End Sub
    Any help is appriciated

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: Call a macro numerous times

    try deleting the target.address line in generate_sites
    you will need to have a sheets("page name").select before your set rng

    Cheers

    Jmac1947

    1. Please consider clicking on the * Add Reputation if you think this post has helped you
    2. Mark your thread as SOLVED when question is resolved

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

    Re: Call a macro numerous times

    Maybe:

    Sub tharindudk()
        Dim rng As Range, cnt As Integer, x
        Set rng = Range("D3")
        cnt = rng
    Do Until cnt = 0
            Call Insert_Site
            cnt = cnt - 1
    Loop
    End Sub

  4. #4
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Call a macro numerous times

    Hi, It didn't work. I have attached a template. Any help is much Appriciated. Thank you
    Attached Files Attached Files
    Last edited by tharindudk; 01-27-2014 at 06:03 PM.

  5. #5
    Registered User
    Join Date
    12-04-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2007
    Posts
    92

    Re: Call a macro numerous times

    All good. I managed to solve it. Thankyou

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

    Re: Call a macro numerous times

    You're welcome. Glad you got a solution.

+ 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. Calling macro 10 times, add header list to macro call only once
    By capson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-05-2013, 09:42 AM
  2. Incorrect calculation until 'Calculate Sheet' is clicked numerous times
    By LoneWolf3574 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-14-2013, 01:28 AM
  3. [SOLVED] Call "another Macro" multiple times
    By Simon.Ward in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-05-2013, 08:03 AM
  4. VBA to print numerous times an excel sheet
    By Gerbs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2012, 08:56 AM
  5. Create a loop to call macro a specified number of times
    By magito in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-09-2011, 09:18 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