 true

Find the best tutors and institutes for Microsoft Excel Training

Find Best Microsoft Excel Training classes

No matching category found.

No matching Locality found.

Outside India?

Search for topics

# VBA Tip: Repeating Emp Names N number of times using 4 different loops Naga Satyanarayana Anantapalli
01/02/2017 0 0

Hi All,
This is a requirement from one of our student and here we are posting the question along with solution (this can be achieved using 4 different loops). In Column “J” there are few employee names which need to be repeated for “n” (this “n” is in cell “O1”) number of times in column “L”. Check the below screenshot for understanding the requirement. Above requirement can be done using 4 different VBA loops. Here you go

Solving the above task using 1. For Next Loop

Sub EmpName_Recurring1()
Dim emp_loop As Integer
Dim source_lr As Integer
Dim destination_lr As Integer
For emp_loop = 1 To source_lr
destination_lr = ThisWorkbook.Sheets("Task").Cells(Rows.Count, "L").End(xlUp).Row + 1
Range(Cells(destination_lr, "L"), Cells(destination_lr + Range("Repeat_Count").Value - 1, "L")).Value =          Cells(emp_loop + 1, "J").Value
Next emp_loop
MsgBox "Task completed using For Next loop"

End Sub

Solving the above task using 2. For Each Loop

Sub EmpName_Recurring2()
Dim rng As Range
Dim source_lr As Integer
Dim destination_lr As Integer

For Each rng In Sheets("Task").Range("J2:J" & source_lr)
destination_lr = ThisWorkbook.Sheets("Task").Cells(Rows.Count, "L").End(xlUp).Row + 1
Range(Cells(destination_lr, "L"), Cells(destination_lr + Range("Repeat_Count").Value - 1, "L")).Value =          rng.Value
Next rng
MsgBox "Task completed using For Each loop"

End Sub

Solving the above task using 3. Do While Loop

Sub EmpName_Recurring3()
Dim rng As Range
Dim row_increment As Integer
Dim destination_lr As Integer
Do While Cells(row_increment + 2, "J").Value <> ""
destination_lr = ThisWorkbook.Sheets("Task").Cells(Rows.Count, "L").End(xlUp).Row + 1
Range(Cells(destination_lr, "L"), Cells(destination_lr + Range("Repeat_Count").Value - 1, "L")).Value =          Cells(row_increment + 2, "J").Value
row_increment = row_increment + 1
Loop
MsgBox "Task completed using Do While loop"

End Sub

Solving the above task using 4. Do Until Loop

Sub EmpName_Recurring4()
Dim rng As Range
Dim row_increment As Integer
Dim destination_lr As Integer
Do Until Cells(row_increment + 2, "J").Value = ""
destination_lr = ThisWorkbook.Sheets("Task").Cells(Rows.Count, "L").End(xlUp).Row + 1
Range(Cells(destination_lr, "L"), Cells(destination_lr + Range("Repeat_Count").Value - 1, "L")).Value =         Cells(row_increment + 2, "J").Value
row_increment = row_increment + 1
Loop
MsgBox "Task completed using Do Until loop"

End Sub

0 Dislike
Follow 0  ## Other Lessons for You

How to add bullets Point in MS-EXCEL
There is no direct option to add bullets in EXCEL. So we can create the bullets by the following steps: 1. Select the range where you want to apply the bullets.2. go to formate cell ==> Custom Formate.3.... Data Analysis with MS Excel Filter - Top Performers, Bottom Performers, Above Average Performers, Below Average Performers!
In the following, You would find lesser-known, unexplored yet Powerful Features of Filter. Please refer to sample dataset below: To do Data Analysis:Step 1) Apply Filter on the dataset.Step 2) In Sales... Tips - How to put PivotTable Field List back at its Original Position?
Have You ever struggled to put PivotTable Field List back at its Original Position?*Original Position - right-side of the worksheet, as highlighted in the following picture: If Your answer is Yes, You... ### Looking for Microsoft Excel Training classes?

Find best Microsoft Excel Training classes in your locality on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

### Looking for Microsoft Excel Training Classes?

Find best tutors for Microsoft Excel Training Classes by posting a requirement.

• Post a learning requirement
• Get customized responses
• Compare and select the best  ### Looking for Microsoft Excel Training Classes?

Find best Microsoft Excel Training Classes in your locality on UrbanPro UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 25 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 6.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more