Forum Discussion
Mucho_Elanstrand
Oct 01, 2019Copper Contributor
VBA for autofill
Hi Wizards, Let me first preface this by saying; I know nothing about coding. But in my experience, when you feel there can be a more efficient way to do something, there usually is one. I a...
Smitty Smith
Oct 01, 2019Former Employee
Mucho_Elanstrand How's this:
Sub BuildArray()
Dim i As Long
Dim x As Long
Dim y As Long
x = Application.InputBox("How many columns do you want to fill?", "Column Count", 1, Type:=1)
y = Application.InputBox("How many rows do you want to fill?", "Row Count", 1, Type:=1)
Range("A2:A16").Value = 1
For i = 1 To y
Range(Cells(i, 2), Cells(i, x)).Offset(1).Value = i
Next i
End Sub
Mucho_Elanstrand
Oct 02, 2019Copper Contributor
Hi Smitty,
The code works! However I need a little modification. As it stands, it populates rows 2 - 16 of column A and B with the numbers 1-15 as in the first screenshot (A). What I need however, is what it looks like in the second screenshot (B). How would I accomplish that? 🙂 Thank you, sensei!
AB
- Smitty SmithOct 02, 2019Former Employee
Mucho_Elanstrand With the code I posted, if you were to enter 10 (columns) & 15 (rows), you'd get this:
Isn't that what you were after?
Also, you might want to add this line of code to clear out any previous entries:
ActiveSheet.UsedRange.Offset(1).ClearContents