vba - 遍历填充的行,根据条件匹配,使用 VBA 将值添加到 Excel 中另一个工作表中的特定单元格

标签 vba excel loops if-statement

我正在尝试在选项卡时间的列小时中选择一个值,并将其放在选项卡月份的相应列中。我想遍历工作表时间中的每一行并将所有数据添加到选项卡月份。

对于 Tab TIME 中的每一行,根据 Engagement Number、Engagement Phase、Staff Level 将 Hours 添加到 Tab Month 中的适当参与中。我每个月手动执行此操作。我创建了上个月选项卡的副本并每个月添加到该选项卡。大多数列都有这样的单元格:=1+3+6+4+14+5+2+5,这是参与该事件的时间。对于其中包含某些内容的单元格,我只想添加那里的内容。对于其中没有任何内容的单元格,我想设置新值:= 1 如果值为 1。我想自动化这个,因为它每个月的第一天都需要几天,这个恰好在一个周末所以猜猜我会做什么。 :)

这就是我所在的地方。

Sub Recon()

'Macro to add time to reconciliation report


'Declare variables

Dim Last_Row_TIME As Double
Dim Last_Row_MONTH As Double
Dim wb As Workbook
Dim ws As Worksheet
Dim rw As Range
Dim wstime As

Set wb = ActiveWorkbook
Set wstime = Sheets("TIME")
Set wsmonth = Sheets("MONTH")

wstime.Select

'Find the last non-blank cell in column A(1)
Last_Row_wstime = wstime.Cells(Rows.Count, 1).End(xlUp).Row

'Find the first blank cell in column A
First_Empty_Row_wstime = Last_Row_wstime + 1

I_Col = 1

For i = 1 To Last_Row_wstime

For Each rw In wstime.Rows

If wstime.Cells(rw.Row,1).Value = wsmonth.Cells(rw.Row, 3).Value
And

End Sub

我被困在 if 语句中,不知道如何使它工作。
IF 
TIME.Eng. No. (Column A) = Month.Eng. No (Column B)
AND
TIME.Eng. Phase (Column C)  = Month.Eng.Phase (Column C)
AND
TIME.Staff Level (Column M) = PARTNER or MANAGING DIRECTOR

THEN

Add Value TIME.Hours (Column Y) to  Month.Partner/MD (Column I) 

'If blank then "=TIME.Hours) elseif add to the previous value "previous     addition statement +(hours)"


ELSEIF

TIME.Eng. No. (Column A) = Month.Eng. No (Column B)
AND
TIME.Eng. Phase (Column C)  = Month.Eng.Phase (Column C)
AND
TIME.Staff Level (Column M) = SR. MANAGER/DIRECTOR

THEN 

Add Value TIME.Hours (Column Y) to  Month.Director (Column J)

ELSEIF

TIME.Eng. No. (Column A) = Month.Eng. No (Column B)
AND
TIME.Eng. Phase (Column C)  = Month.Eng.Phase (Column C)
AND
TIME.Staff Level (Column M) = MANAGER

THEN

Add Value TIME.Hours (Column Y) to  Month.Manager (Column K)

ELSEIF

TIME.Eng. No. (Column A) = Month.Eng. No (Column B)
AND
TIME.Eng. Phase (Column C)  = Month.Eng.Phase (Column C)
AND
TIME.Staff Level (Column M) = SENIOR ASSOCIATE

THEN

Add Value TIME.Hours (Column Y) to  Month.Sr.Assoc (Column L)

ELSEIF

TIME.Eng. No. (Column A) = Month.Eng. No (Column B)
AND
TIME.Eng. Phase (Column C)  = Month.Eng.Phase (Column C)
AND
TIME.Staff Level (Column M) = ASSOCIATE

THEN

Add Value TIME.Hours (Column Y) to  Month.Associate (Column M)

ELSEIF

'Create a new line after the biggest primary key, located in Month.Primary Key (Column A) 

TIME.Eng. No. (Column A) = Month.Eng. No (Column B)
AND
TIME.Eng. Phase (Column C)  = Month.Eng.Phase (Column C)
AND
TIME.Eng. Description (Column B) = Month.Project Name (Column D)


End If

最佳答案

这是您做出的非常勇敢的努力。我很乐意帮助你,但目前我不明白你想要什么。据我了解,我编写的代码如下。

Sub Recon()
    ' Macro to add time to reconciliation report
    ' 01 Apr 2017
    
    Dim Wb As Workbook
    Dim WsTime As Worksheet, WsMonth As Worksheet
    Dim Crit As String                          ' Search criterium
    Dim Rl As Long                              ' last row (Time)
    Dim R As Long                               ' row counter (Time)
    
    With WsTime
        Rl = .Cells(.Rows.Count, 1).End(xlUp).Row
        For R = 2 To Rl                         ' skip captions row
            Crit = .Cells(R, 1).Value
            Debug.Print Crit                    ' print value to Immediate Window (for testing)
        Next R
    End With
End Sub
事实上,这段代码比我的理解更深入,因为它假定两个工作表都在同一个工作簿中。它假定包含这两个工作表的工作簿将是事件工作簿。这意味着代码可能在另一个工作簿中,也许是您保留的一个,因为其中有代码。
现在,以比您可能想要的更小的步骤进行,上面的代码循环遍历 Time 工作表中的所有行并在 A 列中找到值。为了让您查看它是否有效,它将这些值打印到即时窗口( Ctrl+G,如果它没有显示在您的 VBE 窗口中)。
这就是它变得模糊的地方:我假设这个值必须是名称,也许,您现在需要在月表中找到相同的名称。为此,您需要知道月表中可能找到该名称的列,如果找到该名称该怎么办,以及如果找不到该名称该怎么办。

关于vba - 遍历填充的行,根据条件匹配,使用 VBA 将值添加到 Excel 中另一个工作表中的特定单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43150835/

相关文章:

vba - 使用 VBA 将公式分配给 Excel 单元格

arrays - 从数组中提取工作表名称

python - 检查Python字典值是否在另一个字典的值中

python - 从两个 DataFrame 列加载信息的函数

java - 添加循环结果

excel - vba 脚本的替代公式

VBA:从本地驱动器名称转换为网络驱动器名称

excel - 如果单元格内容与列表值不匹配,则删除工作表

excel - 如果名称等于不同工作表上的字符串,则将值加 1

excel - 使用 VBA 将数据透视表设置为基于另一个字段的特定日期