2016-11-30 166 views
0

我想使用VBA自動更改包含管道字符「|」的單元格的顏色。Excel VBA錯誤

當檢測到時,我想代碼去除管道字符「|」並將單元格顏色更改爲灰色。該代碼不工作,以下列出:

With Sheets("DATASHEET").Range("AG1:BG53") 
    Set c = .Find("|", LookIn:=xlValues) 
     If Not c Is Nothing Then 
      firstAddress = c.Address 
      Do 
      c.Value = "" 
      c.Pattern = xlSolid 
      c.PatternColorIndex = xlAutomatic 
      c.ThemeColor = xlThemeColorDark1 
      c.TintAndShade = -0.249977111117893 
      c.PatternTintAndShade = 0 
      Loop While Not c Is Nothing And c.Address <> firstAddress 
     End If 
    End With 

當我運行VBA,我收到以下錯誤:

運行時錯誤「91」: 對象變量或With塊變量未設置

的代碼失敗在這裏:

循環而不是C一無所有,c.Address <> firstAddress

碰撞後,我的調試手錶具有以下值:

表達firstAddress" = $AG$37(這是在被合併,併爲中心的範圍內的第一小區 - 一個要求)

表達式c.Address =對象變量或帶塊變量未設置

表達式c.Value =對象變量或With塊變量未設置

我將不勝感激任何幫助。

謝謝!

jmseiver

更新:

此代碼的工作,感謝達倫!

With Sheets("DATASHEET").Range("AG1:BG53") 
      Set c = .Find("|", LookIn:=xlValues) 
      If Not c Is Nothing Then 
       Do 
       c.Value = "" 
       With Selection.Interior 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
        .ThemeColor = xlThemeColorDark1 
        .TintAndShade = -0.249977111117893 
        .PatternTintAndShade = 0 
       End With 
       Set c = .FindNext(c) 
       Loop While Not c Is Nothing 
      End If 
     End With 

此代碼不:

With Sheets("DATASHEET").Range("AG1:BG53") 
      Set c = .Find("|", LookIn:=xlValues) 
      If Not c Is Nothing Then 
       Do 
       c.Value = "" 
       With Selection.Interior 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
        .ThemeColor = xlThemeColorDark1 
        .TintAndShade = -0.249977111117893 
        .PatternTintAndShade = 0 
       End With 
       Set c = .FindNext(c) 
       Loop While Not c Is Nothing 
      End If 
     End With 

更改單元格顏色是程序的目的。額外的With - End With不起作用。

它不會彈,它不會改變單元格的顏色。

感謝大家爲他們的時間到目前爲止!

jmseiver

更新2:

此代碼的工作!

'color any cell with updated data to gray 
     With Sheets("DATASHEET").Range("AG1:BG53") 
      Set c = .Find("|", LookIn:=xlValues) 
      If Not c Is Nothing Then 
       Do 
       c.Replace What:="|", Replacement:="" 
       c.Interior.ColorIndex = 15 
       Set c = .FindNext(c) 
       Loop While Not c Is Nothing 
      End If 
     End With 

總之,本碼的目的1)查找其第一個字符是管道字符的所有小區「|」,2)去除管道字符「|」,和3)着色細胞灰色。

再次感謝Darren和約翰!

jmseiver

回答

1

你要測試是否c什麼在同時沒有(儘管隱式)的條件假設,它也並非一無是處。像這樣(未經測試):

With Sheets("DATASHEET").Range("AG1:BG53") 
    Set c = .Find("|", LookIn:=xlValues) 
    If Not c Is Nothing Then 
     firstAddress = c.Address 
     Do 
      c.Value = "" 
      Set c = .FindNext(c) 
      If c is Nothing Then Exit Do 
     Loop While c.Address <> firstAddress 
    End If 
End With 

問題是,與大多數編程語言不同,VBA不會短路邏輯運算符。無論AB總是A and B評估,即使A是假。因此,您有時需要在VBA中以更全面的方式編寫代碼。

+0

你可以使用原來的代碼,並刪除與'firstAddress'交易的代碼。代碼將循環,直到所有'|'都被替換後c爲止。 –

+0

@ DarrenBartrup庫克好一點。老實說,我並沒有過多地關注到了代碼試圖做的,而是集中在他們跑進了特定的錯誤。也許你可以發表一個答案,說明如何簡化代碼。 –

+0

所有,下面的代碼工作: –

0

它可與Excel Find and Replace Format被簡化:

Application.ReplaceFormat.Clear      ' optional 
Application.ReplaceFormat.Interior.ColorIndex = 15 ' set the replacement format 
[DATASHEET!AG1:BG53].Replace "|", "", LookAt:=xlPart, ReplaceFormat:=True 

但是,這就像你更新的代碼將取代包含|所有單元格。

只查找|啓動電池,你可以在你的代碼替換查找條件:

Set c = .Find("|*", , LookIn:=xlValues, LookAt:=xlWhole)