How to automatically update macro after adding a row?

inxcndd

New Member
Joined
Aug 9, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I'm currently creating a sheet for some weekly workouts and I wanted to create a randomize button, using macros, that takes an x amount of exercises from a list and assigns them to their respective place - this way the workouts wouldn't get repetitive over time.
I'm veeery new to VBA coding, so I basically searched on the internet for a code that worked and I found one, but I encountered a couple of issues:
  • I can get it to work for the first section (Legs&Glutes), but when I went to change the code for the "Abs" section I realized that I can't get the exercises to start on row 12 (instead they just replace the first exercises of the "Legs&Glutes" section).
  • I don't know how to make the code automatically change when I add a row. For example, I currently have 9 exercises for the first section but I wanted the code to automatically update after adding a row to assign more exercises (so if I added another row between row 10 and 11, the code would update to assign 10 exercises instead of 9, but also in a way that changes the Abs code to start on row 13).
Here is the code that I'm currently using (first section but they're the same, the columns just change):

VBA Code:
Sub Legs_And_Glutes()
Dim NameColumn As Integer
Dim FirstNameRow As Integer
Dim NewColumn As Integer
Dim NumberOfNames As Integer
Dim lrow As Long
Dim StoredNames As String
Dim Counter As Integer
Dim Name As String


NameColumn = 8 'Change this if your names are not in column A
FirstNameRow = 2 'Change this if your first name does not start in row 2 (1 would be the column header)
NewColumn = 2 'Change this if you want to change the column of 10 names from column E to a different column
NumberOfNames = 9 '10 Names in new list


lrow = Cells(Rows.Count, NameColumn).End(xlUp).Row
Counter = 2
StoredNames = ""


Cells(1, NewColumn) = "Exercises"
Range(Cells(2, NewColumn), Cells(NumberOfNames + 1, NewColumn)).ClearContents


Do Until Cells(NumberOfNames + 1, NewColumn) <> ""
    Name = WorksheetFunction.Index(Range(Cells(FirstNameRow, NameColumn), Cells(lrow, NameColumn)), WorksheetFunction.RandBetween(1, lrow - FirstNameRow + 1))
    If InStr(StoredNames, Name) = 0 Then
        StoredNames = StoredNames & " " & Name
        Cells(Counter, NewColumn) = Name
        Counter = Counter + 1
    End If
Loop




End Sub


And here is my sheet as well.

workout2 (version 1).xlsb
BCDEFGHI
1ExercisesSetsRepsNotesLegs/GlutesAbs
2side leg lifts10each leg2kgsbulgarian split squatbicycle crunches
3kneeling squats10x110 sec holddonkey kicksc curls
4donkey kicks pulse10each legdonkey kicks pulsecrunches
5squats pulses w/leg raise10x1fire hydrantsheel touches
6stand up side kicks10x1fire hydrants kicksleg raises
7bulgarian split squat10each legpulse 3 timesfire hydrants pulsesplank
8fire hydrants pulses10each legfrog bridge pulsesplank side dips
9fire hydrants kicks10each legfrog pumpreverse bicycle peddles
10side box leg lifts10each leghip thrusts/bridgesreverse crunches
11abskneeling squatsrussian twists
12plank10x1lungesscissor extensions
13table top leg extensions10each legside box leg liftstable top leg extensions
14reverse crunches10x1side kicks
15bycicle kicks10each legside leg lifts
16russian twists10x1side squats
17leg raises10x1single leg RDL
18squats
19squats pulses w/leg raise
20stand up side kicks
Monday (2)
 

Attachments

  • excel.png
    excel.png
    94 KB · Views: 10

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,224,818
Messages
6,181,152
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