当列A与值匹配时,将值粘贴到列X.

我有一个约14张工作簿。 第一张“摘要”具有以下结构:

Electric Heat Comm. | Month1 | Month2 | Month 3 | etc. NameA | CopiedValue | CopiedValue | CopiedValue | CopiedValue NameB | CopiedValue | CopiedValue | CopiedValue | CopiedValue NameC | CopiedValue | CopiedValue | CopiedValue | CopiedValue | SUM | SUM | SUM | SUM BLANK ROW ----- BLANK ROW ----- BLANK ROW ----- BLANK ROW ----- BLANK ROW ------ Gas Heat Comm. | Month1 | Month2 | Month 3 | etc. NameA | CopiedValue | CopiedValue | CopiedValue | CopiedValue NameB | CopiedValue | CopiedValue | CopiedValue | CopiedValue NameC | CopiedValue | CopiedValue | CopiedValue | CopiedValue | SUM | SUM | SUM | SUM BLANK ROW ----- BLANK ROW ----- BLANK ROW ----- BLANK ROW ----- BLANK ROW ------ Combo Heat Comm. | Month1 | Month2 | Month 3 | etc. NameA | CopiedValue | CopiedValue | CopiedValue | CopiedValue NameB | CopiedValue | CopiedValue | CopiedValue | CopiedValue NameC | CopiedValue | CopiedValue | CopiedValue | CopiedValue | SUM | SUM | SUM | SUM BLANK ROW ----- BLANK ROW ----- BLANK ROW ----- BLANK ROW ----- BLANK ROW ------ Total Est. Over | Total SUM | Total SUM | Total SUM | Total SUM 

每个社区在工作簿中都有专门的工作表。 该工作表包含社区中每个属性的详细信息,但仅适用于CURRENT月份(旧数据在每个月末被擦除。

社区特定工作表的结构如下:

 Address | columns with other data & calculations | Estimated Overage ($) | Other data 123 Main| other data from other columns | $XXX.XX | Other data 122 Main| other data from other columns | $XXX.XX | Other data Blank | Blank | SUM of above values | blank 

“摘要”工作表中的“ CopiedValue ”是社区工作表中“I”列的CopiedValue (Estimated Overage $)。 该值已经通过社区特定工作表中每个月的列底部的公式计算出来(如果macros更容易编写,可以重新计算)。

我正在尝试编写一个代码来检查社区名称的“摘要”表的A列中的每个单元格(每当循环运行在名为“db_community”的STRING时存储为文本值),并且当发现匹配,在“摘要”工作表的右侧第一个空白列中插入列I的SUM。 本质上,将该值与A列中相应的社区进行匹配

这里是我写的代码开始尝试完成这个结果:

 Dim cell As Range Dim db_community As String For Each cell In Worksheets("Summary").Range("A4:A24") If cell.Value = db_community Then 'Paste value from cell in other sheet to first empty 'cell at end of current row End If Next cell 

每个月,单个工作表中包含SUM的单元格会更改(根据当月的条目数量)。 这是写入列的SUM并将其作为variables存储在数据下面的空白单元格中的代码:

 Dim sumRow as Long Dim overRow as Long Dim overValue As Long sumRow = Range("I4").End(xlDown).Row Cells(sumRow + 1, "I").Formula = "=SUM(I4:I" & sumRow & ")" overRow = Range("I4").End(xlDown).Row overValue = Cells(overRow, "I").Value 

所以,实质上,在“摘要”表中,我需要在列A中查找该单元格的值等于db_community(社区名称),并将overValue粘贴到新月份列中相应的单元格中。 然后重复循环直到整个工作表被填充。

最重要的是,它需要根据新的数据和新的日历月,每月增加额外的列。

如果这还不清楚,请询问。 任何帮助表示赞赏。

阅读更新后的原始文章,我相信这个代码适合您的需求。

  • 这将检查“摘要”栏“A”的价值

  • 然后,它会查看不是名为Summary的其他工作表,然后检查ws.Name是否与被检查行上的“A”值相匹配。

  • find匹配项后,find该社区表的最后一行,并在列“I”结束后创build一个公式。

  • 刚刚创build的总和的值将被发送回摘要中同一行的最后一列之后的列。

  • 循环“摘要”中的每一行并重复该过程。

码:

 Sub MonthlyCommunityUpdte() Dim source As String Dim db_Community As String Dim ws As Worksheet Dim lastRow As Long Dim tRow As Long 'set target row Dim overValue As Double 'Not sure if you already declared this. Dim lastCol As Long source = "Summary" lastRow = Sheets(source).Range("A" & Rows.count).End(xlUp).row lastCol = Sheets(source).Cells(1, Columns.count).End(xlToLeft).Column For lRow = 2 To lastRow db_Community = Sheets(source).Cells(lRow, "A") For Each ws In Worksheets If ws.Name <> source Then 'Make sure not processing source again If ws.Name = db_Community Then tRow = Sheets(db_Community).Range("A" & Rows.count).End(xlUp).row + 1 Sheets(db_Community).Cells(tRow, "I").Formula = "=SUM(I4:I" & (tRow - 1) & ")" overValue = Sheets(db_Community).Cells(tRow, "I") 'you can skip and 'replace overValue in the next line with what it is = to. Sheets(source).Cells(lRow, lastCol + 1) = overValue End If End If Next ws Next lRow End Sub