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

10,664

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
Share:
10,664
MrPatterns
Author by

MrPatterns

Updated on November 25, 2022

Comments

  • MrPatterns
    MrPatterns over 1 year

    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.

     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?

    • Siddharth Rout
      Siddharth Rout over 10 years
      isn't this the same question as your previous question?
    • MrPatterns
      MrPatterns over 10 years
      Notice that my last question was answered differently. We did not set the cell's contents equal to the string variable, and that is the part that I want to understand how to do.
    • Siddharth Rout
      Siddharth Rout over 10 years
      What column is this in?
    • MrPatterns
      MrPatterns over 10 years
      Nothing's changed, still in Column A like the last question.