简体   繁体   English

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

[英]Adding an apostrophe to entire column in Excel-VBA

I've been asked to write a macro for a ~9,000 line worksheet that has numbers that are longer than Excel typically allows. 我被要求为约9,000行的工作表编写一个宏,该工作表的数字比Excel通常允许的数字长。 To get around this, I have to append a single quote/apostrophe to each cell prior to removing hyphens or otherwise sanitizing the data or else Excel abbreviates the numbers even when classified as Text. 为了解决这个问题,我必须在每个单元格前附加一个单引号/撇号,然后再删除连字符或对数据进行消毒,否则即使将其归类为“文本”,Excel也会将其缩写。

Is there a way to do this other than with a FOR loop and iterating through every cell? 除了使用FOR循环并遍历每个单元格之外,还有其他方法吗?

Run a loop through an array and do all the 'sanitizing' at the same time. 在数组中循环运行,并同时进行所有“消毒”操作。

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

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

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