R
RompStar
HI there... I written a Sub that opens Excel files from a network
drive and imports them into an Excel sheet one at a time in an append
fashion inside a loop. So, I have create a different Sub previously
that export Excel files to their respective network folders, and then
employees work on it, and then we needed an automatic way to import
them all in for analysis. I have a Log from from the previous export
process so records the locations/file names of all the excel files
that were exported, so I use that to import them back in.
My Sub works very well, but I wanted to include a way to check when
the File is being opened for different supprise conditions that can
arise, including errors.
I am not sure how Excel behaves in opening a file if someone else has
it open and if working on it.
or
IF the file is missing for some reason from the expected location
where it should be
or whatever other conditions there exists that could become
problematic, basically I want the script to cycle through all the
files and handle errors or problems gracefully so that the VBA don't
error out.
Anyways, I am posting my entire VBA script that I did, which works,
but I wanted to add some handling, and I am not sure if a Function
Call would be appropriate here, have little experience with Function
Calls. Also, I consider my skills like beginner to maybe a little
intermediate, so if anyone has suggestions on making the script
better, let me know.
But basically it reads the locations and file names from a Log sheet,
so addresses would be something like this:
Location the Store folders were written to:
c:\Documents and Settings\LANID\Desktop\test_store_import\Store_1_.xls
c:\Documents and Settings\LANID\Desktop\test_store_import\Store_2_.xls
Location the Store folders were written to, but in the real script
they will point to network drives, instead of my local computer for
testing..... The script also strips the store number value from the
file name, so that the right sheet for copy can be selected, which in
this example would be a sheet named 1, which represents store number
1, thanks a lot!
code below ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
Sub Import_From_Log()
Dim wsLogName, wsOpenName As String
Dim wsNew, wsLog As Worksheet ' New sheet that will contain all the
imported Excel sheets, appended in one sheet
Dim wsLogRange As Range
Dim FileToOpen, get_store_number As String
Dim lastrow, lastrow2, lastrow3, real As Long
Dim RangeCell As Range
Dim Switch As Worksheet
wsLogName = ActiveWorkbook.Name
Set wsLog = ActiveSheet
Application.ScreenUpdating = False
With wsLog
Sheets("Log").Activate
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row ' last row is
stored in variable RangeFinder
Set wsLogRange = Range("A2:A" & lastrow)
End With
Set wsNew = Sheets.Add ' Add the new workbook that will contain the
updated lists
wsNew.Name = "File_Import_Appended"
For Each Value2 In wsLogRange
FileToOpen = Value2
With wsLog
Workbooks.Open FileName:=FileToOpen ' works up to here
wsOpenName = ActiveWorkbook.Name ' record the name of Excel
file just opened into a variable for later usage...
get_store_number = Left(Mid(wsOpenName, 7), 1) ' gets the value
for the sheet number to select and copy from, ignore all else
ActiveWorkbook.Sheets(get_store_number).Activate ' make sure to
only select the store sheets to copy from
Range("A1").Select ' start by placing cursor into cell A1
Selection.End(xlToRight).Select ' find last column
Selection.End(xlDown).Select ' next - find last row
Set RangeCell = ActiveCell ' record the right
side of the range and assign to RangeCell
Range("A1:" & RangeCell.Address()).Select ' select
the range
Selection.Copy ' now
copy it
Windows(wsLogName).Activate ' switch
back to the Append sheet
ActiveWorkbook.Sheets("File_Import_Appended").Activate ' make
sure we are activating the append sheet
With wsNew
lastrow2 = .Range("A" & .Rows.Count).End(xlUp).Row
End With
If lastrow2 = 1 Then ' if sheet is blank that mean nothing was
pasted, so start here for the first paste
ActiveSheet.Paste
Else
real = lastrow2 + 1 ' if the sheet was already posted, we want
all other pastes to follow this rule here
Range("A" & real).Select
ActiveSheet.Paste
End If
Application.Windows(wsOpenName).Activate
Application.CutCopyMode = False ' clear the clipboard, otherwise a
window will popup asking if clipboard contents should be saved
ActiveWorkbook.Close SaveChanges:=False ' close the Import
Excel Workbook
End With
Next Value2
With wsNew
Cells.EntireColumn.AutoFit
End With
Application.ScreenUpdating = True
End Sub
drive and imports them into an Excel sheet one at a time in an append
fashion inside a loop. So, I have create a different Sub previously
that export Excel files to their respective network folders, and then
employees work on it, and then we needed an automatic way to import
them all in for analysis. I have a Log from from the previous export
process so records the locations/file names of all the excel files
that were exported, so I use that to import them back in.
My Sub works very well, but I wanted to include a way to check when
the File is being opened for different supprise conditions that can
arise, including errors.
I am not sure how Excel behaves in opening a file if someone else has
it open and if working on it.
or
IF the file is missing for some reason from the expected location
where it should be
or whatever other conditions there exists that could become
problematic, basically I want the script to cycle through all the
files and handle errors or problems gracefully so that the VBA don't
error out.
Anyways, I am posting my entire VBA script that I did, which works,
but I wanted to add some handling, and I am not sure if a Function
Call would be appropriate here, have little experience with Function
Calls. Also, I consider my skills like beginner to maybe a little
intermediate, so if anyone has suggestions on making the script
better, let me know.
But basically it reads the locations and file names from a Log sheet,
so addresses would be something like this:
Location the Store folders were written to:
c:\Documents and Settings\LANID\Desktop\test_store_import\Store_1_.xls
c:\Documents and Settings\LANID\Desktop\test_store_import\Store_2_.xls
Location the Store folders were written to, but in the real script
they will point to network drives, instead of my local computer for
testing..... The script also strips the store number value from the
file name, so that the right sheet for copy can be selected, which in
this example would be a sheet named 1, which represents store number
1, thanks a lot!
code below ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++
Sub Import_From_Log()
Dim wsLogName, wsOpenName As String
Dim wsNew, wsLog As Worksheet ' New sheet that will contain all the
imported Excel sheets, appended in one sheet
Dim wsLogRange As Range
Dim FileToOpen, get_store_number As String
Dim lastrow, lastrow2, lastrow3, real As Long
Dim RangeCell As Range
Dim Switch As Worksheet
wsLogName = ActiveWorkbook.Name
Set wsLog = ActiveSheet
Application.ScreenUpdating = False
With wsLog
Sheets("Log").Activate
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row ' last row is
stored in variable RangeFinder
Set wsLogRange = Range("A2:A" & lastrow)
End With
Set wsNew = Sheets.Add ' Add the new workbook that will contain the
updated lists
wsNew.Name = "File_Import_Appended"
For Each Value2 In wsLogRange
FileToOpen = Value2
With wsLog
Workbooks.Open FileName:=FileToOpen ' works up to here
wsOpenName = ActiveWorkbook.Name ' record the name of Excel
file just opened into a variable for later usage...
get_store_number = Left(Mid(wsOpenName, 7), 1) ' gets the value
for the sheet number to select and copy from, ignore all else
ActiveWorkbook.Sheets(get_store_number).Activate ' make sure to
only select the store sheets to copy from
Range("A1").Select ' start by placing cursor into cell A1
Selection.End(xlToRight).Select ' find last column
Selection.End(xlDown).Select ' next - find last row
Set RangeCell = ActiveCell ' record the right
side of the range and assign to RangeCell
Range("A1:" & RangeCell.Address()).Select ' select
the range
Selection.Copy ' now
copy it
Windows(wsLogName).Activate ' switch
back to the Append sheet
ActiveWorkbook.Sheets("File_Import_Appended").Activate ' make
sure we are activating the append sheet
With wsNew
lastrow2 = .Range("A" & .Rows.Count).End(xlUp).Row
End With
If lastrow2 = 1 Then ' if sheet is blank that mean nothing was
pasted, so start here for the first paste
ActiveSheet.Paste
Else
real = lastrow2 + 1 ' if the sheet was already posted, we want
all other pastes to follow this rule here
Range("A" & real).Select
ActiveSheet.Paste
End If
Application.Windows(wsOpenName).Activate
Application.CutCopyMode = False ' clear the clipboard, otherwise a
window will popup asking if clipboard contents should be saved
ActiveWorkbook.Close SaveChanges:=False ' close the Import
Excel Workbook
End With
Next Value2
With wsNew
Cells.EntireColumn.AutoFit
End With
Application.ScreenUpdating = True
End Sub