简体   繁体   English

Excel VBA从数组获取单元格位置

[英]Excel vba get cell position from array

在此处输入图片说明

Hi 你好

I want to loop through a source range and pickup the actual cell position from the source array, and apply conditional formating on a destination range, with each cell referencing the unique cell in source range. 我想遍历源范围并从源数组中提取实际单元格位置,并在目标范围上应用条件格式,每个单元格都引用源范围中的唯一单元格。 Plase see the picture for description. 请看图片进行说明。

So instead of getting the value in "A2" (for instance "1") from range "A2:C3". 因此,不是从范围“ A2:C3”中获取“ A2”中的值(例如“ 1”)。 Id' like to get the positional excel value of "Sheet1!A2" ..B2 etc. Id'想获取“ Sheet1!A2” .. B2等的位置excel值。

' High level approach '高级方法

Sub setcondformat()

Dim sourcearr ??, Dim destarr ??, Dim Strpos as String

Dim Strvalue as String ' not necessary, but interesting for educational purposes

For each cell in Source Array do

 select next cell in destination Array    ' eg Sheet2!"J2", "K2" etc ..

  Set conditional format value = Strpos     ' in cond format dialog eg "A2", "B2" etc..

   Next cell sourcearr

end loop


End sub

So if you all you are really trying to do is return the address of the cell you are currently on in your For Loop, you can just write something like: 因此,如果您真正想做的就是返回您当前在For循环中所在单元格的地址,则可以编写如下内容:

For Each cell In SourceArray
    MsgBox cell.Address
Next

Obviously you would want to store the address as a variable, or into an array to use the address later. 显然,您希望将地址存储为变量,或存储到数组中以供以后使用该地址。 This will return $A$1 if the cell is currently looping through cell A1 . 如果单元当前正在通过单元A1 循环,则将返回$ A $ 1。 If you were calling the cells value, as mentioned in your questions, you would use cell.value to return the actual visible value in the cell. 如您的问题中所述,如果您要调用单元格值,则可以使用cell.value返回单元格中的实际可见值。 If this is not what you are after, please let me know and I will attempt to adjust my answer. 如果这不是您想要的,请告诉我,我将尝试调整答案。

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

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