gpt4 book ai didi

vba - 在 Excel 中快速将 Textformat 转换为 Numberformat - VBA

转载 作者:行者123 更新时间:2023-12-04 22:31:05 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





VBA: Convert Text to Number

(10 个回答)


3年前关闭。




我正在寻找一种代码,它将为大量带有数字的单元格转换单元格的格式。我应用的程序是将数字“1”写入一个空单元格,复制它,然后乘以整个选择,其中数字存储为文本。与提供 Excel 的自动“转换为数字”建议(选择 1000 多个单元格需要很长时间)相比,这真的很快。
我写了这个宏(它可以工作,但不如上面提到的过程快):

Option Explicit

Sub nasobeni()

Dim cell As Range
Dim one As Integer

one = 1

For Each cell In Selection
cell.value = cell.value * one
Next

Application.CutCopyMode = False
Selection.NumberFormat = "0"

End Sub

有人有什么建议可以加快这个过程吗?
谢谢

最佳答案

设置范围并更改格式后,只需将值写回范围即可。

Option Explicit
Sub colaTextToNumbers()
Dim R As Range

'Can be set in many different ways
Set R = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp)) 'for column A

'Set R = Selection
'Set R = whatever

With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With

With R
.EntireColumn.NumberFormat = "General" 'or could limit this just to R, not entire column
.Value = .Value
End With

With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With

End Sub

关于vba - 在 Excel 中快速将 Textformat 转换为 Numberformat - VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52663480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com