Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:47:01+00:00 2026-06-15T13:47:01+00:00

I have never coded in VBA but I’m trying to transpose some knowledge from

  • 0

I have never coded in VBA but I’m trying to transpose some knowledge from obj-c into this.

-I want to open a file (about 200 files in a folder)

-look through the range of cells in each file

-then find everything (words) before the first comma in each cell (cells in this range have three commas)

-add the value of each cell to an array

(scan the remaining files and do the same)

-take results array and paste them all in another file named master list

I think I have covered most of this (first time with VBA though so not sure), but I haven’t figured out how to read everything up to the first comma in each cell

Also please let me know if I have any obvious errors, or logic issues

Thank you in advance

Thank you for your help!

Sub CopyWordsToMainFileRow()

Dim Cell As Range
Dim counter As Integer
Dim word As String
Dim arrayOfIngredients() As Variant 'array of words from search
Dim fileName As String
Dim arrayOfFileNames As Variant
Dim MainCounter As Integer
Dim p As String, x As Variant

MainCounter = 0
counter = 0

' Make array of file names
    p = "/Users/waf04/Desktop/*.xls"
    arrayOfFileNames = GetFileList(p)

    Select Case IsArray(arrayOfFileNames)
        Case True 'files found
            MsgBox UBound(arrayOfFileNames)
            Sheets("Sheet1").Range("A:A").Clear
            For i = LBound(arrayOfFileNames) To UBound(arrayOfFileNames)
                Sheets("Sheet1").Cells(i, 1).Value = arrayOfFileNames(i)
            Next i
        Case False 'no files found
            MsgBox "No matching files"
    End Select
'end make array of file names

'Create array from cells in each file
For fileNameCounter = 0 To UBound(arrayOfFileNames)

    fileName = arrayOfFileNames(MainCounter)
    Workbooks.Open fileName:="fileName"
    arrayOfIngredients = Range("AT2:EP200").Value 'add value of cells to array

    'make array of results for each file
    For Each Cell In Range("AT2:EP200")
        word = Cell.Value ' make this string equal to the value of everything before the first comma in that cell
        arrayOfIngredients(counter) = word 'add string to array
        counter = counter + 1
        Next Cell

Workbooks.Close fileName:="fileName"
Next fileNameCounter
'==============================
'Output unsorted array
Workbooks.Open fileName:="/Users/waf04/Desktop/ingredients_collection.xlsx"
Range("A1:A" & UBound(arrayOfIngredients) + 1) = _
WorksheetFunction.Transpose(arrayOfIngredients)

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:47:03+00:00Added an answer on June 15, 2026 at 1:47 pm

    Here’s your code revised to address some lgic issues and to show how to get the string up to the first comma.

    This outputs comma separated list of words from each file in a single column in output file.

    I’ve assume in all cases the worksheet of interest in each workbook is index 1. You may need to change this to suit your sheets.

    Note: I have developed this on a Windows machine, it may mave issues on Mac I am unaware of.

    Changes are explained with comments ' *** like this

    Sub CopyWordsToMainFileRow()
        Dim cell As Range
        Dim counter As Long 'Integer *** no advanatge in using Integer, and risks overflow
        Dim word As Variant  'String *** need variant for For Each loop
        Dim arrayOfIngredients() As Variant 'array of words from search
        Dim fName As String ' fileName As String *** dont use keywords as variables
        Dim arrayOfFileNames As Variant
        Dim MainCounter As Long 'Integer
        Dim p As String, x As Variant
    
        ' *** extra variables
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim arrayFromSheet As Variant
        Dim CellValue As Variant
    
        ' *** not used ?
    '    MainCounter = 0
    '    counter = 0
    
        ' Make array of file names
        p = "/Users/waf04/Desktop/*.xls"
        arrayOfFileNames = GetFileList(p)
    
        Select Case IsArray(arrayOfFileNames)
            Case True 'files found
                MsgBox UBound(arrayOfFileNames)
                With Sheets("Sheet1") ' *** avoid multiple references to sheet
                    .Range("A:A").Clear
    '            For i = LBound(arrayOfFileNames) To UBound(arrayOfFileNames)
    '                Sheets("Sheet1").Cells(i, 1).Value = arrayOfFileNames(i)
    '            Next i
    
                ' *** put file names into sheet in one step ***
                    .Range(.Cells(1, 1), .Cells(UBound(arrayOfFileNames) - LBound(arrayOfFileNames) + 1)) = arrayOfFileNames
                End With
            Case False 'no files found
                MsgBox "No matching files"
                ' ***** End Sub here. ***
                Exit Sub
        End Select
        'end make array of file names
    
        ' *** Initialise results array: Range("AT2:EP200")  has 20099 cells
        ReDim arrayOfIngredients(1 To 20099)  ' <== you may want a more generic sizing solution
    
    
        'Create array from cells in each file
        'For fileNameCounter = 0 To UBound(arrayOfFileNames)
        For fileNameCounter = LBound(arrayOfFileNames) To UBound(arrayOfFileNames) ' *** handle 0 or 1 based arrays
    
            fName = arrayOfFileNames(fileNameCounter) ' MainCounter) *** use correct counter
            Set wb = Workbooks.Open(fileName:=fName)  ' *** use  variable, use workbook object
            Set ws = wb.Worksheets(1) ' *** use worksheet object, set to required sheet
            ' *** don't overwrite prior results, so don't need this line
            ' arrayOfIngredients = ws.Range("AT2:EP200") 'add value of cells to array 
    
            'make array of results for each file
            '  *** don't loop over cells, get data into an array instead
            arrayFromSheet = ws.Range("AT2:EP200")
            counter = 1 ' *** initialise counter for each file
            'For Each Cell In Range("AT2:EP200")
            For Each word In arrayFromSheet
                ' *** see new code below
                'word = Cell.Value ' make this string equal to the value of everything before the first comma in that cell
                i = InStr(word, ",")
                If i > 0 Then
                    arrayOfIngredients(counter) = arrayOfIngredients(counter) & Left$(word, i - 1) & ","  ' *** add string to array
                Else
                    ' *** what to do if no , ???
                End If
                counter = counter + 1
            Next word
    
            wb.Close SaveChanges:=False ' *** close object
        Next fileNameCounter
        '==============================
        'Output unsorted array
        ' *** strip trailing comma
        For i = LBound(arrayOfIngredients) To UBound(arrayOfIngredients)
            If Len(arrayOfIngredients(i)) > 0 Then
                arrayOfIngredients(i) = Left$(arrayOfIngredients(i), Len(arrayOfIngredients(i)) - 1)
            End If
        Next i
        Set wb = Workbooks.Open(fileName:="/Users/waf04/Desktop/ingredients_collection.xlsx") ' *** use object
        wb.Worksheets(1).Range("A1:A" & UBound(arrayOfIngredients) - LBound(arrayOfIngredients) + 1) = _
        Application.Transpose(arrayOfIngredients) ' *** use object and use Application rather than Worksheet tramspose
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never code in Python and trying to switch from Javascrpt/SVG. Being confused
I have this code that runs but never stops. class A { public static
I've never coded anything for the iPhone but have written a GPS tracker for
I've never used java from the terminal before, and I certainly have never coded
I am trying to translate the VBA code found in this link into IronPython.
I have never used Try-catch in my code before, but now I need to
I have seen many souce codes but I have never ever come across a
I have never used Foxpro before. Can you convert the following Foxpro code into
Never thought I'd have this problem :) The following snippet of code works in
I have never saved and retrieved an image to and from the database before.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.