Not getting desired results when moving a VBA array to a spread sheet

JLoewen

New Member
Joined
Jun 20, 2018
Messages
29
I have a VBA program for importing all the file names in a target folder into a spreadsheet for manipulation. I am using this for managing architectural specifications. The program appears to work (monitoring variables) until the last step where it is to pass the data to the spreadsheet. At that point, it places the 1st cell of the array into the spreadsheet the once for each cell. Results shown below along with code.

VBA Code:
Public Sub GetFileNames()

Dim Result As Variant
Dim i As Integer
Dim MyFile, MyFso, MyFolder, MyFiles As Object
Dim FolderPath, FileExt As String
Dim outputarray As Variant

FolderPath = Worksheets("Sheet1").Range("A1").Value

Set MyFso = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFso.GetFolder(FolderPath)
Set MyFiles = MyFolder.Files



ReDim Result(1 To MyFiles.Count)

i = 1
For Each MyFile In MyFiles
Result(i) = MyFile.Name

i = i + 1

Next MyFile

i = i - 1 

Worksheets("Sheet1").Range(Cells(3, 1), Cells(3 + i, 1)).Value = Result

End Sub

1630524129578.png
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
try
VBA Code:
With Worksheets("Sheet1")
   .Range(.Cells(3, 1), .Cells(3 + i, 1)).Value = Application.Transpose(Result)
End With
 
Upvote 0
A safer option would be
VBA Code:
ReDim Result(1 To MyFiles.Count, 1 To 1)

i = 1
For Each MyFile In MyFiles
Result(i, 1) = MyFile.Name

i = i + 1

Next MyFile

i = i - 1

With Worksheets("Sheet1")
   .Range(.Cells(3, 1), .Cells(3 + i, 1)).Value = Result
End With
End Sub
 
Upvote 0
Solution
No luck with either of those. For a 1-d array does VBA do those strictly horizontally instead of vertically? Would it work to effectively force a 2-d array? Something that is (n x 1) in size instead of just (n) in size?
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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