誠求指點 Get Cell Color 公式 marco
- 帖子
- 37
- 主題
- 10
- 精華
- 0
- 積分
- 63
- 點名
- 0
- 作業系統
- XP
- 軟體版本
- 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2013-8-25
- 最後登錄
- 2016-5-24
|
誠求指點 Get Cell Color 公式 marco
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
2#
發表於 2013-9-10 17:52
| 只看該作者
回復 1# jakcy1234
試試看- Option Explicit
- Sub Ex()
- Dim Rng(1 To 3) As Range, E As Range, T As Single, i As Integer, R_Add As String
- Set Rng(1) = Sheet1.[b24:b25]
- Set Rng(2) = Sheet1.[b2:f20]
- For Each E In Rng(1)
- Application.FindFormat.Interior.ColorIndex = E.Interior.ColorIndex
- 'FindFormat 設定或傳回要尋找之儲存格格式類型的搜尋準則。
- Set Rng(3) = Rng(2).Find(What:="", SearchFormat:=True)
- If Not Rng(3) Is Nothing Then R_Add = Rng(3).Address
- i = 0
- T = 0
- Do While Not Rng(3) Is Nothing
- i = i + 1
- T = T + Rng(3)
- Set Rng(3) = Rng(2).Find(What:="", After:=Rng(3), SearchFormat:=True)
- If R_Add = Rng(3).Address Then Exit Do
- Loop
- E.Offset(, 1) = T
- E.Offset(, 2) = i
- Next
- Application.FindFormat.Clear ' 清除 FindFormat 設定要尋找之儲存格格式類型的搜尋準則。
- End Sub
複製代碼 |
|
|
|
|
|
|
- 帖子
- 37
- 主題
- 10
- 精華
- 0
- 積分
- 63
- 點名
- 0
- 作業系統
- XP
- 軟體版本
- 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2013-8-25
- 最後登錄
- 2016-5-24
|
3#
發表於 2013-9-11 08:42
| 只看該作者
回復 2# GBKEE - '---------------------------------------------------------------------
- ' ColorIndex Function
- '---------------------------------------------------------------------
- ' Function: Returns the colorindex of the supplied range
- ' Synopsis: Initially, gets a colorindex value for black and white
- ' from the activeworkbook colour palette
- ' Then works through each cell in the supplied range and
- ' determines the colorindex, and adds to array
- ' Finishes by returning acumulated array
- ' Variations: Determines cell colour (interior) or text colour (font)
- ' Default is cell colour
- ' Constraints: Does not count colours set by conditional formatting
- '---------------------------------------------------------------------
- Function ColorIndex(rng As Range, _ Optional text As Boolean = False) As Variant
- '---------------------------------------------------------------------
- Dim cell As Range, row As Range
- Dim i As Long, j As Long
- Dim iWhite As Long, iBlack As Long
- Dim aryColours As Variant
- If rng.Areas.Count > 1 Then
- ColorIndex = CVErr(xlErrValue)
- Exit Function
- End If
- iWhite = WhiteColorindex(rng.Worksheet.Parent)
- iBlack = BlackColorindex(rng.Worksheet.Parent)
- If rng.Cells.Count = 1 Then
- If text Then
- aryColours = DecodeColorIndex(rng, True, iBlack)
- Else
- aryColours = DecodeColorIndex(rng, False, iWhite)
- End If
- Else
- aryColours = rng.Value
- i = 0
- For Each row In rng.Rows
- i = i + 1
- j = 0
- For Each cell In row.Cells
- j = j + 1
- If text Then
- aryColours(i, j) = _
- DecodeColorIndex(cell, True, iBlack)
- Else
- aryColours(i, j) = _
- DecodeColorIndex(cell, False, iWhite)
- End If
- Next cell
- Next row
- End If
- ColorIndex = aryColours
- End Function
- '---------------------------------------------------------------------
- Private Function WhiteColorindex(oWB As Workbook)
- '---------------------------------------------------------------------
- Dim iPalette As Long
- WhiteColorindex = 0
- For iPalette = 1 To 56
- If oWB.Colors(iPalette) = &HFFFFFF Then
- WhiteColorindex = iPalette
- Exit Function
- End If
- Next iPalette
- End Function
- '---------------------------------------------------------------------
- Private Function BlackColorindex(oWB As Workbook)
- '---------------------------------------------------------------------
- Dim iPalette As Long
- BlackColorindex = 0
- For iPalette = 1 To 56
- If oWB.Colors(iPalette) = &H0 Then
- BlackColorindex = iPalette
- Exit Function
- End If
- Next iPalette
- End Function
- '---------------------------------------------------------------------
- Private Function DecodeColorIndex(rng As Range, _ text As Boolean, _ idx As Long)
- '---------------------------------------------------------------------
- Dim iColor As Long
- If text Then
- iColor = rng.Font.ColorIndex
- Else
- iColor = rng.Interior.ColorIndex
- End If
- If iColor < 0 Then
- iColor = idx
- End If
- DecodeColorIndex = iColor
- End Function
- '---------------------------------------------------------------------
- ' End of ColorIndex Function
- '---------------------------------------------------------------------
複製代碼
cellcolor.rar (16.52 KB)
G大, 我對macro語法真是有限公司, 但是問題免強解決得到, 不過另一個問題又來...........
為什麼 excel 範圍會有限制
=SUMPRODUCT(--(ColorIndex(A1:A5462)=3))
範圍 A1:A5461 就 work
範圍 A1:A5462 或以上 就用不到 ( 問題出在那 ?) |
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
4#
發表於 2013-9-11 10:05
| 只看該作者
回復 3# jakcy1234
2003測試沒這問題
可精簡如下- Function MyColor(Rng As Range, MyIndex As Long) As Long
- Dim A As Range, C As Range
- For Each C In Rng.Cells
- MyColor = MyColor + IIf(C.Interior.ColorIndex = MyIndex, 1, 0)
- Next
- End Function
複製代碼 |
|
|
|
|
|
|
- 帖子
- 37
- 主題
- 10
- 精華
- 0
- 積分
- 63
- 點名
- 0
- 作業系統
- XP
- 軟體版本
- 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2013-8-25
- 最後登錄
- 2016-5-24
|
5#
發表於 2013-9-11 18:02
| 只看該作者
回復 4# GBKEE - Sub PopulateRangeWithArray()
- Dim x
- ReDim x(1 To 2, 1 To 2)
- x(1, 1) = String(2000, "a"): x(1, 2) = String(5000, "b")
- x(2, 1) = String(17000, "c"): x(2, 2) = String(33000, "d")
- MsgBox Len(x(1, 1)) & "," & Len(x(1, 2)) _
- & "," & Len(x(2, 1)) & "," & Len(x(2, 2))
- Range("a1").Value = x(1, 1)
- Range("b1").Value = x(1, 2)
- Range("a2").Value = x(2, 1)
- Range("b2").Value = x(2, 2)
- End Sub
複製代碼 我已經知道 陣列中元素的數目上限是 5461, http://support.microsoft.com/kb/177991
但是我不知道以上語句怎樣修改及加插
cellcolor.rar (16.52 KB)
|
|
|
|
|
|
|