【问题标题】:Copy a range of cells and only select cells with data and just the value not the formulas复制一系列单元格,只选择包含数据的单元格,只选择值而不是公式
【发布时间】:2012-11-12 20:42:08
【问题描述】:

我正在寻找一个宏来复制一系列单元格,但只复制包含值的单元格并且只复制值而不是公式。

我的问题差不多是这样的:

Copy a range of cells and only select cells with data

我找到了这个宏 :) 但它只是缺少我需要的东西:(

iMaxRow = 5000 ' 或任何最大值。 '不要太大,因为这会减慢你的代码。

' Loop through columns and rows
For iCol = 1 To 3 ' or however many columns you have
    For iRow = 1 To iMaxRow 

    With Worksheets("Sheet1").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
            .Copy Destination:=Worksheets("Sheet2").cells(iRow,iCol)
        End If
    End With

    Next iRow
Next iCol

我希望你们中的一些天才可以帮助我。 此致。 林哥

【问题讨论】:

  • 你为什么不一次复制所有单元格并粘贴特殊值 - 无论如何空白都会复制为空白?循环显得多余。

标签: excel vba


【解决方案1】:

简单 - 只需稍微编辑 Else 语句...

' Loop through columns and rows
For iCol = 1 To 3 ' or however many columns you have
    For iRow = 1 To iMaxRow 

    With Worksheets("Sheet1").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("Sheet2").cells(iRow,iCol).value = .value
        End If
    End With

    Next iRow
Next iCol

希望这就是你的意思....

【讨论】:

  • 您好,感谢您的意见。你真是太棒了! :) 它有效
【解决方案2】:

要将所有单元格从 Sheet1A:C 的已用部分复制到 Sheet2 作为您可以使用的值

无论如何复制时,空白单元格将是空白的。

Sub Better()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Set rng1 = ws.Range(ws.[a1], ws.Cells(Rows.Count, "A").End(xlUp))
Sheets("Sheet2").Range(rng1.Address).Value = rng1.Value
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多