2015-07-10 54 views
0

我有這個非常令人沮喪的問題。下面的代碼應該從Access導出一個查詢到Excel,然後爲第一行加上顏色,加粗並自動填充整個工作表。對象變量或在初始工作後沒有設置塊變量

這需要多次發生,所以我只是簡單地複製粘貼所需次數和對昏暗名稱的小改動。不幸的是,它在第​​二輪之後停止工作,並給我錯誤「Object variable or With block variable not set」,然後突出顯示代碼的一部分,如下所示。我的意思是,在這一點上,我只是手動做這件事,所以它不是一個威脅生命的問題,但我很想弄清楚如何使這項工作。

謝謝,這裏是一個的重複多次的代碼:

Private Sub cmdFinal_Click() 
Dim fileLocation As String ' Main folder 
Dim finalExport As String 

fileLocation = "C:\MYDOCS\Latest\" 
finalExport = "FINAL_EXPORT_" & UCase(Format(DateAdd("m", -7, Date), "MMMyy")) & "-" & UCase(Format(DateAdd("m", -2, Date), "MMMyy")) 

' Export queries to Final Excel file 
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qryFINAL", fileLocation & finalExport True, finalExport 
     DoEvents 
     Me.ProgressBar.Visible = True 
     Me.ProgressBar.Width = 500 
     Me.Repaint 
     DoEvents 
    ' Open Excel file, apply colors, save, and quit 
    Set xl = CreateObject("Excel.Application") 
    Set wr = xl.Workbooks.Open(fileLocation & finalExport & ".XLSX") 
    Call ColorMe 
    DoEvents 
    wr.Save 
    wr.Close 
    xl.Quit 
    DoEvents 
    Set sh = Nothing 
    Set wr = Nothing 
    Set xl = Nothing 
    DoEvents 

Sub ColorMe() 
' 
' Format document and make it pretty 
' 
Set sh = wr.Worksheets(1) 

With sh 
    sh.Rows("1:1").EntireRow.Select 
    With Selection.Interior <----------Here's where the error occurs 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .ThemeColor = xlThemeColorDark1 
     .TintAndShade = -0.499984740745262 
     .PatternTintAndShade = 0 
    End With 
    With Selection.Font 
     .ThemeColor = xlThemeColorDark1 
     .TintAndShade = 0 
    End With 
End With 
    Selection.Font.Bold = True 
    Cells.Select 
    Cells.EntireColumn.AutoFit 
DoEvents 
End Sub 

回答

3

有幾件事情:

1)我看不到的地方的方法cmdFinal_Click()關閉 - 沒有

End Sub 

line。

2)您應該將變量wr作爲參數傳遞給ColorMe方法。否則,此方法不知道什麼工作簿的過程:

Sub ColorMe(wr As Excel.Workbook) 

Call ColorMe(wr) 

3)你不需要爲了格式化選擇範圍。試試以下代碼:

Sub ColorMe(wr As Excel.Workbook) 
    Dim sh As Worksheet 
    Dim rng As Excel.Range 


    Set sh = wr.Worksheets(1) 
    Set rng = sh.Rows(1) '<---- you create reference to the first row here 
          '  and later you should use this variable 
          '  instead of selecting cells in Worksheet and 
          '  use Selection. 

    With rng.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .ThemeColor = xlThemeColorDark1 
     .TintAndShade = -0.499984740745262 
     .PatternTintAndShade = 0 
    End With 
    With rng.Font 
     .ThemeColor = xlThemeColorDark1 
     .TintAndShade = 0 
     .Bold = True 
    End With 

    'Cells.Select   '<---- Commented. Can cause errors if worksheet is not active. 
    Cells.EntireColumn.AutoFit 

    DoEvents 

End Sub 
+0

現貨!去的路!自信助你一臂之力!除了「Cells.EntireColumn.Autofit」仍然奇怪的是什麼都沒有做,但其餘的完美,所以沒關係!當然,我應該把wr作爲一個變量!明顯!你知道,有時候感覺我的思想在縮小,而不是在增長。 – thedoorbehindyourmind

+1

嘗試指定這樣的工作表: wr.Cells.EntireColumn.AutoFit 如果未指定工作表,則處理當前工作表。 – mielk

+0

完美!謝謝! – thedoorbehindyourmind

相關問題