VBA Autofill

joshrescue62

New Member
Joined
Sep 18, 2013
Messages
8
Hi all,

This is probably something pretty easy, I'm just not at all familiar with vba.

I have a grade book that I am using for my class this semester. I have entered in it my 15 students names, with each of there 5 test scores in columns (B3:F3) and class average in column (G3). I would like to create a macro that will insert a column in between F3 (the 5th test) and the Class average (G3), since there will be other tests I would like to input into this spreadsheet. I would like the heading to autofill from cell F3 (Test 5), and I would like it to say (Test 6) in G3. I would also like this macro to insert holding 0's in the new column (G) inserted for rows 4:19.

If anyone could help me out, it would definitely be very appreciated.

I hope this isn't too complex :rolleyes:. If you need more information or pictures, let me know.

Thanks,
Josh
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Create a new copy of your workbook and try this out:
Code:
Sub Add_Test()
    'vars
        Dim i As Integer, RefCol As Integer, ColLetter As String
    With Range("A3").End(xlToRight)
        RefCol = .Column
        If .Column > 26 Then
            ColLetter = Chr(Int((.Column - 1) / 26) + 64) & Chr(((.Column - 1) Mod 26) + 65)
        Else
            ColLetter = Chr(.Column + 64)
        End If
    'insert new column
        .EntireColumn.Insert
    'test #
        .Offset(0, -1).Value = "Test " & Right(Range("A3").End(xlToRight), 1) + 1
    End With
    'adjust formulas and place 0s
        For i = 4 To 19
            Cells(i, RefCol + 1).Formula = _
                "=AVERAGE($B" & i & ":" & ColLetter & i & ")"
            Cells(i, RefCol).Value = 0
        Next i
End Sub
Adds the next test in row 3, adjusts all the average formulas from rows 4 to 19.
 
Upvote 0
Disregard the previous post, I wrote the test number code incorrectly. This works correctly:
Code:
Sub Add_Test()
    'vars
        Dim i As Integer, RefCol As Integer, ColLetter As String
    With Range("A3").End(xlToRight)
        RefCol = .Column
        If .Column > 26 Then
            ColLetter = Chr(Int((.Column - 1) / 26) + 64) & Chr(((.Column - 1) Mod 26) + 65)
        Else
            ColLetter = Chr(.Column + 64)
        End If
    'insert new column
        .EntireColumn.Insert
    'test #
        .Offset(0, -1).Value = "Test " & Replace(Range("A3").End(xlToRight), "Test ", "") + 1
    End With
    'adjust formulas and place 0s
        For i = 4 To 19
            Cells(i, RefCol + 1).Formula = _
                "=AVERAGE($B" & i & ":" & ColLetter & i & ")"
            Cells(i, RefCol).Value = 0
        Next i
End Sub
 
Upvote 0
hey thanks for your reply,

however when I run this code, it inserts 3 columns with headings Test 5, Test 6, and Test 7, in the incorrect place. It inserts them after column J, and I would like them to be inserted before column G. The 0's inserted are perfect, however I only need this macro to insert 1 column with heading Test 6, with 0's as placeholders.

Thanks so much for your help,

Josh
 
Upvote 0
This is what I get when I run the code:
Before:

Name​

Test 1​

Test 2​

Test 3​

Test 4​

Test 5​

Average​

Johnny​

41​

66​

36​

26​

91​

52​

Sally​

81​

86​

50​

35​

46​

59.6​

<tbody>
</tbody>
...
After:

Name​

Test 1​

Test 2​

Test 3​

Test 4​

Test 5​

Test 6​

Average​

Johnny​

41​

66​

36​

26​

91​

0

43.33333​

Sally​

81​

86​

50​

35​

46​

0
49.66667​

<tbody>
</tbody>

...
 
Upvote 0
Extra columns: I have no idea how or why the code would insert more than one row, there's only one insert in there. Can you paste the code you've got in the module?

Wrong place: That would happen if there's data in the cells of row 3 immediately following the Average cell. IE: Average is in cell G3, cell H3 contains "Favorite Color". Is that the case?
 
Last edited:
Upvote 0
Hey,

Ive attached two copies of my spreadsheet, one that is the original, and the other that includes yellow highlighting of exactly what I would like this macro to do. I'm sorry if this has been confusing

original- js | Stuff to Buy
what i want macro to do -js | Stuff to Buy

Sorry about the Weird hyperlink names, just check them out they are pictures on a random pintrest acct, lol

Josh
 
Upvote 0
I've altered the code so it specifically finds the cell containing "Average" and then adds the column in front of that, give this a try:
Code:
Sub Add_Test()
    'vars
        Dim i As Integer, RefCol As Integer, ColLetter As String
        RefCol = Range(Cells(3, 1), Cells(3, Range("A3").End(xlToRight).Column)).Find(What:="Average", LookIn:=xlValues, LookAt:=xlWhole).Column
        If RefCol > 26 Then
            ColLetter = Chr(Int((RefCol - 1) / 26) + 64) & Chr(((RefCol - 1) Mod 26) + 65)
        Else
            ColLetter = Chr(RefCol + 64)
        End If
    'insert new column
        Cells(3, RefCol).EntireColumn.Insert
    'test #
        Cells(3, RefCol).Value = "Test " & Replace(Cells(3, RefCol - 1), "Test ", "") + 1
    'adjust formulas and place 0s
        For i = 4 To 19
            Cells(i, RefCol + 1).Formula = _
                "=AVERAGE($B" & i & ":" & ColLetter & i & ")"
            Cells(i, RefCol).Value = 0
        Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,758
Messages
6,126,709
Members
449,331
Latest member
smckenzie2016

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