Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

United States (English) Sign in

Search SQL Server with Bing

Home Library Learn Downloads Troubleshooting Community Forums

Ask a question Search related threads Search forum questions

Quick access

Answered by: Insert Auto Serial Number using VBA


Microsoft Office for Developers > Excel for Developers
13,135 Question
Points
Top 0.5%
Hi All,

Ashidacchi I want to insert auto serial no. in Column A of Sheet2. First row can be ignored as I am using that as
Joined Jun 2007 Header. Serial No will be assigned based on the last row of B Column.For example, If B10 is the last
Ashidacchi's threa… 0 used cell in Sheet2 then Serail number would be 1-9 starting from A2 to A10. I want to achieve this by
3 5 15 Show activity
Sign in clicking on a command button which is present in Sheet1. Also is it possible to have 'All Border' upto
to
vote last cell of B Column? Means, if B12 is the last used cell in Sheet2 then I want to have 'All Border' in
A1:B12.

Thanks.. 

Saturday, May 23, 2015 9:40 AM

Reply | Quote 0 Points


Hobert P

Answers
Here's my sample code.

I hope I don't misunderstand your intention.

0 [VBA sample]
Sign in
to
vote Private Sub CommandButton1_Click()
    Dim LastRow As Integer
    Dim idx As Integer
    Worksheets("Sheet2").Activate
    LastRow = ActiveSheet.Range("B2").End(xlDown).Row
    For idx = 1 To LastRow - 1
        Worksheets("Sheet2").Cells(idx + 1, 1).Value = idx
    Next
End Sub

[image of sheets]

  

  

P.S. I'm a Japanse living in Tokyo. My Excel is Japanese version.

Edited by Ashidacchi Saturday, May 23, 2015 11:13 AM


Marked as answer by Hobert P Monday, May 25, 2015 11:07 AM

Saturday, May 23, 2015 10:59 AM

Reply | Quote Ashidacchi Hokusosha 北窓舎 (Japan) 13,135 Points


You can check below which avoids loop (which may be slow if large data is their) and does the
bordering
Private Sub CommandButton1_Click()

0 Dim rLastCell As Range


Sign in
to Dim i As Long
vote
With Worksheets("Sheet2")
Set rLastCell = .Range("B2").End(xlDown)
.Range("a2").Value = 1
.Range(.Range("a2"), rLastCell.Offset(, -1)).DataSeries _
step:=1, stop:=rLastCell.Row + 1

For i = 7 To 12
.Range(.Range("a2"), rLastCell).Borders(i).LineStyle = xlContinuous
Next i

End With
End Sub

Best Regards,
Asadulla Javed, Kolkata
---------------------------------------------------------------------------------------------
Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it
solves the issue.

Marked as answer by Hobert P Monday, May 25, 2015 11:07 AM

Saturday, May 23, 2015 4:49 PM Answerer

Reply | Quote Asadulla Javed 11,645 Points

Private Sub CommandButton1_Click()

Dim rLastCell As Range


Dim i As Long
0
Sign in
to
Application.ScreenUpdating = False
vote
With Worksheets("Sheet2")
Set rLastCell = .Range("B2").End(xlDown)
.Range("a2").Value = 1
.Range(.Range("a2"), rLastCell.Offset(, -1)).DataSeries _
step:=1, stop:=rLastCell.Row - 1

For i = 7 To 12
.Range(.Range("a1"), _
rLastCell.Offset(, 1)).Borders(i).LineStyle = xlContinuous
Next i

End With

End Sub
It does what you need

Best Regards,
Asadulla Javed, Kolkata
---------------------------------------------------------------------------------------------
Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it
solves the issue.

Marked as answer by Hobert P Monday, May 25, 2015 11:07 AM

Sunday, May 24, 2015 3:25 AM Answerer

Reply | Quote Asadulla Javed 11,645 Points

All replies
Here's my sample code.

I hope I don't misunderstand your intention.

0 [VBA sample]
Sign in
to
vote Private Sub CommandButton1_Click()
    Dim LastRow As Integer
    Dim idx As Integer
    Worksheets("Sheet2").Activate
    LastRow = ActiveSheet.Range("B2").End(xlDown).Row
    For idx = 1 To LastRow - 1
        Worksheets("Sheet2").Cells(idx + 1, 1).Value = idx
    Next
End Sub

[image of sheets]

  

  

P.S. I'm a Japanse living in Tokyo. My Excel is Japanese version.

Edited by Ashidacchi Saturday, May 23, 2015 11:13 AM


Marked as answer by Hobert P Monday, May 25, 2015 11:07 AM

Saturday, May 23, 2015 10:59 AM

Reply | Quote Ashidacchi Hokusosha 北窓舎 (Japan) 13,135 Points


You can check below which avoids loop (which may be slow if large data is their) and does the
bordering
Private Sub CommandButton1_Click()

0 Dim rLastCell As Range


Sign in
to Dim i As Long
vote
With Worksheets("Sheet2")
Set rLastCell = .Range("B2").End(xlDown)
.Range("a2").Value = 1
.Range(.Range("a2"), rLastCell.Offset(, -1)).DataSeries _
step:=1, stop:=rLastCell.Row + 1

For i = 7 To 12
.Range(.Range("a2"), rLastCell).Borders(i).LineStyle = xlContinuous
Next i

End With
End Sub

Best Regards,
Asadulla Javed, Kolkata
---------------------------------------------------------------------------------------------
Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it
solves the issue.

Marked as answer by Hobert P Monday, May 25, 2015 11:07 AM

Saturday, May 23, 2015 4:49 PM Answerer

Reply | Quote Asadulla Javed 11,645 Points

Hi Asadulla,

It's very nice to have your code. It;s working exactly as I mentioned above. Only one question I have
i.e. is it possible to have 'All border' in C Column also? At the time of clicking the button from Sheet
0 I have value in only B Column of Sheet2 But I want to have 'All Border' in A,B,C Column upto last
Sign in used cell of B Column.
to
vote
Thank you.

Saturday, May 23, 2015 5:53 PM

Reply | Quote 0 Points


Hobert P

Private Sub CommandButton1_Click()

Dim rLastCell As Range


Dim i As Long
0
Sign in
to
Application.ScreenUpdating = False
vote
With Worksheets("Sheet2")
Set rLastCell = .Range("B2").End(xlDown)
.Range("a2").Value = 1
.Range(.Range("a2"), rLastCell.Offset(, -1)).DataSeries _
step:=1, stop:=rLastCell.Row - 1

For i = 7 To 12
.Range(.Range("a1"), _
rLastCell.Offset(, 1)).Borders(i).LineStyle = xlContinuous
Next i

End With

End Sub
It does what you need

Best Regards,
Asadulla Javed, Kolkata
---------------------------------------------------------------------------------------------
Please do not forget to click “Vote as Helpful” if any post helps you and "Mark as Answer”if it
solves the issue.

Marked as answer by Hobert P Monday, May 25, 2015 11:07 AM

Sunday, May 24, 2015 3:25 AM Answerer

Reply | Quote Asadulla Javed 11,645 Points

© 2018 Microsoft. All rights reserved.


Terms of Use | Trademarks | Privacy Statement | Site Feedback

You might also like