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 02, 2019Former Employee
Mucho_Elanstrand Glad that helped. The code can be amended to populate the array before creating the results if you want.
Mucho_Elanstrand
Oct 03, 2019Copper Contributor
I don't know how I'll ever be able to show my gratitude!
Yes that will be great if we can we amend the code to populate the array before creating the results! Can you do that?
"Maybe a message box that asks how many columns in the array", "rows in the array" kind of thing.
- Smitty SmithOct 07, 2019Former Employee
Mucho_Elanstrand Glad I could help
- Mucho_ElanstrandOct 07, 2019Copper Contributor
You, sir are a saint!!! Sorry for the late reply, I was traveling on Friday.
Thank you for all your help with this - very much appreciated.
- Smitty SmithOct 03, 2019Former Employee
Mucho_Elanstrand Here you go:
Option Explicit Sub BuildAll() Call BuildArray Call ReportArray End Sub Sub BuildArray() Dim i As Long Dim x As Long Dim y As Long Dim ws As Worksheet Set ws = Sheets("Array") 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) With ws .Activate .UsedRange.Offset(1).ClearContents .Range("A2:A" & y + 1).Value = 1 For i = 1 To y .Range(Cells(i, 2), Cells(i, x)).Offset(1).Value = i Next i End With End Sub Sub ReportArray() Dim lr As Long Dim lc As Long Dim i As Long Dim x As Long Dim y As Long Dim ws As Worksheet Dim ws1 As Worksheet Set ws = Sheets("Results") ws.Range("A1:B" & Cells(Rows.Count, "A").End(xlUp).Row).ClearContents Set ws1 = Sheets("Array") lr = ws1.Cells(Rows.Count, "A").End(xlUp).Row - 1 lc = ws1.Cells(1, Columns.Count).End(xlToLeft).Column i = 1 For x = 1 To lc For y = 1 To lr ws.Cells(i, "A").Value = x ws.Cells(i, "B").Value = y i = i + 1 Next y Next x End SubWorkbook is attached as well.