I am making an Excel sheet for keeping track of courses and its participants.
The format is as such:
01. Place | Time | Date | Slots
02. SO.com Now Now 5
03. SE.com Soon Soon 10
And etc.
This list is sent to me with anywhere from 10 to 50 courses at a time, and I want to keep track of the participants for each course in the same file.
So ideally, I want to run a macro that, under the course for SO.com, creates 5 (See Slots) new rows, and then replaces the value 5 with a =COUNTBLANK for the range that was just created, so that the "Slots" value will now show how many free slots there are instead of the total number of slots. Bonus points if anyone can point me to how I can make the macro group the created selection as well.
The macro should loop through the entire worksheet and do the same operation for every course.
The result should look like this:
01. Place | Time | Date | Slots
02. SO.com Now Now 5
03. <empty>
04. <empty>
05. <empty>
06. <empty>
07. <empty>
08. SE.com Soon Soon 10
09. <empty>
10. <empty>
...
Entering the participants into the Excel field is done through copypasta, as I have a system for mass exporting this information from a different program.
I'm very new at VBA, but below is my initial attempt at starting to construct this code. I cut some code from other parts of the web, did some lookups to MSDN, and guesstimated the rest, and not very surprisingly, it doesn't quite work yet. I get an object required error on the line starting with "Set cellCount = Worksheets [...]" and I don't understand why.
Any input on other parts of the process (if you see that my code is stillborn regardless of the object error, for example) is also appreciated.
Sub insertRowsCourseSpace()
Dim i&
Dim cellCount As Integer
Dim a As Integer
'Locate the column to look for course space values
Dim col_n As Long
For f = 1 To NumCols
If Cells(2, f).Value = "Slots" Then col_n = f 'Finding the cell with the given string sets the column number
Next
'If cell value is numerical, create rows equal to value
For i = 1 To NumRows
If IsNumeric(Worksheets(1).Range(col_n & i).Value) = True Then
Set cellCount = Worksheets(1).Range(col_n & i).Value
Set Worksheets(1).Range(col_n & i).Value = "=COUNTBLANK(ActiveCell.Offset(1):ActiveCell(Offset(1 + cellCount))"
For j = 1 To cellCount
ActiveCell.Offset(j).EntireRow.Insert
Next j
Next i
End Sub
EDIT:
OK, new attempt:
Using this workbook: http://s000.tinyupload.com/?file_id=02770147469124312893
Sub insertRowsCourseSlots()
Dim i&
Dim cellCount As Integer
Dim cellValue As Integer
Dim a As String
Dim b As String
'Locate correct column to look for course slots
'Dim col_n As Long
'
' For f = 1 To 15 'Course slots won't be located further out than 15 columns, arbitrary value
' If Cells(2, f).Value = "Antall kursplass" Then col_n = f
'Next
'If cell value is numerical, insert number of rows equal to the cell value
For i = 3 To 400 '400 = Arbitrary number
If IsNumeric(Sheets("Sheet1").Cells(2, i).Value) = True Then
cellValue = Sheets("Sheet1").Cells(2, i).Value
cellCount = cellValue
a = ActiveCell.Offset(1)
b = ActiveCell.Offset(1) + CStr(cellCount)
Set Sheets("Sheet1").Cells(2, i).Value = "=COUNTBLANK(a:b)"
For j = 1 To cellCount
ActiveCell.Offset(j).EntireRow.Insert
Next j
End If
Next i
End Sub
This gives me Runtime error '9', subscript out of range, on the line Set Sheets("Sheet1").Cells(2, i).Value = "=COUNTBLANK(a:b)"