
好的,我有一個很大的電子表格(所以我不想單獨或手動設置每個單元格的顏色),並且我有許多單元格複製其他單元格。例如,我有儲存格N9、N 3、N80、N117 等=IF(N$931="","",N$931) 因此,如果N931 中存在某些內容,則會將其複製到N9 和co,否則它是空白的。我希望能夠讓我設定的 N931 背景顏色自動與 N9 中的背景顏色相同。我該怎麼做?
乾杯!喬恩
答案1
由於您無法使用條件格式或任何其他前端技術根據另一個單元格的顏色設定單元格顏色,因此您必須使用 VBA。
假設您的帶有顏色的資料位於第 N 行並從第 500 行開始=if(N500="", "", N500)
。進一步假設您將該公式複製到 N499; VBA 看起來像:
Sub copyValuesAndFormats()
Dim intRow As Integer
Dim rngCopy As Range
Dim rngPaste As Range
'Loop from Rows 1 through 499
For intRow = 1 To 499
'Set the copy and paste range
'CHANGE THE SHEET TO MATCH YOURS
Set rngCopy = Sheet3.Range("N" & intRow + 499)
Set rngPaste = Sheet3.Range("N" & intRow)
'Test to see if rows 500+ have a value
If rngCopy.Value <> "" Then
'Since it has a value, copy the value and color
rngPaste.Value = rngCopy.Value
rngPaste.Interior.Color = rngCopy.Interior.Color
'If you wish to copy the color of the font as well, uncomment the next line
'rngPaste.Font.Color = rngCopy.Font.Color
End If
Next intRow
End sub
如果您使用它,則 N1:N499 中不需要任何公式。無論如何,他們都會被這段程式碼擊垮。