EXCEL VBA 以A列、B列、K列、M列为条件,根据C列内容“其他”匹配多行C列结果,以C列每两个“其他”中,第一个“其他”下一行到第二个“其它”中间多行内容E列、F列、G列、H列、U列内容提取到第一个“其他”行下一行对应第21个单元格后面增加内容。
时间: 2025-01-12 22:03:28 浏览: 65
在Excel VBA中,你可以编写一个宏来处理这个需求。以下是一个示例代码,它会根据A列、B列、K列、M列的条件,并根据C列内容“其他”匹配多行C列结果,将E列、F列、G列、H列、U列的内容提取并插入到第一个“其他”行下一行的第21个单元格后面。
```vba
Sub ExtractData()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' 请根据实际情况修改工作表名称
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Dim i As Long
Dim startRow As Long
Dim endRow As Long
Dim insertRow As Long
Dim conditionMet As Boolean
startRow = 0
insertRow = 0
conditionMet = False
For i = 1 To lastRow
If ws.Cells(i, "C").Value = "其他" Then
If startRow = 0 Then
startRow = i + 1
conditionMet = True
ElseIf insertRow = 0 Then
endRow = i - 1
insertRow = i + 1
Call InsertData(ws, startRow, endRow, insertRow)
startRow = 0
insertRow = 0
End If
End If
Next i
If conditionMet And insertRow = 0 Then
endRow = lastRow
insertRow = lastRow + 1
Call InsertData(ws, startRow, endRow, insertRow)
End If
End Sub
Sub InsertData(ws As Worksheet, startRow As Long, endRow As Long, insertRow As Long)
Dim j As Long
Dim lastInsertRow As Long
lastInsertRow = insertRow
For j = startRow To endRow
ws.Cells(lastInsertRow, "U").Offset(0, 1).Value = ws.Cells(j, "E").Value
ws.Cells(lastInsertRow, "U").Offset(0, 2).Value = ws.Cells(j, "F").Value
ws.Cells(lastInsertRow, "U").Offset(0, 3).Value = ws.Cells(j, "G").Value
ws.Cells(lastInsertRow, "U").Offset(0, 4).Value = ws.Cells(j, "H").Value
ws.Cells(lastInsertRow, "U").Offset(0, 5).Value = ws.Cells(j, "U").Value
lastInsertRow = lastInsertRow + 1
Next j
End Sub
```
这个代码的工作原理如下:
1. 遍历A列,找到每个“其他”开始的位置和结束的位置。
2. 将E列、F列、G列、H列、U列的内容提取并插入到第一个“其他”行下一行的第21个单元格后面。
3. 重复上述步骤,直到处理完所有数据。
阅读全文
相关推荐






