简体   繁体   English

基于单元格值的范围

[英]Range based on cell value

I need code to change the range based on a cell value: 我需要代码根据单元格值更改范围:

I can get it to work where the row number is dependent on the cell value as shown below, but I need the column value to be variable: 我可以在行号取决于单元格值的情况下使其工作,如下所示,但我需要将列值可变:

For Nassets = 1 To ws_data.Range("d2")
ws_data.Range("B" & Nassets).Value = 3
Next Nassets

If "d2" have the value 4 , range B1:B4 = 3 , however I want range B4:E4 = 3 如果"d2"的值为4 ,则范围B1:B4 = 3 ,但是我希望范围B4:E4 = 3

Thanks in advance! 提前致谢!

Based on the comments below, it look like you need to change the code so that the value in D2 represents the column within your loop, not the row - In which case: 根据以下注释,您似乎需要更改代码,以便D2中的值表示循环中的 ,而不是 -在这种情况下:

For Nassets = 1 To ws_data.Range("d2")
    ws_data.Cells(4, Nassets).Value = 3 '// Where 4 is the row number
Next Nassets

This can be re-written to exlude the loop altogether like so: 可以将其重写为完全排除循环,如下所示:

ws_data.Range(Cells(4, 2), Cells(4, ws_data.Range("D2").Value)).Value = 3

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

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