迭代填充的行,根据条件进行匹配,使用VBA向Excel中另一个工作表中的特定单元格添加值
我试图在tab Time的hours列中选择一个值,并将其放入tab Month的相应列中。我希望遍历sheet Time中的每一行,并将所有数据添加到tab Month 对于Tab TIME中的每一行,根据约定编号、约定阶段和员工级别,将小时数添加到Tab Month中的相应约定中。我每个月都手工做这个。我创建了“上个月”选项卡的副本,并每个月添加到该选项卡上。大多数列的单元格中都有这样的内容:=1+3+6+4+14+5+2+5,这是该约定的工作时间。对于含有某种物质的细胞,我只想补充一下。对于没有任何内容的单元格,我希望将新值设为:=1(如果值为1)。我想自动完成这项工作,因为每个月的第一天都要花上几天的时间,而这项工作恰好是在周末,所以猜猜我将要做什么。:) 这就是我所处的位置迭代填充的行,根据条件进行匹配,使用VBA向Excel中另一个工作表中的特定单元格添加值,vba,excel,loops,if-statement,Vba,Excel,Loops,If Statement,我试图在tab Time的hours列中选择一个值,并将其放入tab Month的相应列中。我希望遍历sheet Time中的每一行,并将所有数据添加到tab Month 对于Tab TIME中的每一行,根据约定编号、约定阶段和员工级别,将小时数添加到Tab Month中的相应约定中。我每个月都手工做这个。我创建了“上个月”选项卡的副本,并每个月添加到该选项卡上。大多数列的单元格中都有这样的内容:=1+3+6+4+14+5+2+5,这是该约定的工作时间。对于含有某种物质的细胞,我只想补充一下。对
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
事实上,这段代码超出了我的理解,因为它假定两个工作表位于同一工作簿中。它假定包含这两张工作表的工作簿将是活动工作簿。这意味着代码可能在另一个工作簿中,可能是因为代码在其中而保留的工作簿
现在,以比您可能希望的更小的步骤继续,上面的代码循环时间工作表中的所有行,并在A列中找到值。为了让您看到它是否有效,它会将这些值打印到即时窗口(Ctrl+G,如果它未显示在VBE窗口中)
这就是它变得模糊的地方:我假设这个值必须是name,也许,你现在需要在月报表中找到相同的名称。因此,人们需要知道月表中可能找到该姓名的列,如果找到该姓名该怎么办,如果找不到该姓名该怎么办。我就是这样理解您的问题的
- 您需要浏览时间工作表中的每一行
- 对于每一行,您希望在月份工作表中找到一个两列匹配的行。第一场比赛比较了一个月内的发动机编号(时间!A:A)和发动机编号!B:B和第二阶段(C列)的月匹配!C:C
- 如果找到两列匹配项,则在月份工作表中附加基本的求和公式(例如,=n+=n+=n+=n+=n…)和时间的当前值!Y:Y。这将进入本月的一个专栏!I:我要看工资情况及时调整!M:M
- 如果未找到匹配项,请在月份中创建一个新条目,并在新条目的相应列中填写时间
Option Explicit
Sub Recon()
Dim timeRow As Long, monthCol As Long, monthRow As Variant
Dim wsTime As Worksheet, wsMonth As Worksheet, wb As Workbook
Dim rw As Range
Set wb = ActiveWorkbook
Set wsTime = wb.Worksheets("TIME")
Set wsMonth = wb.Worksheets("MONTH")
With wsTime
For timeRow = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
'preload the next available row in MONTH
With wsMonth
monthRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
'evaluate for a two-column MATCH
Debug.Print "=MATCH(" & wsTime.Cells(timeRow, "A").Address(external:=True) & Chr(38) & wsTime.Cells(timeRow, "C").Address(external:=True) & ", " & wsMonth.Columns("B").Resize(monthRow, 1).Address(external:=True) & Chr(38) & wsMonth.Columns("C").Resize(monthRow, 1).Address(external:=True) & ", 0)"
monthRow = Application.Evaluate("MATCH(" & wsTime.Cells(timeRow, "A").Address(external:=True) & Chr(38) & wsTime.Cells(timeRow, "C").Address(external:=True) & ", " & wsMonth.Columns("B").Resize(monthRow, 1).Address(external:=True) & Chr(38) & wsMonth.Columns("C").Resize(monthRow, 1).Address(external:=True) & ", 0)")
'if there is no two-column matching record, create one
If IsError(monthRow) Then
With wsMonth
monthRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(monthRow, "A") = Application.Max(.Columns("A")) + 1 'you need something in MONTH!A:A for this to work
.Cells(monthRow, "B") = wsTime.Cells(timeRow, "A").Value2
.Cells(monthRow, "C") = wsTime.Cells(timeRow, "C").Value2
.Cells(monthRow, "D") = wsTime.Cells(timeRow, "B").Value2
End With
End If
'at this point we know we have a matching record in the MONTH worksheet
'choose the MONTH column to add TIME to according to the position in TIME!M:M
monthCol = 0
Select Case UCase(.Cells(timeRow, "M").Value2)
Case "PARTNER", "MANAGING DIRECTOR"
monthCol = 9 'MONTH!I:I
Case "SR. MANAGER/DIRECTOR"
monthCol = 10 'MONTH!J:J
Case "MANAGER"
monthCol = 11 'MONTH!K:K
Case "SENIOR ASSOCIATE"
monthCol = 12 'MONTH!L:L
Case "ASSOCIATE"
monthCol = 13 'MONTH!M:M
Case Else
'do nothng - no match on staff level
End Select
'if monthCol is not zero then we matched the staff level
If CBool(monthCol) Then
With wsMonth
'is there already a formula in the cell?
If .Cells(monthRow, monthCol).HasFormula Then
.Cells(monthRow, monthCol).Formula = _
.Cells(monthRow, monthCol).Formula & Chr(43) & wsTime.Cells(timeRow, "Y").Value2
Else
.Cells(monthRow, monthCol).Formula = _
Chr(61) & wsTime.Cells(timeRow, "Y").Value2
End If
End With
End If
Next timeRow
End With
End Sub
我在整个代码中都添加了注释。显示一些原始数据和所需输出的示例。对此我感激不尽。这正是我一直在寻找的,现在正在测试它。我让宏运行,但当它遇到需要创建新条目的项目时,它会每次创建一个新条目,而不是创建一个新条目,然后将以下所有条目添加到该条目中。你知道怎么解决这个问题吗?不管怎样,谢谢你的帮助。这对我来说是一个非凡的开始。我有很多事情要做,但似乎
.HasFormula
不起作用。单元格是否格式化为文本?这在我自己的测试中没有发生。我会尝试一下