DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

How to Insert Numbers Using Macros (VBA) in Excel Office 365?

Insert Numbers Using Macros in Excel:

In this article, you will learn the steps to Insert Numbers Using Macros in Excel Office 365 with an example. Let’s jump into this article!! Get an official version of MS Excel from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

Steps to Insert Numbers Using Macros:

  • You need to go to the Developer Tab, choose the Visual Basic option under the Code section.

Select the Visual Basic option
Select the Visual Basic option

  • You have to activate the sheet where you want to insert the numbers.
  • Now, enter the below code.

VBA code for inserting numbers in Excel

Sub InsertNumbers()

Dim maxNumber As Integer

Dim counter As Integer

On Error GoTo Last

maxNumber = InputBox(“Enter the Max Value”, “Generate 1 to n”)

‘It will generate all numbers

For counter = 1 To maxNumber

ActiveCell.Value = counter

‘To Move one cell below

ActiveCell.Offset(1, 0).Activate

Next counter

Last: Exit Sub

End Sub

  • You need to save the code by selecting it. Then close the window.

Enter the source code
Enter the source code

  • Now, you need to select any cell where you want to insert the numbers.
  • Then, on the Developer Tab, select the Macros option in the Code section.

Choose Macros option
Choose Macros option

  • You have to make sure that your macro is selected and click the Run button.

Macro dialog box
Macro dialog box

  • It will open the Generate 1 to n dialog box , you have to give the input of how many numbers to be inserted.
  • You have to click the OK button to get the result.

Generate 1 to n dialog box
Generate 1 to n dialog box

  • After clicking the OK button, you can get the output as shown in the below image.

Result
Result

Conclusion:

In this article, you can understand the steps to Insert Numbers Using Macros in Excel Office 365 with an example. Leave your feedback in the comment section. Thanks for visiting Geek Excel. Keep Learning!!

Top comments (0)