Excel VBA Looping formula to change Range

社会主义新天地 提交于 2019-12-23 04:50:33

问题


I've written a code to manipulate data in L9 : DC9, But now I need to repeat this for L10 : DC10, L11 : DC11 and etc.. I've tried a For Next loop replacing the value in the range with Li:DCi and specifying (i) as 9 to 30 but I get an error. How can I make a loop for this function?

My current version of Excel is 2013


回答1:


What you are looking for is a syntax like this

Sub LoopRows()
    Dim i As Integer
    For i = 9 To 30
        ActiveSheet.Range("L" & i & ":DC" & i).Interior.Color = RGB(100, 100, 100)
    Next i
End Sub

This example just formats the color of the cell in each row. Notice how I use the for-loop to create a looping range selection.




回答2:


I suggest using Range("L9").Resize(21,50).Interior.Color = .. to do it in one statement.



来源:https://stackoverflow.com/questions/31549152/excel-vba-looping-formula-to-change-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!