下面是根據你的代碼有幾個MODS的演示指世行和牀單子,加入數據和圖表
Sub zx()
Dim file_path As String
Dim excel_title As String
Dim second_excel_title As String
Dim wb As Workbook
Dim sh1excel As Worksheet, sh2excel As Worksheet
Dim cht1 As Chart, cht2 As Chart
Dim i As Long, j As Long
Set sh1excel = ThisWorkbook.ActiveSheet
file_path = "C:\Users\Chris\Documents\"
excel_title = "test_info"
second_excel_title = "test_calculation"
Set wb = Workbooks.Open(file_path & excel_title)
Set sh2excel = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
wb.Activate
sh2excel.Activate
' Put some data in test_info at C3:D5
For i = 0 To 2
For j = 0 To 2
sh2excel.Cells(i + 3, j + 3) = i * 3 + j + 1
Next j, i
' Calculate from test_info into second_excel_title
For i = 0 To 2
For j = 0 To 2
sh1excel.Cells(i + 3, j + 3) = sh2excel.Cells(i + 3, j + 3)^2
Next j, i
' or put it as a formula
For i = 0 To 2
For j = 0 To 2
sh1excel.Cells(i + 3, j + 3) = "=[" & wb.Name & "]" & sh2excel.Name & "!" & sh2excel.Cells(i + 3, j + 3).Address & "^2"
Next j, i
ThisWorkbook.Activate
'Add a chart into second_excel_title
Set cht1 = ThisWorkbook.Charts.Add
cht1.Activate
cht1.Name = "DatafromThisBook"
Do While cht1.SeriesCollection.Count > 0
cht1.SeriesCollection.Item(1).Delete
Loop
cht1.SetSourceData Source:=sh1excel.Range("C3:E5")
Set cht2 = ThisWorkbook.Charts.Add
cht2.Activate
cht2.Name = "DatafromOtherBook"
Do While cht2.SeriesCollection.Count > 0
cht2.SeriesCollection(1).Delete
Loop
cht2.SetSourceData Source:=sh2excel.Range("C3:E5")
End Sub
我實際上最終誤解了我的老闆想要的東西,但這對我仍然有很大的幫助。非常感謝你 – 2011-02-23 21:41:08