How to copy column and transpose paste into new workbook with folder containing several files? - vba

I have a folder containing nearly 1,000 .csv files. I would like to grab the second column from each of these files and transpose-paste them into a new Excel workbook, so that the data is in one row.
The following is what i have so far:
Sub OpenFiles2()
Dim MyFolder As String
Dim MyFile As String
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Do While myPath <> ""
Range(Range("B1"), Range("B1").End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWorkbook.Close True
Windows("Compiled.xlsm").Activate
Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1,0).PasteSpecial Transpose:=True
MyFile = Dir
Loop
End Sub
For some reason I keep getting an error for the Paste Special command.
I also tried to replace it with:
ActiveSheet.PasteSpecial Transpose:=True
And
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= False, Transpose:=True
Still got errors. Please help. Thank you.

I would avoid using select and deal with the values. This code stores the original value in a variable, then you can close the active workbook and use the data in that variable by using the Application.Transpose within VBA.
Replace the Do Loop with the below code.
Do While myPath <> ""
lastrow = Cells(Rows.Count, 2).End(xlUp).Row
x = Range("B1:B" & lastrow).Value
ActiveWorkbook.Close True
With Worksheets("Sheet1")
Range("A" & .Cells(.Rows.Count, 1).End(xlUp).Row + 1). _
Resize(, lastrow).Value = Application.Transpose(x)
End With
MyFile = Dir
Loop

Related

VBA EXP file transformed to Excel file not allowing code to work

I hope you can help. The issue I am facing is this I have two pieces of code the first piece takes EXP files stored in a folder and transforms them into Excel files "xls" this piece of code is labelled CODE Piece 1 below.
This piece of code seems to work fine.
The second piece of code labelled CODE Piece 2 below. Allows a user to navigate to a folder select this folder then the code loops through all the files in this folder and deliminates the Excel files with | and then it should add new rows and columns. The issue is that the files deliminate no problem but the adding of rows and columns does not happen.
The odd thing is if I run this macro on workbooks that have not been through CODE Piece 1, Everything works fine the row and columns add.
But even if take out the deliminating part of the code and just just tell the macro to added the row and the columns and i run it on the files that have been transformed by CODE piece 1 again nothing. I don't get any errors, the macro runs but nothing happens.
If I even turn on the screen updating applications and I can see the macro doing its thing, adding the row and columns and saving the document but when I went to open the file back up i got the an error pop up (see error 1 below) once i clicked 'Yes' on the pop up the file would open but no added row or columns
I am at a loss as to how to solve this issue. I believe my coding is good but Ia m not getting the result I want. Can someone please advise.
As always any and all help is welcome.
CODE Piece 1
Public Sub Loop_Rename_Files_in_Folder()
Dim folder As String
Dim filename As String
folder = "C:\Users\CONNELLP\Desktop\Claire Macro\Reject Macro\Move Exceled\" 'MODIFY THIS LINE - FOLDER CONTAINING FILES TO BE RENAMED
If Right(folder, 1) <> "\" Then folder = folder & "\"
filename = Dir(folder & "*.exp")
Do While filename <> vbNullString
Name folder & filename As folder & Left(filename, InStrRev(filename, ".")) & "xls"
filename = Dir
Loop
End Sub
CODE Piece 2
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim rng As Range
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xl??*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Change First Worksheet's Background Fill Blue
With wb
Set rng = Range("A:A")
rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, _
OtherChar:="|"
Range("A1").EntireRow.Insert
Range("A:E").EntireColumn.Insert
End With
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Error 1
Formatted
STUCK AS TXT
CODE that saves as new Workbook but leaves the corrupted Originals as is
CODE Piece 3
Sub OpenFiles()
'UpdateByExtendoffice20160623
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim wb As Workbook
Set wb = ActiveWorkbook
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "C:\Users\CONNELLP\Desktop\GAP Reports\"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xl??")
Do While xFile <> ""
Workbooks.Open xStrPath & "\" & xFile
With wb
Set rng = Range("A:A")
rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, _
OtherChar:="|"
Range("A1").EntireRow.Insert
Range("A:E").EntireColumn.Insert
Range("A1").Value = "Source File"
Range("B1").Value = "Reason"
Range("C1").Value = "Action"
Range("D1").Value = "Team"
Range("E1").Value = "Status"
ActiveWorkbook.SaveAs FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close
End With
xFile = Dir
Loop
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
You should save the Workbooks in CODE Piece 1 as .xlsx instead of .xls (Win2003 Format). Also its enough to say myExtension = "*.xl*" for Excel Files. Your problem is when opening workbooks, that you dont address then worksheet correctly. Also what does DoEvents do? Its probably not necessary.
Code Piece 2 should look like this:
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String: myPath = ""
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim rng As Range
Dim sht As Worksheet
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xl*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(myPath & myFile)
Set sht = wb.Worksheets(1) 'First Sheet (adjust if needed)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Change First Worksheet's Background Fill Blue
Set rng = sht.Range("A:A")
rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, _
OtherChar:="|"
With sht
.Range("A1").EntireRow.Insert
.Range("A:E").EntireColumn.Insert
End With
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

VBA Sort DIR to transfer data in alphabetical order

I have written a macro below to copy and paste data from all workbooks within a user selected folder into a master document, however currently the macro selects the files in a random order. What I want to do is for it to select the files in alphabetical order, so the data in the master document is in the correct order... Help achieving this would be much appreciated, I am not precious about the method!
Sub Import_Data()
' PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
Dim WB As Workbook
Dim wbThis As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Set wbThis = ActiveWorkbook
' Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' Retrieve Target Folder Path From User
MsgBox "Please select Faro Scan Data Folder"
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
' In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
' Target File Extension (must include wildcard "*")
myExtension = "*.xls"
' Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
' Loop through each Excel file in folder
Do While myFile <> ""
' Set variable equal to opened workbook
Set WB = Workbooks.Open(Filename:=myPath & myFile)
' Ensure Workbook has opened before moving on to next line of code
DoEvents
' Copy data from target workbook....
WB.Activate
Application.CutCopyMode = False
Range("D8:D377").Copy
wbThis.Activate
Sheets("Faro Scan Data").Select
Range("E5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
' Insert column for next data set
Columns("E:E").Select
Selection.Insert Shift:=xlToRight
' Format column for new dataset
Columns("I:I").Select
Selection.Copy
Columns("E:E").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
' Close Workbook
WB.Close SaveChanges:=False
' Ensure Workbook has closed before moving on to next line of code
DoEvents
' Get next file name
myFile = Dir
Loop
' Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
' Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
MsgBox "Remeber to enter column headings!"
End Sub
Take a look at the below example showing how you can loop through files in folder with filter and sorted in alphabetical order using Shell.Application ActiveX:
Option Explicit
Sub Test_Shell_Folder_Items()
Dim sPath
Dim sExtension
Dim oShellApp
Dim oFolder
Dim oFolderItems
Dim oFolderItem
sPath = "C:\Test"
sExtension = "*.xls"
Set oShellApp = CreateObject("Shell.Application")
Set oFolder = oShellApp.Namespace(sPath)
Set oFolderItems = oFolder.Items()
oFolderItems.Filter 64 + 128, sExtension ' 32 - folders, 64 - not folders, 128 - hidden
For Each oFolderItem In oFolderItems
Debug.Print oFolderItem.Path
Next
End Sub

Excel closed unexpectedly when running VBA Macros (But only sometimes)

I am wondering if there are any experts that can have a look and advise what I am doing wrong with the codes below. I've adapted and modified the codes from TheSpreadsheetGuru which basically opens up all the Excel spreadsheets in a given folder and copy/paste the information into the master spreadsheet.
If I open my master spreadsheet and run the macro it actually works fine. However, if I clear the contents in the master spreadsheet first or run the macro more than once then Excel would just shut itself down - I can't see anything that's obviously wrong with the codes so would appreciate any help if you can tell me what I am doing wrong
Thanks in advance
Sub SI_Report()
'PURPOSE: To copy strategic initiatives report into the master table
'SOURCE: Codes here are modified based on codes obtained from TheSpreadsheetGuru.com
Check = MsgBox("This will copy all the strategic initiatives from spreadsheets stored in a folder you will now choose, are you sure?", vbOKCancel)
If Check = vbOK Then
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimise Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
' Clear contents first
Windows("Strategic Initiatives Master.xlsm").Activate
Sheets("Strategic Initiatives").Select
Range("A2:W201").Select
Selection.ClearContents
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile, UpdateLinks:=0)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Copy data
wb.Sheets("Strategic Initiatives").Select
Range("A2", Range("W2").End(xlDown)).Select
Selection.Copy
'Paste data
Windows("Strategic Initiatives Master.xlsm").Activate
Sheets("Strategic Initiatives").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Close Workbook without Saving
wb.Close SaveChanges:=False
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
Sheets("Instruction").Select
ResetSettings:
'Reset Macro Optimisation Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Else: Exit Sub
End If
End Sub
Try using Option Explicit to make sure all the variables have been declared

update links prompt issue

I have a length code which opens set of files, unhides and navigates to a particular worksheet, copies a range and pastes that range in another workbook.
The problem is whenever the code opens these files a popup message to update links appears. I understand it can be solved with updatelinks = 0 however wanted to know where should i include this in my code.
Also the code takes time to execute, so is there any modifications for faster execution.
Sub mergeallinputworkbooks()
Dim wkbDest As Workbook
Dim wksDest As Worksheet
Dim wkbSource As Workbook
Dim wksSource As Worksheet
Dim MyPath As String
Dim MyFile As String
Dim FolderName As String
Dim oCell As Range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Set wkbDest = ThisWorkbook
Set wksDest = wkbDest.Worksheets("Master Data")
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
FolderName = .SelectedItems(1)
Err.Clear
On Error GoTo 0
End With
MyPath = FolderName
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xls")
Do While Len(MyFile) > 0
Set wkbSource = Workbooks.Open(MyPath & MyFile)
Set wksSource = wkbSource.Worksheets("Scoring DB")
ActiveWorkbook.Unprotect ("pyroo123")
Sheets("Scoring DB").Visible = True
Sheets("Scoring DB").Select
Range("A4:W4").Copy
Windows("Performance Dashboard.xlsm").Activate
With Sheets("Master Data").Range("$A:$A")
With Sheets("Master Data")
Set oCell = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
End With
oCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Windows("Performance Dashboard.xlsm").Activate
End With
wkbSource.Close savechanges:=False
MyFile = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
For you links issue, have a look at this post. There should be enough information there to give you a good indication of how and where to use the link update.
Now code suggestion:
To improve performance of your code, I would suggest not to interact with worksheet where not necessary. Rather than 'Copy and Past' assign the range to an array:
arrMyRange = Worksheets("SourceWorksheet").Range("A4:W4")
This will create your array. Now assign the array to your location:
Worksheets("DestinationWorksheet").Range("A1").Resize(UBound(arrMyRange, 1), UBound(arrMyRange, 2)).Value = arrMyRange
A1 can be changed dynamically if required.

How to add up two-dimensional arrays?

My code runs trough dozens of excel documents, selects range and gives the range to an array. I would like to add up the arrays to get a summarized data then paste the result to an existing worksheet.
The formula should be something like this:
rangeVar = oNewBook.Worksheets(1).Range("A1:D4").Value
sumRange = sumRange + rangeVar
Important! Some cells in the range is empty (I don't know is this matters). Also I would like to add up the values separately like sumRange(1,1)+rangeVar(1,1) ; sumRange(2,2)+rangeVar(2,2) , etc... How to do this?
You can check the code here:
Sub LoopAllExcelFilesInFolder()
Dim OutputWs As Worksheet
Dim oNewBook As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim Lastrow As Long
Dim i As Integer, j As Integer
Dim summaryVar() As Variant
Dim rangeVar() As Variant
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension
myExtension = "*.xlsx"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'set output worksheet
Set OutputWs = ThisWorkbook.Worksheets("Teszt")
'Loop through each Excel file in folder
Do While myFile <> ""
Set oNewBook = Workbooks.Open(myPath & myFile)
rangeVar = oNewBook.Worksheets(1).Range("A1:D4").Value
oNewBook.Close
'Copy selected items
With OutputWs
Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
OutputWs.Range("A" & Lastrow & ":" & "D" & Lastrow) = Application.WorksheetFunction.Sum(rangeVar) 'summaryVar
Paste:=xlPasteAll, operation:=xlPasteSpecialOperationAdd, skipBlanks:=False
Application.CutCopyMode = False
End With
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
S. Meaden answers this question wonderfully in How to add arrays?. Instead of trying to add the two arrays together, he makes use of Excel's pasteSpecial Addvalues function to add the original range's values to another range. Based on his code, something like the below should work.
Set tempWS = Sheets.Add
Do While myFile <> ""
Set oNewBook = Workbooks.Open(myPath & myFile)
oNewBook.Worksheets(1).Range("A1:D4").Copy
tempWS.Range("A1:D4").PasteSpecial Paste:=xlPasteAll, operation:=xlPasteSpecialOperationAdd
oNewBook.Close
Standard Excel Worksheet Functions will work on 1 and 2 dimensional arras.
Sub Test()
Dim array2(25, 25) As Double
Dim i As Integer, j As Integer
For i = 0 To UBound(array2, 1)
For j = 0 To UBound(array2, 1)
array2(i, j) = Int((Rnd * 100) + 1)
Next
Next
MsgBox WorksheetFunction.Sum(array2)
End Sub