excel - 复制一系列单元格,仅选择包含数据的单元格,仅选择值而不是公式

标签 excel vba

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

我的问题几乎是这样的:

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

希望你们中的一些天才能够帮助我。 此致。 林戈

最佳答案

简单 - 只需稍微编辑一下 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

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

关于excel - 复制一系列单元格,仅选择包含数据的单元格,仅选择值而不是公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351245/

相关文章:

excel - VBA Excel 中的弹出图表

excel - 获取最近截止日期的相应值

vba - 具有不同范围的单元格边框

vba - 如果单元格不为空,我需要使用 VBA 更改单元格格式

vba - Excel VBA将单元格数据保存到单独的工作表

visual-studio - 在哪里可以找到 MS Word 的 VBA api 文档

excel - M(Power Query 公式语言)中开放记录类型的目的和用法

vba - 为什么使用 "String * 1"转换为 double 会失败? CDbl(String) 是否适用于所有系统?

java - 从 Java 生成动态 Excel

vba - 使用 Excel VBA 将范围保存到字典