VBA如果AC-AF列包含空白

时间:2018-07-03 14:07:21

标签: arrays excel-vba if-statement worksheet

我不到一周前才开始自学VBA,所以我经验不足,可以提供一些帮助!我一直在尝试使某些流程自动化,并且已经弄清楚了很多。但是,尽管我已经做了一些研究,但有些事情似乎对我没有用。如果有人愿意帮助我并解释我要去哪里错了。我确定我的问题是缺乏对编码的理解。

我要完成的是: 如果我整个工作表中AC-AF列中的任何单元格为空(最好需要一个根据行数进行调整的代码,因为那永远不会相同),则将整个行剪切并粘贴到标有“ MissingShipping”的新工作表中

从其他示例中,我已经看到我不知道该插入要插入的单元格范围在哪里?我还不断收到错误“对象'_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

1 个答案:

答案 0 :(得分:0)

G'day Nikki,

欢迎来到VBA的世界!互联网上有很多很棒的资源可以为您的旅途提供帮助。

使用代码中的范围而不是读取和写入工作表并选择单元格来模仿如果您手动完成工作通常会做的事情,通常会更容易,更快捷。

一个好主意是尽早绕过范围对象。这对于处理多个工作表非常方便。

以下是在Excel中使用Ranges的良好起点:

https://excelmacromastery.com/excel-vba-range-cells/

另一个方便的东西是收藏。如果以后需要存储一堆东西,可以将它们添加到集合中,然后使用“对于每个”循环对其进行迭代。这是一个很好的集合解释:

https://excelmacromastery.com/excel-vba-collections/

我快速浏览了您的代码,并使用Ranges和Collections的概念,将其更改为可以执行您认为要执行的操作。我还没有作答,因此不得不作一些假设。我在计算机上的一堆随机行上运行了代码,以确保它能正常工作。请考虑以下内容:

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

祝你好运!希望这会有所帮助。