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
現貨!去的路!自信助你一臂之力!除了「Cells.EntireColumn.Autofit」仍然奇怪的是什麼都沒有做,但其餘的完美,所以沒關係!當然,我應該把wr作爲一個變量!明顯!你知道,有時候感覺我的思想在縮小,而不是在增長。 – thedoorbehindyourmind
嘗試指定這樣的工作表: wr.Cells.EntireColumn.AutoFit 如果未指定工作表,則處理當前工作表。 – mielk
完美!謝謝! – thedoorbehindyourmind