Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

Version 1 Next »

In the below video you will see how you can use the macro here to find out what images exist/match the SKU code provided in the Excel file:

Creating an Excel VBA macro to accomplish the tasks you've described would involve several steps. Here's a step-by-step guide on how to create such a macro:

  1. Open Excel and press Alt + F11 to open the Visual Basic for Applications (VBA) editor.

  2. In the VBA editor, right-click on "VBAProject (Your Workbook Name)" in the Project Explorer on the left and select "Insert" -> "Module" to insert a new module.

  3. Copy and paste the following VBA code into the module:

Sub ConceptSauce_ImageMatchHelper()

    Dim folderPath As String
    Dim filenameOnly As String
    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim fileObj As Object
    Dim subfolder As Object
    Dim folder As Object
    Dim fso As Object
    Dim filenamesDict As Object
    
    
    ' Create a dictionary to store filenames
    Set filenamesDict = CreateObject("Scripting.Dictionary")

    ' Prompt user for the folder
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        If .Show = -1 Then
            folderPath = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With

    ' Create filesystem object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(folderPath)
' MsgBox "Checking files in: " & folderPath


    ' Add filenames from main folder
    For Each fileObj In folder.Files
  '  MsgBox "Found file: " & fileObj.Name
    filenameOnly = Split(fileObj.Name, ".")(0)
    If Not filenamesDict.exists(filenameOnly) Then
        filenamesDict.Add filenameOnly, True
  '      MsgBox "Added to dictionary: " & filenameOnly
    End If
Next fileObj


    ' Check if there are subfolders, then add filenames from them
    If folder.subfolders.Count > 0 Then
        For Each subfolder In folder.subfolders
            For Each fileObj In subfolder.Files
                filenameOnly = Split(fileObj.Name, ".")(0)
                If Not filenamesDict.exists(filenameOnly) Then
    filenamesDict.Add filenameOnly, True
 '   MsgBox "Added to dictionary: " & filenameOnly
End If

            Next fileObj
        Next subfolder
    End If

    ' Get the active worksheet
    Set ws = ThisWorkbook.ActiveSheet

    ' Ask user to select the column to match
    On Error Resume Next
    Set rng = Application.InputBox("Select a range", Type:=8)
    On Error GoTo 0

    ' Exit if the range is not valid or has no cells
    If rng Is Nothing Or rng.Cells.Count = 0 Then Exit Sub

    ' Color-code cells based on filename matches
    For Each cell In rng.Cells
        On Error Resume Next
 '       MsgBox cell.Value
 '       MsgBox "Checking cell value: " & cell.Value
        If filenamesDict.exists(CStr(Trim(cell.Value))) Then
            cell.Interior.Color = RGB(0, 255, 0) 'Green
        Else
            cell.Interior.Color = RGB(255, 0, 0) 'Red
        End If
        On Error GoTo 0
    Next cell

End Sub
  1. Close the VBA editor and return to your Excel workbook.

  2. To run the macro, press Alt + F8 to open the "Macro" dialog, select "CheckFileNames," and click "Run."

Here is a video to show this in action (smile)

Excel Macro to match SKU codes with file names.mp4

  • No labels