如何忽略宏中的空单元格

时间:2013-08-13 13:35:41

标签: excel vba excel-vba

在这里寻求一些帮助。我试图让我的宏将仅包含信息的单元格的值粘贴到列表中。不幸的是,我的宏也在拉动所有空单元格,但将它们粘贴为空单元格。有没有人知道如何让我的宏完全忽略空单元格?此外,我正在尝试将此宏贴到C38,但我认为我的参考文件可能搞砸了..

Range("C11").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "3"
Range("C12").Select
SolverOk SetCell:="$A$20", MaxMinVal:=1, ValueOf:=0, ByChange:="$B$26:$V$29", _
    Engine:=2, EngineDesc:="Simplex LP"
SolverOk SetCell:="$A$20", MaxMinVal:=1, ValueOf:=0, ByChange:="$B$26:$V$29", _
    Engine:=2, EngineDesc:="Simplex LP"
SolverSolve UserFinish:=True
        iMaxRow = 17
For iCol = 3 To 21
For iRow = 1 To iMaxRow

With Worksheets("Summary").Cells(iRow, iCol)
    ' Check that cell is not empty.
    If .Value = "" Then
        'Nothing in this cell.
        'Do nothing.
    Else
        ' Copy the cell to the destination
        Worksheets("Summary").Cells(3, 38).Value = .Value
    End If
End With

Next iRow
Next iCol

Sheets("Summary").Select

2 个答案:

答案 0 :(得分:1)

尝试一下:

Sub tgr()

    Dim ws As Worksheet
    Dim rIndex As Long
    Dim cIndex As Long

    Set ws = Sheets("Summary")

    Range("C11").Value = "3"
    SolverOk SetCell:="$A$20", MaxMinVal:=1, ValueOf:=0, ByChange:="$B$26:$V$29", _
             Engine:=2, EngineDesc:="Simplex LP"
    SolverSolve UserFinish:=True

    For cIndex = Columns("C").Column To Columns("U").Column
        For rIndex = 1 To 17
            If Len(Trim(ws.Cells(rIndex, cIndex).Text)) > 0 Then
                ws.Cells(Rows.Count, "C").End(xlUp).Offset(1).Value = ws.Cells(rIndex, cIndex).Text
            End If
        Next rIndex
    Next cIndex

    ws.Select

    Set ws = Nothing

End Sub

答案 1 :(得分:0)

如果你想保存单元格中的所有内容(“C38”),那么你应该使用这一行

Worksheets("Summary").Cells(3, 38).Value = Worksheets("Summary").Cells(3, 38).Value + .Value + chr(11)
相关问题