在 Excel-VBA 中的整个列中添加撇号

Adding an apostrophe to entire column in Excel-VBA

我被要求为大约 9,000 行的工作表编写一个宏,其中的数字比 Excel 通常允许的长。为了解决这个问题,我必须在删除连字符或以其他方式清理数据之前向每个单元格附加一个 quote/apostrophe,否则即使分类为文本,Excel 也会缩写数字。

除了使用 FOR 循环并遍历每个单元格之外,还有其他方法可以做到这一点吗?

运行 遍历数组并同时执行所有 'sanitizing'。

dim arr as variant, i as long

with worksheets(1)

    arr = .range(.cells(2, "A"), .cells(.rows.count, "A").end(xlup)).value2

    for i=lbound(arr, 1) to ubound(arr, 1)

        arr(i, 1) = chr(39) & replace(arr(i, 1), chr(45), vbnullstring)

    next i

    'optional text format for cells
    '.cells(2, "A").resize(ubound(arr, 1), ubound(arr, 2)).numberformat = "@"
    .cells(2, "A").resize(ubound(arr, 1), ubound(arr, 2)) = arr

end with