0

Excel Macro VBA I have two sheets of data in an excel file. I want to loop through all the columns and all the rows to check if the cells are empty.

I am trying to replace a word from the "TemplateSheet".
"DataSheet" contains the words for replacement.

But it seems like my while loop doesn't work on looping through the columns. Can you help out?

 While Sheets("DataSheet").Cells(1, iColumn) <> ""

    While Sheets("DataSheet").Cells(iRow, 1) <> ""
       Dim sReplace As String
       sReplace = Sheets("DataSheet").Cells(iRow, 1)
       sTemplate = Sheets("TemplateSheet").Cells(1, 1)
       sFind = Sheets("DataSheet").Cells(1, iColumn)

       sTemplate = Replace(sTemplate, sFind, sReplace)

       MsgBox sTemplate

       iRow = iRow + 1

    Wend

    iColumn = iColumn + 1
Wend

Thanks for the help.

4
  • Does it give an error? What are your iRow and iColumn variables initialized to? I also think that your While clauses need a .Value after the .Cells bit, so it looks like While Sheets("DataSheet").Cells(1, iColumn).Value <> "", it may be comparing an actual cell to an empty string. Commented Jun 5, 2015 at 18:00
  • Thanks for the accepted answer but I'm not totally sure if we've completely solved your problem yet. Is there data filled in for every cell in Row1 and Column1 on DataSheet for each column and row you want to check? If so, your while loops could still work. Commented Jun 5, 2015 at 18:23
  • I've changed my answer to just add one line of code to your existing code. See below. Commented Jun 5, 2015 at 18:39
  • Why do you set sTemplate to the first cell of TemplateSheet only to set it to something else two lines later? Commented Jun 5, 2015 at 18:45

2 Answers 2

1

If you really want to go until it gets to a blank, there's a CurrentRegion property that will do just that.

Sub ReplaceCells()

    Dim rCell As Range
    Dim shTemplate As Worksheet
    Dim shData As Worksheet
    Dim sReplace As String, sTemplate As String, sFind As String

    Set shTemplate = Sheets("TemplateSheet")
    Set shData = Sheets("DatatSheet")

    For Each rCell In shData.Range("A1").CurrentRegion.Cells
        If Len(rCell.Value) > 0 Then
            sReplace = shData.Cells(rCell.Row, 1)
            sTemplate = shTemplate.Cells(1, 1)
            sFind = shData.Cells(1, rCell.Column)

            sTemplate = Replace(sTemplate, sFind, sReplace)

            MsgBox sTemplate
        End If
    Next rCell
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

So you are looping through your columns and then looping through your rows. However, your rows never get set back to 1 when you get to the next column. Does it fix your issue to add iRow = 1 before the rows loop like so?

While Sheets("DataSheet").Cells(1, iColumn) <> ""
    iRow = 1
    While Sheets("DataSheet").Cells(iRow, 1) <> ""
       Dim sReplace As String
       sReplace = Sheets("DataSheet").Cells(iRow, 1)
       sTemplate = Sheets("TemplateSheet").Cells(1, 1)
       sFind = Sheets("DataSheet").Cells(1, iColumn)
       sTemplate = Replace(sTemplate, sFind, sReplace)
       MsgBox sTemplate
       iRow = iRow + 1
    Wend
    iColumn = iColumn + 1
Wend

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.