vba 使用 CHR(34) 在单元格内容周围添加引号引发“子未定义”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21859792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:59:54  来源:igfitidea点击:

vba to add quotes around cell content using CHR(34) throws "sub not defined" error

excel-vbavbaexcel

提问by xyz

I am tiring to write a macro so that every cell in column B gets quotemarks around entire string in the cell

我很累写一个宏,以便 B 列中的每个单元格在单元格中的quote整个字符串周围都有标记

I get the error Sub or Function not definedand the CHAR(34) is highlighted

我收到错误Sub or Function not defined并且 CHAR(34) 突出显示

Thanks

谢谢

Edit: This works now

编辑:这现在有效

Sub AddQuotesToCells()
Dim rng As Range, cell As Range
Dim LastRow As Long

With Sheets("sheet1")
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row

    Set rng = .Range("B2:B" & LastRow)

    For Each cell In rng
        cell.Value = Chr(34) & cell & Chr(34)
    Next
End With

End Sub

结束子

回答by simpLE MAn

It is not CHAR()it is Chr(). Also, did you forget the .before the rangeat line Set rng = range...?

它不是CHAR()它是Chr()。另外,您是否忘记.rangeat 行之前的内容Set rng = range...

And for the formula question:
Change

对于公式问题:
改变

rng.Formula = "=" & CHAR(34) & "B2" & CHAR(34) & ""

By

经过

rng.Formula = "=""""""" & Chr(34) & "&" & "B2" & "&" & Chr(34) & """"""""