sparkytech
Board Regular
- Joined
- Mar 6, 2018
- Messages
- 96
- Office Version
- 365
- 2019
I am getting closer every day to my goal, but am I never satisfied (continuous improvement mindset). My code below copies data from a "Master" sheet, and then prompts the user to select a file within a specific folder to export / paste the data into. Is there a way to "preselect" the file, but wait until the user clicks "Open" to continue? There are (3) files in this directory (named such as West 2022 - Export, Central 2022 - Export, and East 2022 - Export), and while the same export file is usually selected, I also want to give the end user a chance to choose another file, or even different directory. The master file and the export file are named the same, with the addition of " - Export" on the end of the filename. What would really be cool is for the code to look at the current filename, and then preselect the associated "Export" file. For example: the user opens the workbook named "West 2022". When they run the export code, it looks for the matching filename, in this case "West 2022 - Export", and then preselects that file from the folder contents, and waits for the user to continue. Can someone help me with these goals, and could this code be simplified / cleaned up? Code below, thanks in advance!
VBA Code:
Sub ExportCleanExcel(control As IRibbonControl)
' +-+-+-+-+-+-+ +-+-+-+-+-+ +-+-+-+-+-+
' |E|x|p|o|r|t| |C|l|e|a|n| |E|x|c|e|l|
' +-+-+-+-+-+-+ +-+-+-+-+-+ +-+-+-+-+-+
'Exports the information in the master sheet to a chosen excel file;
'will just move the data without macros or other extra features
Dim FileToOpen As Variant
Dim DestWkb As Workbook
Dim MasBotRow As Long
Dim CurrentDir As String
'Turn off screen updates to improve performance
With Application
.ScreenUpdating = False: .DisplayAlerts = False
End With
CurrentDir = CurDir
ChDrive "T:\Project Spreadsheets\2022"
ChDir "T:\Project Spreadsheets\2022"
'Allows user to select a file to export to using the traditional open file window
FileToOpen = Application.GetOpenFilename("All Excel Files (*.xls?), *.xls?", , "Please export file")
'If the user selects cancel when choosing a file to open FileToOpen is set to FALSE
'Using this we can check if a file was actually selected before continuing
If FileToOpen <> False Then
' Set MasBotRow variable to row 2500
MasBotRow = 2500
'Open the file that was selected and then set that workbook as DestWkb
Set DestWkb = Application.Workbooks.Open(FileToOpen)
'Copies rows A5 to W to last row and also Z5 to last row on sheet {skips "X:Y"}
ThisWorkbook.Sheets("Master").Range("A5:W" & MasBotRow).Copy
DestWkb.Sheets(1).Range("A5:W" & MasBotRow).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Master").Range("Z5:Z" & MasBotRow).Copy
DestWkb.Sheets(1).Range("X5:X" & MasBotRow).PasteSpecial xlPasteValues
'Close the data file
DestWkb.Close True
MsgBox "Export Complete", 64, "Export Complete"
End If
ChDir CurrentDir
'Turn back on screen updates
'Turn back on alerts after closing the file
With Application
.ScreenUpdating = True: .DisplayAlerts = True: .CutCopyMode = False
End With
End Sub