Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save joonjoonjoon/f961da798ddb69fce2c3 to your computer and use it in GitHub Desktop.

Select an option

Save joonjoonjoon/f961da798ddb69fce2c3 to your computer and use it in GitHub Desktop.
MakeAllFilesIntoOne VBA script
'ok so open the excel thing that you want to use
'it's probably .xlsx and you need to make it .xlsm
'cause then you can have macros and shit
'now you need to enable developer shit
'First, go into excel options > popular > enable developer tb
'(yeah there's really a tab called 'popular')
'Open Visual Basic
'Insert > Module
'Paste the shit below and hit the play button
Sub MakeAllFilesIntoOne()
'declare some shit
Dim number As Integer
Dim niceNumber As Integer
Dim lastName As String
'give that shit some starting values
lastName = ""
number = 1
niceNumber = 1
'point to your shit
Path = "C:\...\bunch of excel files\"
'this is some VBA voodoo, who knows really
Filename = Dir(Path & "*.xlsx")
'loopty loop
Do While Filename <> ""
'so this opens the file, I think? and it sets it active
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
'ok so ActiveWorkbook is the file we just opened
'and we're copying it after the last sheet of ThisWorkbook which is where we started
ActiveWorkbook.Sheets(1).Copy After:=ThisWorkbook.Sheets(number)
'figure out the first word of the filename
Dim niceName As String
intPos = InStr(1, Filename, " ")
If intPos > 0 Then
niceName = Left(Filename, intPos - 1)
Else
niceName = Filename
End If
'if the word is different, reset the nice number
If niceName <> lastName Then
niceNumber = 1
End If
lastName = niceName
'increase that shit
number = number + 1
niceNumber = niceNumber + 1
'rename that shitty sheet
ThisWorkbook.Sheets(number).Name = niceName & " " & niceNumber
'close that shit
Workbooks(Filename).Close
'seriously VBA, I don't even know what this does but it needs to be there
Filename = Dir()
Loop
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment