试图复制工作表时超出范围错误
整个macros应从一个站点打开一个文件,然后复制一个特定的工作表并粘贴到另一个工作簿。 到目前为止,macros已打开,但未能复制并粘贴抛出“超出范围的错误”错误。 我正在修复该错误或打开工作簿,然后删除额外的表,我不想看到。 任何帮助将不胜感激!
Public Sub OpenFileFromWeb() ' Define Workbook and Worksheet Variables Dim wkbMyWorkbook As Workbook Dim wkbWebWorkbook As Workbook Dim wksWebWorkSheet As Worksheet Set wkbMyWorkbook = ActiveWorkbook ' Open The Web Workbook Workbooks.Open ("http://path.path.com/path/site/path/test_Data.xls") Sheets("Sheet3").Select ' Set the Web Workbook and Worksheet Variables Set wkbWebWorkbook = ActiveWorkbook Set wksWebWorkSheet = ActiveSheet ' Choose Sheet to pull Data from Range("A1").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy ' Copy The Web Worksheet To My Workbook and Rename wksWebWorkSheet.Copy After:=wkbMyWorkbook.Sheets(Sheets.Count)'-->error occurs here wkbMyWorkbook.Sheets(ActiveSheet.Name).Name = "MyNewWebSheet" ' Close the Web Workbook wkbMyWorkbook.Activate wkbWebWorkbook.Close End Sub
给这个一个镜头
Public Sub OpenFileFromWeb() Dim wkbMyWorkbook As Workbook Dim wkbWebWorkbook As Workbook Set wkbMyWorkbook = ThisWorkbook Set wkbWebWorkbook = Workbooks.Open("http://path.path.com/path/site/path/test_Data.xls") With wkbMyWorkbook wkbWebWorkbook.Sheets("Sheet3").Copy _ After:=.Sheets(.Sheets.Count) .Sheets(.Sheets.Count).Name = "MyNewWebSheet" End With ' Close the Web Workbook wkbMyWorkbook.Activate wkbWebWorkbook.Close End Sub
您只需要确定在sheets.count
使用的工作簿。
我在这里时已经整理好了你的代码:
Public Sub OpenFileFromWeb() ' Define Workbook and Worksheet Variables Dim wkbMyWorkbook As Workbook Dim wkbWebWorkbook As Workbook Dim wksWebWorkSheet As Worksheet Set wkbMyWorkbook = ActiveWorkbook ' Open The Web Workbook and Set the Web Workbook and Worksheet Variables Set wkbWebWorkbook = Workbooks.Open("http://path.path.com/path/site/path/test_Data.xls") Set wksWebWorkSheet = wkbWebWorkbook.Sheets("Sheet3") ' Choose Sheet to pull Data from wksWebWorkSheet.Range("A1", Range("A1").End(xlToRight).End(xlDown)).Copy '''NOT SURE WHAT THIS IS FOR. DOESN'T SEEM TO BE REQUIRED! ' Copy The Web Worksheet To My Workbook and Rename wksWebWorkSheet.Copy after:=wkbMyWorkbook.Sheets(wkbMyWorkbook.Sheets.Count) wkbMyWorkbook.Sheets(ActiveSheet.Name).Name = "MyNewWebSheet" ' Close the Web Workbook wkbMyWorkbook.Activate wkbWebWorkbook.Close End Sub