VBA to Repeat Task a Specific Number of Times

happyhungarian

Active Member
Joined
Jul 19, 2011
Messages
254
Office Version
  1. 365
Platform
  1. Windows
Hi there! So I hope I can explain what i'm trying to do clearly. I have an input table where users input hours for a labor category (column) and a task (row). The user also has the ability to insert more categories (columns) and more tasks (rows). In order for the information to become useful I need to convert this grid pattern into a list. The way I am currently doing that is by manually calculating the number of possible combinations of categories and tasks (for example, if i have 3 labor categories and 4 tasks I have a total possible combination of 12 (3x4)). Essentially I manually paste my total list of categories into the table 4 separate times. Then I let the table do it's magic and it's calculations using an INDEX MATCH to pull the information from the user input section. What I would like to do, however, is have excel realize the number of times it needs to paste the list of categories into the table by counting the number of tasks itself (or referencing a cell that tells the VBA how many times it needs to do that).
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Maybe I can simplify... Is there a code that could look at the value that's in a cell (let's say A1) and based on the value in that cell the VBA will run a certain macro that number of time. Example, if cell A1 says 5 it would run the following string of code 5 times
 
Upvote 0
Maybe I can simplify... Is there a code that could look at the value that's in a cell (let's say A1) and based on the value in that cell the VBA will run a certain macro that number of time. Example, if cell A1 says 5 it would run the following string of code 5 times

I think that this should do what you want.


Code:
Option Explicit


Sub Sub1()
    Dim numberoftimestorun As Integer
    Dim i As Integer
    
    
    numberoftimestorun = ActiveSheet.Range("A1").Value
    
    For i = 1 To numberoftimestorun
        
        'INSERT CODE HERE
        
    Next i




End Sub
 
Upvote 0
Maybe I can simplify... Is there a code that could look at the value that's in a cell (let's say A1) and based on the value in that cell the VBA will run a certain macro that number of time. Example, if cell A1 says 5 it would run the following string of code 5 times

Code:
Sub dynLoop()
    For i = 1 To Sheets("Sheet1").Cells(1, 1)
        'Run Macro
    Next i
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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