简体   繁体   English

MS Excel VBA-遍历行和列(如果为空则跳过)

[英]MS Excel VBA - Loop Through Rows and Columns (Skip if Null)

Hello stackoverflow community, 您好stackoverflow社区,

I must confess I primarily code within MS Access and have very limited experience of MS Excel VBA. 我必须承认我主要是在MS Access中编写代码,并且对MS Excel VBA的经验非常有限。

My current objective is this, I have an expense report being sent to me with deductions in another countries currency, this report has many columns with different account names that may be populated or may be null. 我当前的目标是,我收到了一份费用报告,其中扣除了其他国家/地区的货币,该报告中有许多列,其中包含不同的帐户名,可能填充或为空。

I currently have a Macro that will open an input box and ask for the HostCurrency/USD Exchange rate, my next step will be to start at on the first record (Row 14; Column AK contains personal info regarding the deduction) then skip to the first deduction account (deduction accounts start at column L and span to column DG) checking if each cell is null, if it is then keep moving right, if it contains a value then I want to multiply that value by my FX rate variable that was entered in the input box, and update the cell with the converion. 我目前有一个宏,它将打开一个输入框并询问HostCurrency / USD汇率,我的下一步将是从第一条记录开始(第14行; AK列包含有关扣除的个人信息),然后跳到第一个扣除账户(扣除账户从L列开始,到DG列)检查每个单元格是否为空,然后继续向右移动,如果它包含一个值,那么我想将该值乘以我的FX汇率变量在输入框中输入,然后使用转换功能更新单元格。 Once the last column (DG) has been executed I want to move to the next row (row 15) and start the process again all the way until the "LastRow" in my "Used Range". 一旦执行了最后一列(DG),我想移至下一行(第15行),并再次开始该过程,直到“使用范围”中的“ LastRow”。

I greatly appreciate any feedback, explanations, or links that may point me towards my goal. 我非常感谢任何可能将我引向目标的反馈,解释或链接。 Thank you in advance for taking the time to read though this! 预先感谢您抽出宝贵的时间阅读本文!

First off, you really should attempt to write the code yourself and post what you have so someone can try to point you in the right direction. 首先,您确实应该尝试自己编写代码并发布所拥有的内容,以便有人可以尝试为您指明正确的方向。 If your range is going to be static this is a very easy problem. 如果您的范围是静态的,这是一个非常简单的问题。 You can try something along the lines of: 您可以尝试以下方法:

Sub calcRate(rate As Double, lastrow As Integer)
    Dim rng As Range
    Set rng = Range("L14" & ":DG" & lastrow)

    Dim c As Variant
    For Each c In rng
        If c.Value <> "" Then
            c.Value = c.Value * rate
        End If
    Next

 End Sub

This code will step through each cell in the given range and apply the code without the need for multiple loops. 该代码将逐步遍历给定范围内的每个单元,并应用该代码,而无需多个循环。 Now you can call the calcRate sub from your form where you input the rate and lastrow . 现在,您可以从表单中调用calcRate子级,在其中输入ratelastrow

This will do it without looping. 这样就不会循环。

Sub fooooo()
Dim rng As Range
Dim mlt As Double
Dim lstRow As Long
mlt = InputBox("Rate")

With ActiveSheet
    lstRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    Set rng = .Range(.Cells(14, 12), Cells(lstRow, 111))
    rng.Value = .Evaluate("IF(" & rng.Address & " <>""""," & rng.Address & "*" & mlt & ","""")")
End With
End Sub

If your sheet is static you can replace ActiveSheet with WorkSheets("YourSheetName") . 如果工作表是静态的,则可以将ActiveSheet替换为WorkSheets("YourSheetName") Change "YourSheetName" to the name of the sheet. "YourSheetName"更改为工作表的名称。

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

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