vba 如何将 Excel 单元格的内容设置为等于字符串变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20167254/
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 00:36:55  来源:igfitidea点击:

How do I set the contents of an Excel cell equal to a string variable?

excelvba

提问by phan

I am formatting some data I received. I have several hundred names of students in Column A, and for some strange reason there is a random * placed randomly throughout the names. I want to programmatically remove all * characters from all names.

我正在格式化我收到的一些数据。我在 A 列中有几百个学生的名字,出于某种奇怪的原因,在整个名字中随机放置了一个随机 *。我想以编程方式从所有名称中删除所有 * 字符。

 For x = 2 To 300

        Dim strStudent as String

        //how do i set contents of cell to string strStudent

        strStudent = Replace(strStudent, "*", "") //replace * with nothing

 Next

My question is, how do I set the contents of the cell we are looping through to the strStudent variable?

我的问题是,如何将循环的单元格内容设置为 strStudent 变量?

回答by Siddharth Rout

Try this

尝试这个

Dim strStudent As String

With ThisWorkbook.Sheets("Sheet1")
    For x = 2 To 300
        strStudent = Replace(.Range("A" & x).Value, "*", "") '//replace * with nothing
        .Range("A" & x).Value = strStudent 
    Next
End With

Alternatively you don't need a variable

或者,您不需要变量

With ThisWorkbook.Sheets("Sheet1")
    For x = 2 To 300
        .Range("A" & x).Value = Replace(.Range("A" & x).Value, "*", "") 
    Next
End With