【问题标题】:Paste Value to Column X when Column A matches a value当 A 列与值匹配时,将值粘贴到 X 列
【发布时间】:2014-12-17 18:51:46
【问题描述】:

我有一本大约有 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

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

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

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”列的SUM(估计超额费用)。该值已由社区特定工作表中每个月列底部的公式计算(如果它使宏更易于编写,可以再次重新计算)。

我正在尝试编写一个代码,该代码将检查“摘要”表的 A 列中的每个单元格中的社区名称(每次循环在名为“db_community”的STRING 中运行时存储为文本值)并且,找到匹配项后,在“摘要”工作表右侧的第一个空白列中插入第 I 列的总和。本质上,将值与 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 并将其作为变量存储在数据下方的空白单元格中的代码:

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

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

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

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

【问题讨论】:

  • 这里有很多东西需要考虑,而且由于它太大了,如果没有实际看到它,很难理解它。但是在阅读了您的问题之后,听起来INDEXMATCH 可以用来代替VBA,但这可能会变得相当复杂。 VBA 显然仍然可以工作,您只需从循环中的cell 变量中获取Row,然后在正确的工作表中找到下一个空单元格。只需 google find empty cell in row 大量结果。
  • 查找空行有效,但我必须根据 A 列中匹配值的行找到一个新行。这就是我遇到问题的部分......索引匹配会起作用,但是当新报告运行时,我必须每个月重做一次。在我弄清楚代码之前,这可能是短期解决方案。谢谢!

标签: vba excel


【解决方案1】:

阅读更新后的原始帖子后,我相信此代码符合您的需求。

  • 这将检查“摘要”中“A”列的值

  • 然后它将查看其他未命名为摘要的工作表,并检查 ws.Name 是否与正在检查的行上的“A”值匹配。

  • 当它找到匹配项时,它会找到该社区工作表的最后一行,并在“I”列的末尾创建一个公式。

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

  • 遍历摘要中的每一行并重复该过程。

代码:

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

【讨论】:

  • @johnwonderbread,我阅读了您更新的问题。现在更容易理解,您每个月都会在社区表中清除数据。这就解释了为什么在“I”的底部加上一个总和是没有问题的。您是否尝试过我发布的解决方案?我很确定它完全符合您的要求,除了我使用的是 tempCommunity 而不是 db_community。使用 db_community 更新
  • 我遇到了一些错误,尤其是在“lCol = Sheets(source)...”行。错误“1004”:应用程序定义或对象定义的错误我已经尝试了一些东西,但没有运气。想法?
  • @johnwonderbread,lCol 在哪里?你是说lastCol吗?如果是这样,您是否将您的来源命名为“摘要”?
  • PJ,我最初更改了您建议的变量的命名约定。我已经恢复到您原来的命名方法。在这两行之上,我有一行“source = “Summary”。错误仍然存​​在。
  • 好的。我通常使用 lCol 来表示 LONG 类型的列号。 lastCol 表示最后一个列。每个人都有自己的变化。错误具体在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 2021-10-21
  • 2017-11-20
相关资源
最近更新 更多