简体   繁体   中英

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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