2016-08-14 110 views
1

所以我有一個合併單元格,看起來像下面如何格式化邊框合併單元格的VBA

enter image description here

以下是我的代碼將邊框周圍:

Dim c As Range 

For Each c In testing 
     If c.MergeCells Then 
      c.Interior.ColorIndex = 19 
      c.Borders.LineStyle = xlContinuous 
      c.Borders.Weight = xlThick 
      c.Borders.Color = vbGreen 
     End If 
    Next 

這代碼僅在左上角單元格周圍創建邊框(請參見圖片)。我如何確保邊界放置在整個合併單元格周圍?

+0

爲什麼Excel的公式標籤?當它看起來像你想從你的標題VBA解決方案? –

回答

3

您必須使用引用範圍的MergeArea

Dim c As Range 

For Each c In testing 
    If c.MergeCells Then 
     With c.MergeArea 
      .Interior.ColorIndex = 19 
      .Borders.LineStyle = xlContinuous 
      .Borders.Weight = xlThick 
      .Borders.Color = vbGreen 
     End With 
    End If 
Next 
3

嘗試

With c  'Range of the merged cell 
    .BorderAround , Weight:=xlMedium 
    .Borders.Color = vbGreen 
End With 
+0

嗯,我的答案似乎整潔,並比接受的更快 –

+0

你的解決方案仍然沒有把整個合併細胞的邊界 – sukhvir

+0

@sukhvir我寫了這個「c'合併單元格的範圍」在我的答案中,我的意思是如果已經使用了c,你是否必須改變合併單元格的範圍,例如'D = Range(「B2:D22」)'。我是這樣寫的,因爲我懷疑c是合併單元格的範圍。而更清楚的是,我總是試着將我的代碼發佈爲答案。上面的代碼在我的電腦上正常工作。 –