insert number automatically vba

rakesh seebaruth

Active Member
Joined
Oct 6, 2011
Messages
303
i refer to the above columns

A B C D E
DATE DETAILS REFERENCE MONTH YEAR
1
2
3
4
6
I have the following codes which adds the date,month, year in their respective cells

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A6")) Is Nothing Then
If Target.Cells.Count > 1 Then Exit Sub
Target.Value = Format(Date, "dd.mm.yyyy")
Target.Offset(, 3).Value = Month(Date)
Target.Offset(, 4).Value = Year(Date)
End If
End Sub



i want <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">vba</acronym> to automatically add 001 in cell c1,002 in cell c2 ,003 in cell 3 and so on

thanks for your help

regards

rakesh
 

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.
Code:
Sub AddValues()

ActiveSheet.Range("C1").Value = "001"
ActiveSheet.Range("C2").Value = "002"
ActiveSheet.Range("C3").Value = "003"
ActiveSheet.Range("C4").Value = "004"

End Sub


Or if you would rather it loops through a bunch of cells:

Code:
Sub AddValuesLoop()

For i = 1 to 10

           ActiveSheet.Range("C" & i).Value = i

Next i

End Sub
 
Upvote 0
Try this:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Not Intersect(Target, Range("A1:A6")) Is Nothing Then
 If Target.Cells.Count > 1 Then Exit Sub
 Dim ans As Long
 ans = Target.Row
 Target.Value = Format(Date, "dd.mm.yyyy")
 Target.Offset(, 3).Value = Month(Date)
 Target.Offset(, 4).Value = Year(Date)
 Target.Offset(, 2).NumberFormat = "000"
 Target.Offset(, 2).Value = ans
 End If
 End Sub
 
Upvote 0

Forum statistics

Threads
1,215,540
Messages
6,125,405
Members
449,223
Latest member
Narrian

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