簡體   English   中英

遍歷填充的行,根據條件匹配,使用 VBA 將值添加到 Excel 中另一個工作表中的特定單元格

[英]Iterate through populated rows, Match based on criteria, add value to specific cell in another sheet in Excel using VBA

我試圖在時間選項卡中的小時數列中選擇一個值,並將其放在選項卡月份的相應列中。 我想遍歷工作表時間中的每一行並將所有數據添加到標簽月。

對於 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

事實上,這段代碼比我的理解更深入,因為它假定兩個工作表都在同一個工作簿中。 它假定包含這兩個工作表的工作簿將是活動工作簿。 這意味着該代碼可能在另一本工作簿中,也許您保留了該工作簿中的代碼。

現在,以比您希望的更小的步驟進行,上面的代碼循環遍歷時間工作表中的所有行並找到 A 列中的值。只是為了讓您看看它是否有效,它將這些值打印到立即窗口( Ctrl + G ,如果它未顯示在您的 VBE 窗口中)。

這就是它變得模糊的地方:我認為這個值可能必須是 name,並且您現在需要在 Month 表中找到相同的名稱。 為此,需要知道“月份”表中可能會找到該名稱的列、如果找到該名稱該怎么辦以及如果找不到該名稱該怎么辦。

這就是我閱讀您的問題的方式。

  • 您想查看 TIME 工作表中的每一行。
  • 對於每一行,您希望在 MONTH 工作表中找到一個兩列匹配的行。 第一場比賽比較 Eng。 號 (TIME!A:A) 與 Eng。 MONTH!B:B 中的 no 以及階段(C 列)與 MONTH!C:C 的次要匹配。
  • 如果找到兩列 MATCH,則在 MONTH 工作表中附加基本 SUM 公式(例如=n+=n+=n+=n+=n... ),其中包含來自 TIME!Y:Y 的當前值。 這將進入 MONTH!I:M 中的列之一,具體取決於 TIME!M:M 中的工資職位。
  • 如果未找到匹配項,則在 MONTH 中創建一個新條目並將時間填寫到新條目的相應列中。

在標准代碼模塊表中:

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

我在整個代碼中添加了注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM