如果列 AC-AF 包含空白,则剪切并粘贴行
Cut and paste row if columns AC-AF contain blanks
我想要完成的是:
如果我的整个工作表中 AC-AF 列中的任何单元格为空白,请剪切整行并粘贴到标有“MissingShipping”的新工作表中。
代码应根据行数进行调整,因为行数永远不会相同。
根据我看到的示例,我不明白在哪里插入我想要浏览的单元格范围。
我收到错误
"Method 'Range' of object'_Worksheet'
线上NewSetup.Range(Cells(Destinationrow, 1), Cells(Destinationrow, lastcolumn)).Select
.
Option Explicit
Sub Shipping()
Dim MissingShipping As Worksheet
Set MissingShipping = Sheets.Add(After:=Sheets(Sheets.Count))
MissingShipping.Name = "MissingShipping"
Dim NewSetup As Worksheet
Dim lastcolumn As Integer
Dim Destinationrow As Integer
Dim lastrow As Long
Set NewSetup = Worksheets("NKItemBuildInfoResults")
Set MissingShipping = Worksheets("MissingShipping")
Destinationrow = 1
lastcolumn = NewSetup.Range("XFD1").End(xlToLeft).Column
lastrow = NewSetup.Range("A1048576").End(xlUp).Row
Dim i As Long
Dim j As Long
For i = lastrow To 1 Step -1
For j = 1 To lastcolumn
If NewSetup.Cells(i, j).Value = "" Then
NewSetup.Activate
NewSetup.Range(Cells(i, 1), Cells(i, lastcolumn)).Cut
MissingShipping.Activate
NewSetup.Range(Cells(Destinationrow, 1), Cells(Destinationrow, _
lastcolumn)).Select
ActiveSheet.Paste
NewSetup.Rows(i).Delete shift:=xlUp
Destinationrow = Destinationrow + 1
Exit For
End If
Next j
Next i
End Sub
你好尼基,
欢迎来到 VBA 的世界!互联网上有很多很棒的资源可以帮助您完成旅程。
在代码中使用 运行ge 比读取和写入 sheet 并选择单元格来模仿您通常会做的事情通常更容易和更快手动作业。
尽早了解 运行ge 对象是个好主意。处理多个 worksheet 很方便。
以下是 Excel 范围的良好开端:
https://excelmacromastery.com/excel-vba-range-cells/
另一个方便的东西是集合。如果您必须存储一堆东西以便以后使用,您可以将它们添加到一个集合中,然后使用 "For Each" 循环遍历它们。这是对集合的一个很好的解释:
https://excelmacromastery.com/excel-vba-collections/
我快速查看了您的代码并使用范围和集合的概念,我已将其更改为执行我认为您正在尝试执行的操作。由于我没有见过你 sheet,所以我不得不做出一些假设。我 运行 在我的计算机上一堆 运行dom 行上的代码以确保它有效。考虑以下因素:
Dim MissingShipping As Worksheet
Dim NewSetup As Worksheet
Dim rangeToCheck As Range
Dim cellsToCheck As Range
Dim targetRange As Range
Dim rw As Range 'rw is a row
Dim cl As Range 'cl is a cell
Dim rowColl As New Collection
Dim i As Long
Set NewSetup = Worksheets("NKItemBuildInfoResults")
Set MissingShipping = Worksheets("MissingShipping")
'Get the range of data to check
Set rangeToCheck = NewSetup.Range("A1").CurrentRegion
'For each row in the range
For Each rw In rangeToCheck.Rows
'For the last four cells in that row
Set cellsToCheck = rw.Cells(1, 29).Resize(1, 4)
For Each cl In cellsToCheck.Cells
'If the cell is empty
If cl.Value = "" Then
'Add the row to our collection of rows
rowColl.Add rw
'Exit the for loop because we only want to add the row once.
'There may be multiple empty cells.
Exit For
End If
'Check the next cell
Next cl
Next rw
'Now we have a collection of rows that meet the requirements that you were after
'Using the size collection of rows we made, we now know the size of the range
'we need to store the values
'We can set the size of the new range using rowColl.Count
'(that's the number of rows we have)
Set targetRange = MissingShipping.Range("A1").Resize(rowColl.Count, 32)
'Use i to step through the rows of our new range
i = 1
'For each row in our collection of rows
For Each rw In rowColl
'Use i to set the correct row in our target range an make it's value
'equal to the row we're looking at
targetRange.Rows(i) = rw.Value
'Increment i for next time
i = i + 1
Next rw
End Sub
祝你好运!希望这有帮助。
我想要完成的是:
如果我的整个工作表中 AC-AF 列中的任何单元格为空白,请剪切整行并粘贴到标有“MissingShipping”的新工作表中。
代码应根据行数进行调整,因为行数永远不会相同。
根据我看到的示例,我不明白在哪里插入我想要浏览的单元格范围。
我收到错误
"Method 'Range' of object'_Worksheet'
线上NewSetup.Range(Cells(Destinationrow, 1), Cells(Destinationrow, lastcolumn)).Select
.
Option Explicit
Sub Shipping()
Dim MissingShipping As Worksheet
Set MissingShipping = Sheets.Add(After:=Sheets(Sheets.Count))
MissingShipping.Name = "MissingShipping"
Dim NewSetup As Worksheet
Dim lastcolumn As Integer
Dim Destinationrow As Integer
Dim lastrow As Long
Set NewSetup = Worksheets("NKItemBuildInfoResults")
Set MissingShipping = Worksheets("MissingShipping")
Destinationrow = 1
lastcolumn = NewSetup.Range("XFD1").End(xlToLeft).Column
lastrow = NewSetup.Range("A1048576").End(xlUp).Row
Dim i As Long
Dim j As Long
For i = lastrow To 1 Step -1
For j = 1 To lastcolumn
If NewSetup.Cells(i, j).Value = "" Then
NewSetup.Activate
NewSetup.Range(Cells(i, 1), Cells(i, lastcolumn)).Cut
MissingShipping.Activate
NewSetup.Range(Cells(Destinationrow, 1), Cells(Destinationrow, _
lastcolumn)).Select
ActiveSheet.Paste
NewSetup.Rows(i).Delete shift:=xlUp
Destinationrow = Destinationrow + 1
Exit For
End If
Next j
Next i
End Sub
你好尼基,
欢迎来到 VBA 的世界!互联网上有很多很棒的资源可以帮助您完成旅程。
在代码中使用 运行ge 比读取和写入 sheet 并选择单元格来模仿您通常会做的事情通常更容易和更快手动作业。
尽早了解 运行ge 对象是个好主意。处理多个 worksheet 很方便。
以下是 Excel 范围的良好开端:
https://excelmacromastery.com/excel-vba-range-cells/
另一个方便的东西是集合。如果您必须存储一堆东西以便以后使用,您可以将它们添加到一个集合中,然后使用 "For Each" 循环遍历它们。这是对集合的一个很好的解释:
https://excelmacromastery.com/excel-vba-collections/
我快速查看了您的代码并使用范围和集合的概念,我已将其更改为执行我认为您正在尝试执行的操作。由于我没有见过你 sheet,所以我不得不做出一些假设。我 运行 在我的计算机上一堆 运行dom 行上的代码以确保它有效。考虑以下因素:
Dim MissingShipping As Worksheet
Dim NewSetup As Worksheet
Dim rangeToCheck As Range
Dim cellsToCheck As Range
Dim targetRange As Range
Dim rw As Range 'rw is a row
Dim cl As Range 'cl is a cell
Dim rowColl As New Collection
Dim i As Long
Set NewSetup = Worksheets("NKItemBuildInfoResults")
Set MissingShipping = Worksheets("MissingShipping")
'Get the range of data to check
Set rangeToCheck = NewSetup.Range("A1").CurrentRegion
'For each row in the range
For Each rw In rangeToCheck.Rows
'For the last four cells in that row
Set cellsToCheck = rw.Cells(1, 29).Resize(1, 4)
For Each cl In cellsToCheck.Cells
'If the cell is empty
If cl.Value = "" Then
'Add the row to our collection of rows
rowColl.Add rw
'Exit the for loop because we only want to add the row once.
'There may be multiple empty cells.
Exit For
End If
'Check the next cell
Next cl
Next rw
'Now we have a collection of rows that meet the requirements that you were after
'Using the size collection of rows we made, we now know the size of the range
'we need to store the values
'We can set the size of the new range using rowColl.Count
'(that's the number of rows we have)
Set targetRange = MissingShipping.Range("A1").Resize(rowColl.Count, 32)
'Use i to step through the rows of our new range
i = 1
'For each row in our collection of rows
For Each rw In rowColl
'Use i to set the correct row in our target range an make it's value
'equal to the row we're looking at
targetRange.Rows(i) = rw.Value
'Increment i for next time
i = i + 1
Next rw
End Sub
祝你好运!希望这有帮助。