问题
In my excel sheet I'm using formulas which referring to external sheets.
Instead of manually changing the value in cell E7
(where I insert name of external sheet), I want to write an macro to open all of external sheets, change reference value in E7
and copy the generated values.
Unfortunately my code does't work - Excel can't "see" the values in the external sheet. What should I change?
Sub lista_plik()
Dim oExcel As Excel.Application
Dim oWB As Workbook
Set oExcel = New Excel.Application
folder = "path_here"
folder2 = folder & "*.xlsx"
arkusz = Dir(folder2)
Do While arkusz <> ""
x = x + 1
Range("E7").Value = Replace(arkusz, ".xlsx", "")
arkusz = folder & arkusz
Set oWB = oExcel.Workbooks.Open(arkusz)
''''''''''''''''''''
HERE CODE TO COPY VALUES
'''''''''''''''''''''''
oWB.Close
arkusz = Dir
Loop
End Sub
回答1:
I agree with brettdj, it is difficult to know what you are trying to achieve.
Chris has fixed two of your errors but without explaining the second correction which I suspect is the cause of the problem.
In your code you use Range("E7").Value
. This refers to cell E7 within the active sheet of the active workbook. Chris has changed this to oWS.Range("E7").Value
, which is an improvement, but oWS has not been initialised so its not enough.
My experience of working with multiple workbooks is that you must keep total control. I suspect your error is that Excel is looking at the wrong E7. The steps below will eliminate that and any similar errors.
Step 1
Make sure there is only one workbook open at the beginning by checking that WorkBooks.Count = 1
. If you permit two or more workbooks being open, you will need code to check that the active workbook is the one you want. So something like:
If WorkBooks.Count > 1 Then
Call MsgBox("Please close other workbooks then try again.", vbYesOnly)
Exit Sub
End If
Step 2
Before you open any other workbook, record which is the master workbook.
Dim WBkMaster As WorkBook
WBkMaster = ActiveWorkBook
Step 3
Always refer to workbooks and worksheets explicitly. For example:
With WBkMaster
With Sheets("x1")
.Range("E7").Value = ...
' Extract data from Sheet x1 to variables here
End With
With Sheets("x2")
' Extract data from Sheet x2 to variables here
End With
End With
Set WBkDestination = Application.Workbooks.Open(arkusz)
With WBkDestination
With Sheets("x3")
' Save data for Sheet x3 from variables here
End With
With Sheets("x4")
' Save data for Sheet x4 from variables here
End With
. Close
End With
oWB = Nothing ' Ensure no reference to closed workbook
There are other approaches to copying data between worksheets and workbooks but you do not give enough information to recommend which approach would be the most appropriate. The key issue is that you must be totally explicit about which sheet and which workbook you want addressed.
回答2:
If you are running this in VBA you don't need to declare an instance of Excwl
. Change to:
Sub lista_plik()
Dim oWB As Workbook
Dim oWs As Worksheet
Set oWs = ActiveSheet
folder = "path_here\"
folder2 = folder & "*.xlsx"
arkusz = Dir(folder2)
Do While arkusz <> ""
x = x + 1
oWs.Range("E7").Value = Replace(arkusz, ".xlsx", "")
arkusz = folder & arkusz
Set oWB = Application.Workbooks.Open(arkusz)
''''''''''''''''''''
'HERE CODE TO COPY VALUES
'''''''''''''''''''''''
oWB.Close
arkusz = Dir
Loop
End Sub
Note: I'm not entierly sure which workbooks cell E7
you are refering to, here I assume its in the active workbook.
来源:https://stackoverflow.com/questions/8787612/excel-files-loop