2013-03-27 80 views
1

我在查找錯誤時遇到了困難:我想要做的就是僅在Book1.xls的Sheet1上運行此代碼,即使在其他Excel文件或此文件的其他工作表中工作時也是如此。所有的代碼的第一部分工作正常,直到** -line但後來當我在不同的頁面或文件它「嗆」,並給我一個錯誤。如何使VBA代碼運行特定的Excel文件?

Sub Upload0() 

' Upload Webpage content 
Application.OnTime Now + TimeValue("00:00:10"), "Upload0" 
With Workbooks("Book1.xls").Sheets("Sheet1").QueryTables.Add(Connection:= _ 
    "URL;http://cetatenie.just.ro/ordine/articol-11", Destination:=Workbooks("Book1.xls").Sheets("Sheet1").Range("A1")) 
    .Name = "CetatenieOrdine" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = True 
    .BackgroundQuery = True 
    .RefreshStyle = xlOverwriteCells 
    .SavePassword = True 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .WebSelectionType = xlEntirePage 
    .WebFormatting = xlWebFormattingNone 
    .WebPreFormattedTextToColumns = True 
    .WebConsecutiveDelimitersAsOne = True 
    .WebSingleBlockTextImport = False 
    .WebDisableDateRecognition = False 
    .WebDisableRedirections = False 
    .Refresh BackgroundQuery:=False 
    End With 

' Deletes Empty Cells 
Workbooks("Book1.xls").Sheets("Sheet1").Range("A1").Columns("A:A").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp 

****************************************************************************** 

' Deletes useless Rows and fits the Width 
Rows("1:31").Select 
Selection.Delete Shift:=xlUp 
Range("B28").Select 
Selection.End(xlDown).Select 
Rows("17:309").Select 
Selection.Delete Shift:=xlUp 


' Text to Column function with auto-confirmation to overwrite 
Columns("A:A").Select 
Application.DisplayAlerts = False 
Selection.TextToColumns Destination:=Columns("A:A"), DataType:=xlDelimited, _ 
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ 
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ 
    :=":", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True 
Application.DisplayAlerts = True 

Columns("B:B").Select 
Application.DisplayAlerts = False 
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _ 
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _ 
    Semicolon:=False, Comma:=False, Space:=True, Other:=False, OtherChar _ 
    :=":", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _ 
    TrailingMinusNumbers:=True 
Application.DisplayAlerts = True 
Columns("B:B").Select 
Selection.Delete Shift:=xlToLeft 


' fit the Width of All Columns 
Cells.Select 
Range("A37").Activate 
Cells.EntireColumn.AutoFit 
Range("H1").Select 
Rows("1:1").Select 
Selection.Font.bold = True 

End Sub 

回答

4

當訪問RowsRange而不指定的片材,使用VBA ActiveSheet。在這種情況下,你應該明確地指定要使用工作表:

Sub Upload0() 

' Upload Webpage content 
Application.OnTime Now + TimeValue("00:00:10"), "Upload0" 
With Workbooks("Book1.xls").Sheets("Sheet1").QueryTables.Add(Connection:= _ 
    "URL;http://cetatenie.just.ro/ordine/articol-11", Destination:=Workbooks("Book1.xls").Sheets("Sheet1").Range("A1")) 
    .Name = "CetatenieOrdine" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = True 
    .BackgroundQuery = True 
    .RefreshStyle = xlOverwriteCells 
    .SavePassword = True 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .WebSelectionType = xlEntirePage 
    .WebFormatting = xlWebFormattingNone 
    .WebPreFormattedTextToColumns = True 
    .WebConsecutiveDelimitersAsOne = True 
    .WebSingleBlockTextImport = False 
    .WebDisableDateRecognition = False 
    .WebDisableRedirections = False 
    .Refresh BackgroundQuery:=False 
    End With 

' Deletes Empty Cells 
Workbooks("Book1.xls").Sheets("Sheet1").Range("A1").Columns("A:A").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp 

****************************************************************************** 
With Workbooks("Book1.xls").Sheets("Sheet1") 
    ' Deletes useless Rows and fits the Width 
    .Rows("1:31").Delete Shift:=xlUp 
    .Rows("17:309").Delete Shift:=xlUp 


    ' Text to Column function with auto-confirmation to overwrite 
    Application.DisplayAlerts = False 
    .Columns("A:A").TextToColumns Destination:=Columns("A:A"), DataType:=xlDelimited, _ 
      TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ 
      Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ 
      :=":", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True 
    .Columns("B:B").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _ 
      TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _ 
      Semicolon:=False, Comma:=False, Space:=True, Other:=False, OtherChar _ 
      :=":", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _ 
      TrailingMinusNumbers:=True 
    Application.DisplayAlerts = True 
    .Columns("B:B").Delete Shift:=xlToLeft 


    ' fit the Width of All Columns 
    .Cells.EntireColumn.AutoFit 
    .Rows("1:1").Font.bold = True 
End With 

End Sub 
+0

還有一個問題,例如用於simplcity我用用......結束與條款的部分只有你擁有了它上面仍然考慮Activesheet出於某種原因。任何線索? – maximladus 2013-03-27 10:57:16

+0

你怎麼知道的?它不會刪除Book1!Sheet1工作表上的選擇? – 2013-03-27 11:02:41

+0

好吧,如果我從開始到這個部分的代碼發佈並添加WITH clasue並且結束它,只有當我留在Sheet1上時,才能正確執行代碼,移動到其他頁面會導致代碼執行僅您的DELETING部分有以上沒有下載,粘貼....等。也許你可以試試,它對你有好處嗎? – maximladus 2013-03-27 11:07:59

相關問題