基本VBA分为两列

问题描述

我是VBA的新手,也是StackOverflow的新手,只是想按照教程来掌握一切。 如果我有两列数字,即A列和B列,我想将A / B除以并将结果放入C中。我想使用for循环来这样做。到目前为止,我的代码是:

Sub ForLooptoDivide()

Dim i As Integer

 For i = 2 To 6

Cells(i,3).Value = Cells(1,i).Value / Cells(2,i).Value

Next i


End Sub

就像我说的那样,我对此是全新的,并且刚刚遇到了教程的障碍。

谢谢!

解决方法

您没有提到您的问题。有很多方法可以做到这一点。在sub之下,将在Column A中找到最后使用的单元格,然后迭代将Column A除以Column B并将结果放入Column C中。试试看...

Sub DivideColumns()
Dim Lastrow As Long
Dim i As Long

    Lastrow = Cells(Rows.Count,"A").End(xlUp).Row
    
    For i = 2 To Lastrow
        Cells(i,"C") = Cells(i,"A") / Cells(i,"B")
    Next i

End Sub

enter image description here