简体   繁体   English

Excel:选择性粘贴>使用VBA添加“空白”单元格

[英]Excel: Paste Special > Adding a “blank” cell with VBA

I have a large data set that I'm working with in excel. 我在excel中有一个很大的数据集。 About 1000+ columns and close to 1 million rows. 大约1000列以上,接近一百万行。

My issue is that many of my numbers are formatted as text. 我的问题是我的许多数字都被格式化为文本。 To resolve this, I've been using the copy paste > add technique, adding a blank cell. 为了解决这个问题,我一直在使用复制粘贴>添加技术,添加一个空白单元格。

My problem is that I'm trying to macro this functionality, but I can't figure out how to add a blank cell. 我的问题是我正在尝试宏此功能,但我不知道如何添加空白单元格。

I tried to get crafty and have the macro create a new row, do the add, then delete that row. 我试图变得狡猾,让宏创建新行,进行添加,然后删除该行。 But, I can't seem to get that to work either. 但是,我似乎也无法使它正常工作。

Anyone have a solution? 有人有解决办法吗?

Instead of selecting the entire range, you need to select only the cells with values in them. 无需选择整个范围,只需选择其中包含值的单元格即可。 I would suggest the Special Cells function: 我建议特殊单元功能:

  1. Highlight the cell with the #1 in it and COPY that cell 突出显示其中带有#1的单元格并复制该单元格
  2. Highlight a column of cells to convert 突出显示要转换的单元格列
  3. Press F5 > Goto > Special > Constants (you may have to play with the options here to get only the cells you want) F5>转到>特殊>常量 (您可能必须使用此处的选项才能仅获取所需的单元格)
  4. OK (Now only the cells with values are selected) 确定(现在仅选择具有值的单元格)
  5. Now select Paste Special > Multiply 现在选择选择性粘贴>乘法

Using VBA you can conditionally convert the target values to doubles (or another type of your choosing). 使用VBA,您可以有条件地将目标值转换为两倍(或您选择的另一种类型)。
Tested example below assumes: 以下测试示例假定:

  1. you are working with Sheet1 in ActiveWorkbook 您正在使用ActiveWorkbook Sheet1
  2. numbers stored as text in column A (1) 以文本形式存储在A列中的数字(1)
  3. converted values to appear in column B (2) 转换后的值出现在B列中(2)

An aside: It's probably always a good idea save your work before running VBA. 旁白:在运行VBA之前保存工作可能总是一个好主意。 Cheers, and happy coding. 欢呼,编码愉快。

Option Explicit

Sub convert_to_dbl()
    Dim r As Long
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.Sheets(1)

    For r = 1 To FindLastRow(ws)
        With ws
            If .Cells(r, 1).Value <> "" Then
                .Cells(r, 2).Value = CDbl(.Cells(r, 1).Value)
            End If
        End With
    Next r
End Sub

Function FindLastRow(ws As Worksheet)
    Dim LastRow As Long
    If WorksheetFunction.CountA(Cells) > 0 Then
        LastRow = Cells.Find(What:="*", After:=[A1], _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlPrevious).Row
    End If
    FindLastRow = LastRow
End Function

The following code does what I was looking for. 以下代码完成了我一直在寻找的事情。

Sub psAdd()

    Dim x As Range 'Just a blank cell for variable
    Dim z As Range 'Selection to work with

    Set z = Cells
    Set x = Range("A65536").End(xlUp).Offset(1)
    If x <> "" Then
        Exit Sub
    Else
        x.Copy
        z.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd
        Application.CutCopyMode = False 'Kill copy mode
    End If
    x.ClearContents 'Back to normal

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM