如果 A 列中的单元格为空或值为 "Null",则删除整行。删除这些行后,我需要粘贴另一个 sheet
Delete entire row if cells in column A is empty or having value "Null". After deleting those rows I need to paste in anather sheet
有人可以帮忙吗?
宏:
如果 A 列中的单元格为空或具有值 "Null",则删除整行。删除这些行后,我需要粘贴另一个 sheet。
如果 sheet 2 已经有一些行,那么这些来自 sheet 1 的新删除的行需要放在 sheet 2 中那些存在的行下面。
请给出宏代码
谢谢
好的,我创建了以下示例 Sheet1,我将使用它来说明问题的答案。
Names: Sales
Kevin 5
Bill 2
Joe 4
Steve 5
Frank 2
Jerry 9
Casey 5
您会注意到第 6、9 和 10 行是空白的。
Sheet两个长这样:
Names: Sales
James 7
Phill 3
我们需要我们的代码首先删除 Sheet1 上的空白行,然后将这些行复制到 Sheet2 并将它们放在 sheet 的末尾。
Sub deleteBlanksAndCopy()
Dim i As Integer, j As Integer
Dim numRowsSheet1 As Integer
'This code will delete the blanks on sheet1
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = numRowsSheet1 To 1 Step -1
If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Then
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With
'This code will move the rows from sheet1 to sheet2
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With ThisWorkbook.Worksheets("Sheet2")
j = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For i = 1 To numRowsSheet1
ThisWorkbook.Worksheets("Sheet2").Rows(j + 1).Value = ThisWorkbook.Worksheets("Sheet1").Rows(i).Value
j = j + 1
Next i
End Sub
谢谢,希望对您有所帮助。
Private Sub CommandButton1_Click()
Dim i As Integer, j As Integer
Dim numRowsSheet1 As Integer
'此代码将查找 Sheet2 中的最后一行
With ThisWorkbook.Worksheets("Sheet2")
j = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
'此代码将从工作表 1 中复制 A 列中具有空白单元格的行,并将它们粘贴到工作表 2 中
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To numRowsSheet1 + 1 Step 1
If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Or .Cells(i, 1) = "Null" Then
ThisWorkbook.Worksheets("Sheet2").Rows(j + 1).Value = ThisWorkbook.Worksheets("Sheet1").Rows(i).Value
j = j + 1
End If
Next i
End With
'此代码将删除 sheet1 上的空白
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = numRowsSheet1 + 1 To 1 Step -1
If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Or .Cells(i, 1) = "Null" Then
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With
End Sub
有人可以帮忙吗?
宏:
如果 A 列中的单元格为空或具有值 "Null",则删除整行。删除这些行后,我需要粘贴另一个 sheet。
如果 sheet 2 已经有一些行,那么这些来自 sheet 1 的新删除的行需要放在 sheet 2 中那些存在的行下面。
请给出宏代码
谢谢
好的,我创建了以下示例 Sheet1,我将使用它来说明问题的答案。
Names: Sales
Kevin 5
Bill 2
Joe 4
Steve 5
Frank 2
Jerry 9
Casey 5
您会注意到第 6、9 和 10 行是空白的。
Sheet两个长这样:
Names: Sales
James 7
Phill 3
我们需要我们的代码首先删除 Sheet1 上的空白行,然后将这些行复制到 Sheet2 并将它们放在 sheet 的末尾。
Sub deleteBlanksAndCopy()
Dim i As Integer, j As Integer
Dim numRowsSheet1 As Integer
'This code will delete the blanks on sheet1
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = numRowsSheet1 To 1 Step -1
If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Then
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With
'This code will move the rows from sheet1 to sheet2
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With ThisWorkbook.Worksheets("Sheet2")
j = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
For i = 1 To numRowsSheet1
ThisWorkbook.Worksheets("Sheet2").Rows(j + 1).Value = ThisWorkbook.Worksheets("Sheet1").Rows(i).Value
j = j + 1
Next i
End Sub
谢谢,希望对您有所帮助。
Private Sub CommandButton1_Click()
Dim i As Integer, j As Integer
Dim numRowsSheet1 As Integer
'此代码将查找 Sheet2 中的最后一行
With ThisWorkbook.Worksheets("Sheet2")
j = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
'此代码将从工作表 1 中复制 A 列中具有空白单元格的行,并将它们粘贴到工作表 2 中
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To numRowsSheet1 + 1 Step 1
If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Or .Cells(i, 1) = "Null" Then
ThisWorkbook.Worksheets("Sheet2").Rows(j + 1).Value = ThisWorkbook.Worksheets("Sheet1").Rows(i).Value
j = j + 1
End If
Next i
End With
'此代码将删除 sheet1 上的空白
With ThisWorkbook.Worksheets("Sheet1")
numRowsSheet1 = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = numRowsSheet1 + 1 To 1 Step -1
If IsEmpty(.Cells(i, 1)) Or .Cells(i, 1) = "" Or .Cells(i, 1) = "Null" Then
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With
End Sub