我有一个 Excel 文件,其中一行包含密钥标识符,另一行包含相关数据。
我需要合并数据才能创建有意义的报告。
以下是数据的示例以及最终我最终希望看到的数据。
当前数据格式:
这就是我需要最终产品看起来像这样的方式,每行只有名称和总计行:
答案 0 :(得分:0)
假设“Bob Smith”在A1中,“Bob's Totals”在A3中。
如果您需要组合数据和一个单元格(比如说A10)那么
Private Sub abc()
Worksheets("Nameofyoursheet").Range("A10").Value = Worksheets("Nameofyoursheet").Range("A1").Value & " " & Worksheets("Nameofyoursheet").Range("A3").Value
End Sub
同样地,你可以继续为“简”做同样的事情。 “Jane's Totals”
您可以修改代码的& " " &
部分,以包含您希望介入的任何文字,例如& " the totals are" &
希望这就是你要找的东西。
答案 1 :(得分:0)
我的方法非常简陋和直截了当。逻辑如下:
Dummy
的新工作表,以确保安全。Agent
或Totals
(这就是我要求提供关键字的原因)。设置向上:强>
前两步后:
整个代码完成后:
代码如下:
Sub WeirdConsolidate()
Dim SourceSh As Worksheet, DummySh As Worksheet
Dim DummyLastRow As Long, NewLastRow As Long
Dim Iter As Long, Iter2 As Long
With ThisWorkbook
Set SourceSh = .Sheets("Sheet1")
SourceSh.Copy After:=.Sheets(Sheets.Count)
Set DummySh = ActiveSheet
End With
With DummySh
Application.ScreenUpdating = False
.Name = "Dummy"
DummyLastRow = .Range("A" & Rows.Count).End(xlUp).Row
For Iter = DummyLastRow To 1 Step -1
If InStr(1, .Range("A" & Iter).Value, "Totals") = 0 And _
InStr(1, .Range("A" & Iter).Value, "Agent") = 0 Then
.Rows(Iter).EntireRow.Delete
End If
Next Iter
NewLastRow = .Range("A" & Rows.Count).End(xlUp).Row
For Iter2 = 2 To NewLastRow
.Range("B" & Iter2 & ":D" & Iter2).Copy .Range("B" & (Iter2 - 1))
.Rows(Iter2).EntireRow.Delete
Next Iter2
Application.ScreenUpdating = True
End With
End Sub
相应地修改参数。如果这有帮助,请告诉我们。