首页 > 解决方案 > For loop in excel vba not showing values to cell

问题描述

I'm trying to use "for loop" to get some specific values, code runs perfect but values do not print to destination. subcode, where the problem is, looks like this :

lastRow = Range("H" & Rows.Count).End(xlUp).Row
For i = 5 To lastRow

    radius = Range("C" & i).Value
    spacing = Range("L" & i).Value

    Select Case radius
      Case 0 To 450
        spacing = 6
      Case 451 To 750
        spacing = 9
      Case 751 To 2000
        spacing = 18
    End Select

Next i

标签: excelvbafor-loop

解决方案


In your code you are storing a value from Range("L" & i).Value to the variable spacing. Then, in your For loop you are assigning a new value to the variable, but you are not setting the range (cell) to that value.

The below, albeit shoddy, will work for you.

lastRow = Range("H" & Rows.Count).End(xlUp).Row
For i = 5 To lastRow

    radius = Range("C" & i).Value

    Select Case radius
      Case 0 To 450
        Range("L" & i).Value = 6
      Case 451 To 750
        Range("L" & i).Value = 9
      Case 751 To 2000
        Range("L" & i).Value = 18
    End Select

Next i

推荐阅读