MACRO/VBA - Count number of version a File has in Folder

acerlaptop

New Member
Joined
Feb 17, 2020
Messages
44
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I have a column (A) which has the list of all files in a folder & subfolders (incrementing value removed). I want to add to column B the number of versions a file has.

Example:
File Name "ABC CORP"

If in folder and subfolders there are files like ABC CORP-1 and ABC CORP-2 then column B would return "2"

Thanks in advance
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this

Put the following in a module, run the macro "Count_Number_Version".
Put the list of names starting in cell A2 down.

VBA Code:
Option Explicit

Dim rutas As New Collection

Sub Count_Number_Version()
  Dim sPath As String, arch As Variant, sd As Variant
  Dim a() As Variant, b As Variant, c As Variant
  Dim i As Long, j As Long, n As Long
  '
  Set rutas = Nothing
  sPath = "C:\trabajo\"
  rutas.Add sPath
  Call AddSubDir(sPath)
  '
  For Each sd In rutas
    arch = Dir(sd & "\*.*")
    Do While arch <> ""
      ReDim Preserve a(i)
      a(i) = arch
      i = i + 1
      arch = Dir()
    Loop
  Next
  '
  b = Range("A2", Range("A" & Rows.Count).End(3))
  ReDim c(1 To UBound(b), 1 To 1)
  For i = 1 To UBound(b)
    n = 0
    For j = 0 To UBound(a)
      If a(j) Like b(i, 1) & "*" Then
        n = n + 1
      End If
    Next
    c(i, 1) = n
  Next
  '
  Range("B2").Resize(UBound(b)).Value = c
End Sub
'
Sub AddSubDir(lpath)
  Dim SubDir As New Collection, DirFile As Variant, sd As Variant
  If Right(lpath, 1) <> "\" Then lpath = lpath & "\"
  DirFile = Dir(lpath & "*", vbDirectory)
  Do While DirFile <> ""
    If DirFile <> "." And DirFile <> ".." Then _
      If ((GetAttr(lpath & DirFile) And vbDirectory) = 16) Then SubDir.Add lpath & DirFile
    DirFile = Dir
  Loop
  For Each sd In SubDir
    rutas.Add sd
    Call AddSubDir(sd)
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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