0

Following is my vba code,where i`m trying to get data and assign it to a cell.Now,all I need to do is to lock these cells after filling. And there are many cells where I have to do this, all I got is 'qa' part is same in all the cell names I have to fill.So I have to check if 'qa' is part of the cell name,if yes then lock that complete cell and change the color.

ActiveWorkbook.Names("Book.aa.a.qa.qq.le.").RefersToRange = Doc.getAttribute("le")
ActiveWorkbook.Names("Book.bb.a.qa.qq.lf.").RefersToRange = Doc.getAttribute("lf")
ActiveWorkbook.Names("Book.cc.a.qa.qq.lg.").RefersToRange = Doc.getAttribute("lg")
ActiveWorkbook.Names("Book.dd.a.qa.qq.ll.").RefersToRange = Doc.getAttribute("ll")

How to do this? Please suggest a function for this. Thanks in advance.

4

1 回答 1

1

I noticed that you had asked almost similar question earlier. The only different here that you are specifically asking if the name contains qa, you would have been able to fix that by now with the given details in that answer. Anyway, here I use Like operator on IF to do the check on the same code,

Dim strName As String, objName As Name, strID As String

Sheet1.Unprotect
For Each objName In ActiveWorkbook.Names
    strName = objName.Name
    If InStr(1, strName, "book", vbTextCompare) > 0 Then
        strID = Mid(strName, InStr(1, strName, ".", vbTextCompare) + 1, 1)
        With ActiveWorkbook.Names(strName).RefersToRange
            .Value = doc.getAttribute(strID)
            '--change here
            If strName Like "*qa*" Then
              .Interior.Color = vbRed
              .Locked = True
            End If
        End With
    End If
Next
Sheet1.Protect
于 2013-01-21T09:14:55.183 回答