各位神人:
小弟想要讓sheets 中如果有內文可以自動調整適當的列高血了一巨集:
Dim i As Integer
Columns("N:R").Select
Selection.Delete Shift:=xlToLeft
i = 6
If Sheets("Failure report").Cells(i, 2).Value <> "" Then
Sheets("Failure report").Rows("i:i").AutoFit
i = i + 1
End If
但是一執行就會出現執行階段錯誤 "1004"
請各位大大幫忙診斷,感恩不盡
關閉廣告
realgodjoseph wrote:
各位神人: (恕刪)
Sheets("Failure report").Rows("i:i").AutoFit
這一句,當中的 i ,應該是變數(執行時等於6),而不是固定字元「 i」? ......可是你用了"i" (前後雙引號把i包起來),它就是固定字元i囉。
那執行該句時,就是說:把i列的列高自動調整一下.........問題是,excel只有1,2,3...6,7.....數字列,哪來的i列?.....所以就出現錯誤訊息囉(我想應該是這樣)。
或者你可以試試把該改成:Sheets("Failure report").Rows(i).AutoFit (去掉i的前後引號)。
~~~~~~~~
但是,你在調整列時,用了
i = 6
If Sheets("Failure report").Cells(i, 2).Value <> "" Then
Sheets("Failure report").Rows(i).AutoFit
i = i + 1
End If
看起來是要執行迴圈(i=i+1)?但沒有迴圈的語法,所以也不會執行迴圈,就只單純調整第6行(因為i=6)
或者你可以改成:
Sub autofit()
Dim i As Integer
Columns("N:R").Select
Selection.Delete Shift:=xlToLeft
i = 6 '從第6行開始
Do While i < 1000 '檢查到999行
If Sheets("Failure report").Cells(i, 2).Value <> "" Then 'B欄有資料才調整
Sheets("Failure report").Rows(i).autofit
End If
i = i + 1
Loop
MsgBox "調整完成!"
End Sub
給你參考。
Der,misser1