[英]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:
我建议特殊单元功能:
Using VBA you can conditionally convert the target values to doubles (or another type of your choosing). 使用VBA,您可以有条件地将目标值转换为两倍(或您选择的另一种类型)。
Tested example below assumes: 以下测试示例假定:
Sheet1
in ActiveWorkbook
ActiveWorkbook
Sheet1
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.