VBA delete Excel columns in multiple workbooks from an Access Button

shieldsco
shieldsco used Ask the Experts™
on
I using the code below to delete columns in Excel Workbooks however I get an error message:


Error
Set wkb = oXL.Workbooks.Open("\\hhhfs02.itsc.hhs-itsc.local\financedata\Policy Data Call Database\Import Files\*.xlsx")


Dim oXL As Object
Dim wkb As Object
Dim wks As Object
Set oXL = CreateObject("excel.application")
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, strBrowseMsg As String



strBrowseMsg = "Select the folder that contains the Data Call EXCEL files:"

strPath = BrowseFolder(strBrowseMsg)

If strPath = "" Then
       MsgBox "No folder was selected.", vbOK, "No Selection"
       Exit Sub
 End If




 strFile = Dir(strPath & "\*.xls*")
 Do While Len(strFile) > 0
 strPathFile = strPath & "\" & strFile
       
       
          
              
Set wkb = oXL.Workbooks.Open("\\hhhfs02.itsc.hhs-itsc.local\financedata\Policy Data Call Database\Import Files\*.xlsx")
wkb.Worksheets("Data Call").Range("AC:HFD").Delete
wkb.Close True
oXL.Quit
Set oXL = Nothing

       
    

 strFile = Dir()
 Loop
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Does the same issue occur if you move these two lines after the Loop statement?

oXL.Quit
Set oXL = Nothing

i.e.

wkb.Close True

 strFile = Dir()
 Loop

oXL.Quit
Set oXL = Nothing

Open in new window

Author

Commented:
Corrected that error but now there in line:
Set wkb = oXL.Workbooks.Open("\\hhhfs02.itsc.hhs-itsc.local\financedata\Policy Data Call Database\Import Files\*.xlsx")


run-time error 1004 unable to find file
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
instead of looking for the file and doing something with it in the same loop, load file names into an array and then process them. This code is designed to loop through files and then open them -- but instead of opening, you can do something else
Sub LoopFilesAndOpen( _
   psPath As String _
   , Optional psMask As String = "*.*")
'read files into array and open each one
's4p
   'PARAMETERS
   '  psPath is path to look in
   '  psMask is what to look for (ie: *.jpg)

  Dim psPathFile As String _
     , sFilename As String _
     , i As Integer

   Dim arrFile() As String
   
   psPath = Trim(psPath)
   If Right(psPath, 1) <> "\" Then
      psPath = psPath & "\"
   End If
   
   'first array element will be 0
   i = -1
   sFilename = Dir(psPath & psMask)
   
   'load files matching mask into an array
   Do While sFilename <> ""
      If (GetAttr(psPath & "\" & sFilename) And vbDirectory) <> vbDirectory Then
         i = i + 1
         'redimension array and preserve previous values
         ReDim Preserve arrFile(i)
         'assign filename to array element
         arrFile(i) = sFilename
      End If
       'get next filename
      sFilename = Dir()
   Loop
   
   'open all the files
   If Not UBound(arrFile) >= 0 Then
      'No Files
      Exit Sub
   End If

   'loop through specified files and open
   For i = LBound(arrFile) To UBound(arrFile)
      psPathFile = psPath & arrFile(i)
      Application.FollowHyperlink psPathFile
   Next i

End Sub

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Corrected that error but now there in line:
Set wkb = oXL.Workbooks.Open("\\hhhfs02.itsc.hhs-itsc.local\financedata\Policy Data Call Database\Import Files\*.xlsx")


run-time error 1004 unable to find file

OK, guessing what your code is supposed to do, please replace that line with this one:

Set wkb = oXL.Workbooks.Open(strPathFile)
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
you may need to set defaults. File, Options, Trust Center, Trust Center Settings... command button, Trusted Locations:
check --> Allow Trusted Locations on my network
Excel Allow Trusted Locations on my network
is this code running from Access? oXl is set but never dimensioned.  Add:
Option Explicit

Open in new window

to the top of the code in the compiler directives.  This will force you to DIMension every variable that is used and also check more when it compiles.
dim oXL as object

Open in new window

and do this:
oXl.quit
and
Set oXL = Nothing
AFTER the loop, not in it. Also, before that, release the worksheet object then the workbook object (first save then close and release)

Author

Commented:
Error in line :
Set wkb = oXL.Workbooks.Open(strFile)

Open in new window



Error

Author

Commented:
Dim oXL As Object
Dim wkb As Object
Dim wks As Object
Set oXL = CreateObject("excel.application")
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, strBrowseMsg As String




strBrowseMsg = "Select the folder that contains the Data Call EXCEL files:"

strPath = BrowseFolder(strBrowseMsg)

If strPath = "" Then
       MsgBox "No folder was selected.", vbOK, "No Selection"
       Exit Sub
 End If




 strFile = Dir(strPath & "\*.xls*")
 Do While Len(strFile) > 0
 strPathFile = strPath & "\" & strFile
       
       
          
              
Set wkb = oXL.Workbooks.Open(strFile)
wkb.Worksheets("Data Call").Range("AC:HFD").Delete
wkb.Close True

       
    

 strFile = Dir()
 Loop
 oXL.Quit
Set oXL = Nothing

Open in new window

Author

Commented:
I'm getting a runtime error 9 subscript out of range in line:

wkb.Worksheets("Data Call").Range("AC:HFD").Delete

Open in new window


Here is the full code:

Dim oXL As Object
Dim wkb As Object
Dim wks As Object
Set oXL = CreateObject("excel.application")
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, strBrowseMsg As String




strBrowseMsg = "Select the folder that contains the Data Call EXCEL files:"

strPath = BrowseFolder(strBrowseMsg)

If strPath = "" Then
       MsgBox "No folder was selected.", vbOK, "No Selection"
       Exit Sub
 End If




 strFile = Dir(strPath & "\*.xls*")
 Do While Len(strFile) > 0
 strPathFile = strPath & "\" & strFile
       
       
          
              
Set wkb = oXL.Workbooks.Open(strPathFile)
wkb.Worksheets("Data Call").Range("AC:HFD").Delete
wkb.Close True
       
    

 strFile = Dir()
 
 Loop
 
 oXL.Quit
Set oXL = Nothing

Open in new window

This thread is going off at a tangent!

shieldsco: In response to ID: 41808634 please re-read, then correct your code to match what I provided in ID: 41808028.

Author

Commented:
Please refer to code in 41810801 :

Set wkb = oXL.Workbooks.Open(strPathFile)
wkb.Worksheets("Data Call").Range("AC:HFD").Delete
wkb.Close True

Open in new window

crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015
Commented:
>subscript out of range in line:

>wkb.Worksheets("Data Call").Range("AC:HFD").Delete

I suspect the workbook being opened does not have a sheet with that name -- or wkb is not actually set.  Earlier I suggested first loading filenames into an array.

Add an error handler to your code.  EE video:

1. basic error handling code for VBA (3:48)
http://www.experts-exchange.com/videos/1478/Excel-Error-Handling-Part-1-Basic-Concepts.html
I'm getting a runtime error 9 subscript out of range in line:

wkb.Worksheets("Data Call").Range("AC:HFD").Delete

The worksheet "Data Call" does not exist in the filename currently set within the wkb workbook object.
Figured it out... no array necessary
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
glad you got it
Close Request Pending

shieldsco requested that this question be closed on 9/26/2016, as follows:

    shieldsco's comment #a41808643 (0 points)
    crystal (strive4peace) - Microsoft MVP, Access's comment #a41808042 (500 points)

For the following reason:

Thanks

To cancel this request, state your reason as a comment and click the 'Object' button.

Yes, I object.

I offered continued assistance (without the need to rewrite the code using an array-based approach, as the question asker stated was not required) but the eventual points were allocated to a comment that does not seem to address the (initial) question.

Author

Commented:
What's the objection .... you received 500 points... you tried pushing me towards what you knew not what I wanted. My solution works great for me with some help from you.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
actually, I am the one who suggested an array, what you did not want to do.  I also brought up defaults since you were working with a network file, and pointed out errors such as moving code to quit and release out of the loop.  [ fanpages ] also pointed out errors, along with what to do to make them right, and should get points too.  We are all here to help and freely give you our time. While it is great you figured it out on your own, our ideas helped you get there. Hopefully, being there for you also helped alleviate some of your frustration. It is always frustrating when things don't work! ... and nice that others are there. Awarding points, and saying thank you, is your way of appreciating the time we spend to help you as best we can. Points should be split between all those who gave you helpful advice.

Author

Commented:
My apologies you two should split the points.... 250 each. Administrator, please adjust accordingly. Thanks

Author

Commented:
It was the best solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial