简体   繁体   English

VBA中的Excel宏-如何应用循环向下移动列

[英]Excel Macro in VBA - How to apply a loop to move down columns

I have a sub Macro that basically works as a currency converter that allows the user to define their own exchange rate values. 我有一个基本的子宏,可以用作货币转换器,使用户可以定义自己的汇率值。 I have tested it on one cell and works fine but I need it to loop and move down a row for a define range. 我已经在一个单元格上对其进行了测试,并且工作正常,但是我需要它循环并向下移动一行以定义范围。 (Currently the cells defined are L2, K2 and G", need to to repeat on L3, K3 and G3 until Ln, Kn and Gn etc.) (当前定义的单元格为L2,K2和G“,需要在L3,K3和G3上重复直到Ln,Kn和Gn等。)

Code is as follows: 代码如下:

Sub Macro9()
'
' Macro9 Macro
'

Dim score As String, result As String
score = Sheets("Paste Orders Here").Range("K2").Value

If score = "USD" Then
    result = Sheets("Paste Orders Here").Range("L2") * Sheets("Configuration").Range("C5")
ElseIf score = "EUR" Then
    result = Sheets("Paste Orders Here").Range("L2") * Sheets("Configuration").Range("B5")
ElseIf score = "GBP" Then
    result = Sheets("Paste Orders Here").Range("L2")
ElseIf score = "" Then
    result = Sheets("Paste Orders Here").Range("K2")
End If

Sheets("Brightpearl").Range("G2").Value = result
'
End Sub

Thanks, I understand this could be completely incorrect for wanting to apply this to multiple rows but I could really use some help! 谢谢,我知道这可能完全不正确,希望将其应用于多行,但我真的可以使用一些帮助!

Here is my interpretation. 这是我的解释。 I wasn't quite sure how far to fill the formulas down as you only referred to this as n so I used the bottom of Paste Orders Here 's column L. 我不确定将公式向下填充多远,因为您仅将其称为n,所以我使用了Paste Orders Here的L列的底部。

Sub Macro9()
    Dim r As Long, lr As Long
    With Sheets("Paste Orders Here")
        lr = .Cells(.Rows.Count, "K").End(xlUp).Row
        For r = 2 To lr
            Select Case UCase(.Cells(r, "K").Value)
                Case "USD"
                    Sheets("Brightpearl").Cells(r, "G") = _
                      CCur(.Cells(r, "L").Value * Sheets("Configuration").Range("C5").Value)
                Case "EUR"
                    Sheets("Brightpearl").Cells(r, "G") = _
                      CCur(.Cells(r, "L").Value * Sheets("Configuration").Range("B5").Value)
                Case "GBP"
                    Sheets("Brightpearl").Cells(r, "G") = CCur(.Cells(r, "L").Value)
                Case Else
                    Sheets("Brightpearl").Cells(r, "G") = .Cells(r, "K").Value
            End Select
        Next r
    End With
End Sub

I find a Select Case method works well in these cases and provides ease of future expandability. 我发现Select Case方法在这些情况下效果很好,并为将来的扩展提供了便利。

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

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