我是VBA的新手,我目前正在处理一些Excel文件。
我现在看的文件有一千张。除第一张纸外,它们看起来都一样,如:
http://i.imgur.com/t5UMte3.png
第一张纸具有col B每行中每张纸的名称:
http://i.imgur.com/AlH3pm5.png
我想要做的是,对于已经填充的col E中的每个单元格,将值写入第一页中的相应行。就像在每张纸的第一张纸上写一份报告一样。 希望我足够清楚......
Sub macro()
Dim c As Range
Dim ws As Worksheet
Dim d As Range
Dim I As Integer
For Each c In Feuil1322.UsedRange.Columns("B").Cells
For Each ws In ActiveWorkbook.Worksheets
If c.Value = ws.Name Then
For Each d In ws.UsedRange.Columns("E").Cells
If Not IsEmpty(d.Value) Then
Feuil1322.Cells(c.End(xlUp).Row, 3).Formula = d.Value
End If
Next
End If
Next
Next
End Sub
答案 0 :(得分:1)
每个子工作表中的列标题标签很重要,因为它将用作过滤器标题,以过滤掉E列中的空白并收集所有可见值(例如问题语句)。 / p>
Sub macro()
Dim wsI As Worksheet, ws As Worksheet
Dim i As Long, rng As Range, str As String
on error goto Safe_Exit
application.screenupdating = false
application.enableevents = false
Set wsI = Worksheets("Issues")
For Each ws In ActiveWorkbook.Worksheets
With ws
str = vbnullstring
If .AutoFilterMode Then .AutoFilterMode = False
If CBool(Application.CountIf(wsI.Columns(2), .Name)) And _
Application.CountA(.Columns(5)) > 2 Then
With .Range(.Cells(2, 5), .Cells(Rows.Count, 5).End(xlUp))
.AutoFilter Field:=1, Criteria1:="<>"
With .Resize(.Rows.Count - 1, 1).Offset(1, 0)
For Each rng In .SpecialCells(xlCellTypeVisible).Cells
str = str & rng.Value & Chr(10)
Next rng
End With
.AutoFilter Field:=1
End With
wsI.Cells(Application.Match(.Name, wsI.Columns(2), 0), 3) = Left(str, Len(str) - 1)
End If
If .AutoFilterMode Then .AutoFilterMode = False
End With
Next ws
Safe_Exit:
application.enableevents = true
application.screenupdating = true
End Sub
我收集了E列中的所有数据,并使用CHr(10)
(也称为vbLF或换行符)加入了它们。
这可以通过关闭屏幕更新和启用事件来加速,但它应该按原样合理地运行。