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

标签 excel vba

我正在格式化我收到的一些数据。我在 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

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

最佳答案

尝试这个

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

或者,您不需要变量
With ThisWorkbook.Sheets("Sheet1")
    For x = 2 To 300
        .Range("A" & x).Value = Replace(.Range("A" & x).Value, "*", "") 
    Next
End With

关于excel - 如何将 Excel 单元格的内容设置为等于字符串变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20167254/

相关文章:

vba - 从一列中提取 5 位数字到另一列

vba - 使用 VBA : double clicking cell creates new sheet, 单元格也超链接到新工作表

arrays - 在Excel中如何在排序后获取原始行索引

vba - 如何使录制的宏在任何工作表上工作?

excel - 如何使用条件格式或 VBA 根据行中的颜色为单元格着色

excel - VBA-检查是否加载了特定的用户表单

excel - 给定半径、高度和深度,在 VBA 中查找水箱的体积

excel - 当搜索范围中的第一个单元格为空时匹配公式?

vba - Excel VBA 数组循环故障排除 : Using Redim and UBound, 一维和二维数组

excel - 根据下拉选项在工作簿之间复制和粘贴数据