[英]Find a table from a given cell excel-vba
我有一個表,該表位於電子表格上的某個位置,例如b3:f15,並且在該表附近沒有任何內容(空單元格),我試圖選擇該表並為其着色。 不知何故我的代碼不起作用,我只是找不到邏輯錯誤
Sub colorize_table()
Dim temp As Range
Set temp = Application.InputBox(prompt:="Please choose cell", Type:=8)
Dim i As Integer, k As Integer
Dim r As Integer, p As Integer
i = temp.Row
k = temp.Column
r = temp.Row
p = temp.Column
While temp(i, k) <> ""
While temp(i, k) <> ""
i = i - 1
Wend
k = k - 1
Wend
While temp(r, p) <> ""
While temp(r, p) <> ""
r = r + 1
Wend
p = p + 1
Wend
Set temp = Range(Cells(i-1, k-1).Address, Cells(r+1, p+1).Address)
temp.Interior.ColorIndex = 36
End Sub
有什么建議么? 提前致謝
采用:
temp.CurrentRegion.Interior.ColorIndex = 36
而不是循環。
如果您的While/Wend
循環正常工作,則可以這樣設置temp
Range
變量:
Set temp = Range(Cells(i-1, k-1), Cells(r+1, p+1)) '<~ no address needed
話雖這么說,您已經在提示用戶輸入了-您是否可以要求他或她在表格中輸入左上角的單元格? 由此可以大大簡化您的代碼...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.