Auto Row Numbering using VBA

leviackerman13

New Member
Joined
Jan 2, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi. I have a large data set where i need to add a word "Count" and then number the rows. The data set looks like the image below. What i want to do is in the red font. Hoping for your help as i am new to VBA.
1704174983519.png
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Welcome to the MrExcel board!

See if this would do.

VBA Code:
Sub Row_Numbering()
  With Range("B2:B" & Range("C" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(RC[1]="""","""",IF(RC[1]=""Name"",""Count"",N(R[-1]C)+1))"
    .Value = .Value
  End With
End Sub
 
Upvote 1
Welcome to the MrExcel board!

See if this would do.

VBA Code:
Sub Row_Numbering()
  With Range("B2:B" & Range("C" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(RC[1]="""","""",IF(RC[1]=""Name"",""Count"",N(R[-1]C)+1))"
    .Value = .Value
  End With
End Sub
Thanks a lot. This has worked for me.
 
Upvote 0
Welcome to the MrExcel board!

See if this would do.

VBA Code:
Sub Row_Numbering()
  With Range("B2:B" & Range("C" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(RC[1]="""","""",IF(RC[1]=""Name"",""Count"",N(R[-1]C)+1))"
    .Value = .Value
  End With
End Sub
Hi. I would like to ask for your help again. I am trying to auto sum the grades of every student and output the sum in the last row. What i want to do is in the red font.
1704178354011.png
 
Upvote 0
Looks like you have changed columns. So this should do both jobs if student names are in column B.

VBA Code:
Sub Row_Numbers_and_Totals()
  Dim rA As Range
  
  With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(RC[1]="""","""",IF(RC[1]=""Name"",""Count"",N(R[-1]C)+1))"
    .Value = .Value
    For Each rA In Range(.Address).SpecialCells(xlConstants, xlNumbers).Areas
      rA.Offset(rA.Rows.Count, 2).Resize(1, 2).FormulaR1C1 = "=SUM(R" & rA.Row & "C:R[-1]C)"
    Next rA
  End With
End Sub
 
Upvote 0
Solution
Looks like you have changed columns. So this should do both jobs if student names are in column B.

VBA Code:
Sub Row_Numbers_and_Totals()
  Dim rA As Range
 
  With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(RC[1]="""","""",IF(RC[1]=""Name"",""Count"",N(R[-1]C)+1))"
    .Value = .Value
    For Each rA In Range(.Address).SpecialCells(xlConstants, xlNumbers).Areas
      rA.Offset(rA.Rows.Count, 2).Resize(1, 2).FormulaR1C1 = "=SUM(R" & rA.Row & "C:R[-1]C)"
    Next rA
  End With
End Sub
I have another data where I just need to sum columns C and D. What lines should I delete?
 
Upvote 0
Looks like you have changed columns. So this should do both jobs if student names are in column B.

VBA Code:
Sub Row_Numbers_and_Totals()
  Dim rA As Range
 
  With Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=IF(RC[1]="""","""",IF(RC[1]=""Name"",""Count"",N(R[-1]C)+1))"
    .Value = .Value
    For Each rA In Range(.Address).SpecialCells(xlConstants, xlNumbers).Areas
      rA.Offset(rA.Rows.Count, 2).Resize(1, 2).FormulaR1C1 = "=SUM(R" & rA.Row & "C:R[-1]C)"
    Next rA
  End With
End Sub
I have another data where I just need to sum columns C and D. What lines should I delete?
Don't mind this. I was able to work this out. My last task is to add a border from A2 to D5 and A8 to D13 and etc. Can you also help me with this? Below is what I wanted to do.
1704270528575.png
 
Upvote 0
Assuming column A does not contain formulas, try ..

VBA Code:
Sub Add_Borders()
  Dim rA As Range
  
  For Each rA In Columns("A").SpecialCells(xlConstants).Areas
    With rA.Resize(, 4)
      .BorderAround xlContinuous
      .Borders(xlInsideVertical).LineStyle = xlContinuous
      .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
  Next rA
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,247
Messages
6,123,857
Members
449,129
Latest member
krishnamadison

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