简体   繁体   English

从给定单元格excel-vba中查找表

[英]Find a table from a given cell excel-vba

I have a table which is located somewhere on a spreadsheet for example b3:f15 , and near it there is nothing ( empty cells ), I am trying to select this table and color it. 我有一个表,该表位于电子表格上的某个位置,例如b3:f15,并且在该表附近没有任何内容(空单元格),我试图选择该表并为其着色。 Somehow my code doesn't work and I just can't find my logic mistake 不知何故我的代码不起作用,我只是找不到逻辑错误

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

Any suggestions? 有什么建议么? Thanks in advance 提前致谢

Use: 采用:

temp.CurrentRegion.Interior.ColorIndex = 36

instead of looping. 而不是循环。

If your While/Wend loops are working, you can set the temp Range variable like this: 如果您的While/Wend循环正常工作,则可以这样设置temp Range变量:

Set temp = Range(Cells(i-1, k-1), Cells(r+1, p+1)) '<~ no address needed

That being said, you're already prompting the user for an input -- could you ask him or her to enter the top-left cell in the table? 话虽这么说,您已经在提示用户输入了-您是否可以要求他或她在表格中输入左上角的单元格? From that your code could be much simplified... 由此可以大大简化您的代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM