无法访问vba中的另一个工作表

时间:2012-12-08 04:53:14

标签: excel vba excel-vba

我试图访问位于同一文件夹中的另一个工作簿中存在的图表和图片以及图表。这是我的代码。

wbname = Worksheets("Sheet1").Range("A1:A1").Value
wsname = Worksheets("Sheet1").Range("A2:A2").Value
MsgBox wbname
wbname = ThisWorkbook.Path & "\" & wbname    
MsgBox "reached here"
MsgBox "HI"
MsgBox Workbooks("C:\charts\1.xls").Sheet("L1-XO PGT ScoreCard").Range("A1:A1").Value
Set aWorkSheet = Workbooks("C:\charts\1.xls").Sheet("L1-XO PGT ScoreCard")
sCurrentSheet = aWorkSheet.Name
MsgBox "Shapes count " + CStr(aWorkSheet.Shapes.Count)
For iIndex = 1 To aWorkSheet.Shapes.Count
Set aShape = aWorkSheet.Shapes(iIndex)
MyPicture = aShape.Name
MsgBox aShape.Name + " Name, " + Str(aShape.Type)
If Left(aShape.Name, 7) = "Picture" Then
With aShape
PicHeight = .Height
PicWidth = .Width
End With
Set aChart = ActiveWorkbook.Charts.Add
ActiveWorkbook.ActiveChart.Location Where:=xlLocationAsObject, Name:=sCurrentSheet
iShapeCount = aWorkSheet.Shapes.Count
Set aShapeChart = aWorkSheet.Shapes(iShapeCount)
MyChart = aShapeChart.Name      '"Chart " & Str(aWorkSheet.Shapes.Count)
aShapeChart.Width = PicWidth
aShapeChart.Height = PicHeight
sChartJpg = sChartJpg & aShapeChart.Name & ".jpg"
With aWorkSheet
aShape.Copy
With ActiveChart 'aChart
.ChartArea.Select
.Paste
End With
.ChartObjects(iIndex).Chart.Export Filename:=sChartJpg, FilterName:="jpg",                    Interactive:=True
aShapeChart.Cut
End With
Application.ScreenUpdating = True
MsgBox "Completed."
Exit Sub
End If
Next
MsgBox "Completed."Exit Sub
ErrHandler:
MsgBox "Error # " & CStr(Err.Number) & " " & Err.Description & " " & Err.Source
Err.Clear   ' Clear the error.
 End Sub

显示" HI"警告框,它直接显示已完成的测试框,它不显示TestBOX警报 " MsgBox工作簿(" C:\ charts \ 1.xls")。工作表(" L1-XO PGT ScoreCard")。范​​围(" A1:A1&# 34;。)值" ...也不是从工作簿中提取图像。可以请一些人帮助

1 个答案:

答案 0 :(得分:2)

您必须打开工作簿 - 大部分代码都无法在已关闭的工作簿上运行。