UrbanPro
true

Learn Microsoft Excel Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

VBA Tip: Repeating Emp Names N number of times using 4 different loops

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
   ThisWorkbook.Sheets("Task").Range("L2:L10000").ClearContents
   source_lr = ThisWorkbook.Sheets("Task").Cells(Rows.Count, "J").End(xlUp).Row
   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
   ThisWorkbook.Sheets("Task").Range("L2:L10000").ClearContents
   source_lr = ThisWorkbook.Sheets("Task").Cells(Rows.Count, "J").End(xlUp).Row

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
   ThisWorkbook.Sheets("Task").Range("L2:L10000").ClearContents
   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
   ThisWorkbook.Sheets("Task").Range("L2:L10000").ClearContents
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

Please Enter a comment

Submit

Other Lessons for You

What is a VBA Module?
VBA code is stored and typed in the VBA Editor in what are called modules As stated on the VBA Editor page, a collection of modules is what is called a VBA project Every major Microsoft Office product...

TODAY WE LEARN MAX FORMULA, HOW TO USE IT.
1 A B 2 NAME SERIES 3 Vinita 50 4 Vimlesh 34 5 Karina 73 6 Kartrina 18 7 Salman 65 8 Gold 48 =MAX(C3:C8) ANSWER- 73 =MAX(NUMBER 1, NUMBER 2)

What is Big Data and Why Do Organizations Need It?
Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. But it’s not the amount of data that’s...

Clipboard Task Pane
When you copy a cell's contents, formula or format, that information goes into the clipboard. The clipboard will hold the information until you decide to paste it somewhere else on the spreadsheet,...

Presentation Perfect Reports
Presentation Perfect Reports. Why Presentable, neatly crafted Reports makes a job more perfect and easy for Business Analyst who is going make decisions out of the information structured in reports. Many...

Looking for Microsoft Excel Training classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Microsoft Excel Training Classes?

The best tutors for Microsoft Excel Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Microsoft Excel Training with the Best Tutors

The best Tutors for Microsoft Excel Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 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 7.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