用vba写一个当E列不为空,F列为空,G列显示F列为空的代码
时间: 2023-03-27 11:01:47 浏览: 143
你好,以下是回答:
Sub CheckEmpty() Dim lastRow As Long lastRow = Cells(Rows.Count, "E").End(xlUp).Row
For i = 1 To lastRow
If Not IsEmpty(Cells(i, "E")) And IsEmpty(Cells(i, "F")) Then
Cells(i, "G").Value = "F列为空"
End If
Next i
End Sub
希望能对你有所帮助。
相关问题
EXCEL VBA 以A列、B列、K列、M列为条件,根据C列内容“其他”匹配多行C列结果,以C列每两个“其他”中间内容E列、F列、G列、H列、U列内容提取到第一个“其他”+1行对应的V列后。
在Excel VBA中,您可以使用以下代码来实现根据A列、B列、K列、M列为条件,根据C列内容“其他”匹配多行C列结果,并以C列每两个“其他”中间内容E列、F列、G列、H列、U列内容提取到第一个“其他”+1行对应的V列后。
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 destRow As Long
destRow = 2 ' 目标行的起始位置
For i = 2 To lastRow
If ws.Cells(i, "C").Value = "其他" Then
startRow = i + 1
endRow = FindNextOther(i, lastRow)
If endRow = 0 Then
endRow = lastRow
Else
endRow = endRow - 1
End If
Dim j As Long
For j = startRow To endRow
ws.Cells(destRow, "V").Value = ws.Cells(j, "E").Value
ws.Cells(destRow, "W").Value = ws.Cells(j, "F").Value
ws.Cells(destRow, "X").Value = ws.Cells(j, "G").Value
ws.Cells(destRow, "Y").Value = ws.Cells(j, "H").Value
ws.Cells(destRow, "Z").Value = ws.Cells(j, "U").Value
destRow = destRow + 1
Next j
i = endRow
End If
Next i
End Sub
Function FindNextOther(currentRow As Long, lastRow As Long) As Long
Dim i As Long
For i = currentRow + 1 To lastRow
If Cells(i, "C").Value = "其他" Then
FindNextOther = i
Exit Function
End If
Next i
FindNextOther = 0
End Function
代码说明:
- FindNextOther函数:用于查找当前行之后下一个“其他”出现的位置。
- ExtractData子程序:
- 遍历每一行,找到“其他”出现的位置。
- 找到“其他”之间的内容,并将其提取到目标行。
使用步骤:
- 打开Excel并按
Alt + F11
打开VBA编辑器。 - 插入一个新模块,并将上述代码粘贴进去。
- 修改代码中的表名
Sheet1
为您实际使用的表名。 - 运行
ExtractData
子程序。
EXCEL VBA 以A列、B列、K列、M列为条件,根据C列内容“其他”匹配多行C列结果,以C列每两个“其他”中,第一个“其他”下一行到第二个“其它”中间多行内容E列、F列、G列、H列、U列内容提取到第一个“其他”行下一行对应第21个单元格后面增加内容。
在Excel VBA中,你可以编写一个宏来处理这个需求。以下是一个示例代码,它会根据A列、B列、K列、M列的条件,并根据C列内容“其他”匹配多行C列结果,将E列、F列、G列、H列、U列的内容提取并插入到第一个“其他”行下一行的第21个单元格后面。
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
这个代码的工作原理如下:
- 遍历A列,找到每个“其他”开始的位置和结束的位置。
- 将E列、F列、G列、H列、U列的内容提取并插入到第一个“其他”行下一行的第21个单元格后面。
- 重复上述步骤,直到处理完所有数据。
相关推荐






