Sub ExportData_V02()
Dim wbCurrent As Workbook, wbExport As Workbook
Dim wsCurrent As Worksheet, wsExportMain As Worksheet
Dim rngCurrent As Range
Dim cellUnlock As Range
Dim arrUnlock As Variant
Dim i As Long
Dim FirstCell As Range
Dim CurrCell As Range
Dim ExportDateTime As Date
Dim fnameFull As String, ExportFName As String
Dim arrShtNames As Variant, ShtName As Variant
Dim startTime As Double
startTime = Timer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set wbCurrent = ThisWorkbook
fnameFull = wbCurrent.FullName
arrShtNames = Array("Comments", "Info-Setup", "Plates-Input")
ReDim arrUnlock(1 To 100000, 1 To 1)
With Application.FindFormat
.Clear
.Locked = False
End With
Dim pwd As String
'pwd = "Test"
For Each ShtName In arrShtNames
Set wsCurrent = wbCurrent.Worksheets(ShtName)
Set rngCurrent = wsCurrent.UsedRange
'wsCurrent.Unprotect Password:=pwd
With rngCurrent
Set FirstCell = .Cells.Find(What:="", After:=.Cells(1, 1), _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=True)
End With
If Not FirstCell Is Nothing Then
Set CurrCell = FirstCell
' First row in output array
If i = 0 Then
i = i + 1
ExportDateTime = Now
arrUnlock(i, 1) = "Export Run: " & ExportDateTime
End If
Do
i = i + 1
With CurrCell
arrUnlock(i, 1) = .Parent.Name & "~" & .Address(0, 0, 1, 0) & "~" & .Formula
End With
Set CurrCell = rngCurrent.Cells.Find(What:="", After:=CurrCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=True)
Loop Until CurrCell.Address = FirstCell.Address
End If
'wsCurrent.Protect Password:=pwd
Next ShtName
If i > 1 Then
arrUnlock(1, 1) = arrUnlock(1, 1) & " ~ No of Cells: " & i - 1
' Output Export Array
Set wbExport = Workbooks.Add(xlWBATWorksheet)
Set wsExportMain = ActiveSheet
With wsExportMain
.Range("A1").Resize(i).Value = arrUnlock
.Columns(1).AutoFit
End With
Dim FldrPicker As FileDialog
Dim myFolder As String
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Export Folder"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button
myFolder = .SelectedItems(1) & "\"
End With
With wbExport
'ExportFName = Left(fnameFull, InStrRev(fnameFull, ".") - 1) & Format(ExportDateTime, "_yyyymmdd_hhmmss") & ".csv"
ExportFName = myFolder & Left(wbCurrent.Name, InStrRev(wbCurrent.Name, ".") - 1) & Format(ExportDateTime, "_yyyymmdd_hhmmss") & ".csv"
.SaveAs FileName:=ExportFName, FileFormat:=xlCSV
.Saved = True
.Close
End With
End If
Application.FindFormat.Clear
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Dim msg As String
msg = "Data export completed successfully! ( " & Format(Timer - startTime, "0.00") & " seconds)"
MsgBox msg
End Sub
Sub ImportData_v02()
Dim wbCurrent As Workbook, wbImport As Workbook
Dim wsImportMain As Worksheet
Dim rngImport As Range
Dim i As Long
Dim fnameImport As String
Dim cellImport As Variant
Dim rCell As Variant
Dim startTime As Double
startTime = Timer
Set wbCurrent = ThisWorkbook
fnameImport = Application.GetOpenFilename(FileFilter:="CSV Files,*.csv", Title:="Select file to import", MultiSelect:=False)
If fnameImport = "False" Then
MsgBox "No file selected, exiting macro"
GoTo CleanExit
End If
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set wbImport = Workbooks.Open(FileName:=fnameImport, ReadOnly:=True)
Set wsImportMain = ActiveSheet
Set rngImport = wsImportMain.Range("A1").CurrentRegion.Columns(1)
Set rngImport = rngImport.Resize(rngImport.Rows.Count - 1).Offset(1) ' Exclude header in A1
For Each rCell In rngImport.Cells
' format = sheetname ~ cell address ~ cell content
cellImport = Split(rCell.Value, "~")
With wbCurrent
.Worksheets(cellImport(0)).Range(cellImport(1)).Formula = cellImport(2)
End With
Next rCell
wbImport.Close SaveChanges:=False
Dim msg As String
msg = "Data import completed successfully! ( " & Format(Timer - startTime, "0.00") & " seconds)"
MsgBox msg
CleanExit:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub