VBA to insert numbers

IGPOD

New Member
Joined
May 16, 2014
Messages
20
Hello, I've been trying to figure out how to insert a predefined set of values into a column but I have been unable to do it myself using activecell or selection or offset. What I'm looking for is the following numbers to be placed in any selected cell and populate from that cell downward:

671
1036
1100
1164
1192

any help would be greatly appreciated, google was no help pertaining to this specific topic, everything I as able to find was from known ranges etc.
 

Attachments

  • example.png
    example.png
    19.7 KB · Views: 6

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Maybe
VBA Code:
Sub InsertNumbers1()
Dim MyArr As Variant
MyArr = Array(671, 1036, 1100, 1164, 1192)
ActiveCell.Resize(5) = Application.Transpose(MyArr)
End Sub

or

VBA Code:
Sub InsertNumbers2()
Dim MyArr As Variant
MyArr = Array(671, 1036, 1100, 1164, 1192)
ActiveCell.Resize(UBound(MyArr) + 1) = Application.Transpose(MyArr)
End Sub
 
Last edited:
Upvote 0
Maybe
VBA Code:
Sub InsertNumbers1()
Dim MyArr As Variant
MyArr = Array(671, 1036, 1100, 1164, 1192)
ActiveCell.Resize(5) = Application.Transpose(MyArr)
End Sub

or

VBA Code:
Sub InsertNumbers2()
Dim MyArr As Variant
MyArr = Array(671, 1036, 1100, 1164, 1192)
ActiveCell.Resize(UBound(MyArr) + 1) = Application.Transpose(MyArr)
End Sub
Thank you, Sir... I learned something new today! I really appreciate your help.
 
Upvote 0
Since you know how many values there are (it is a constant array of values), 5 in this case, you can do this with a single line of code...
VBA Code:
Sub InsertNumbers()
  ActiveCell.Resize(5) = [{671;1036;1100;1164;1192}]
End Sub
 
Upvote 0
I suspect it isn't in reality as there are 12 in the image in the first post ;)
It was 5 for the 5 values in the post (which were the 5 values I used in my example)... I assumed the OP would figure out that if he had 12 values, that he would change the 5 to 12. The point is that whatever the values are, what they are and how many of them there are is known and does not have to be calculated.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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