Sub CreateNewWorkSheet() 'Instantiate variables Dim xSht As Worksheet Dim xNSht As Worksheet Dim xSUpdate As Boolean Dim xRow As Long Dim MyFile As String Dim Counter As Long On Error Resume Next Set xSht = ActiveWorkbook.Sheets("3rd Party") 'Create a dynamic array variable, and then declare its initial size Dim DirectoryListArray() As String ReDim DirectoryListArray(1000) 'Loop through all the files in the directory by using Dir$ function MyFile = Dir$("C:\Users\Desktop\3rd Party\Work Folder\*.*") 'This line of code just helps the macro sun faster xSUpdate = Application.ScreenUpdating Application.ScreenUpdating = False For Counter = 0 To UBound(DirectoryListArray) DirectoryListArray(Counter) = MyFile 'If the sheet does not exist, then create the new sheet and name it the string from index I If xNSht Is Nothing Then Set xNSht = Worksheets.Add(, Sheets(Sheets.Count)) xNSht.Name = DirectoryListArray(Counter) Else End If Counter = Counter + 1 Next Counter 'Reset the size of the array without losing its values by using Redim Preserve ReDim Preserve DirectoryListArray(Counter - 1) xSht.AutoFilterMode = False xSht.Activate Application.ScreenUpdating = xSUpdate End Sub
Gain unlimited access to on-demand training courses with an Experts Exchange subscription.Get Access
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE
Connect with Certified Experts to gain insight and support on specific technology challenges including: