標題:
誠求指點 Get Cell Color 公式 marco
[打印本頁]
作者:
jakcy1234
時間:
2013-9-10 14:03
標題:
誠求指點 Get Cell Color 公式 marco
[attach]16002[/attach]
[attach]16003[/attach]
作者:
GBKEE
時間:
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
複製代碼
作者:
jakcy1234
時間:
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
'---------------------------------------------------------------------
複製代碼
[attach]16017[/attach]
[attach]16018[/attach]
G大, 我對macro語法真是有限公司, 但是問題免強解決得到, 不過另一個問題又來...........
為什麼 excel 範圍會有限制
=SUMPRODUCT(--(ColorIndex(A1:A5462)=3))
範圍 A1:A5461 就 work
範圍 A1:A5462 或以上 就用不到 ( 問題出在那 ?)
作者:
GBKEE
時間:
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
複製代碼
作者:
jakcy1234
時間:
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
但是我不知道以上語句怎樣修改及加插 [attach]16036[/attach]
作者:
GBKEE
時間:
2013-9-11 20:13
本帖最後由 GBKEE 於 2013-9-12 07:00 編輯
回復
5#
jakcy1234
這網頁是指些版本 的陣列中元素的數目上限是 5461
5.0 A B,C
7.0 (Excel 95) D E,C,H
8.0 (Excel 97) F G,H
9.0 (Excel 2000) F G,H
圖示 :2003版的執行圖
[attach]16038[/attach]
Sub ex()
Dim ar()
'2003的Rows.Count=65536
ReDim ar(0 To Rows.Count + 1)
ar(Rows.Count + 1) = Rows.Count
MsgBox ar(Rows.Count + 1)
End Sub
複製代碼
5# 的程式是將一定數目22000,5000,17000,33000 的字串指定到陣列再轉存到儲存格.
可將程式碼複製到任一空白工作表模組 試試看
4# 的自訂函數 MyColor(儲存格範圍 , 圖樣底色數字) 試試看
可傳回: 儲存格範圍圖樣=圖樣底色數字的個數
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)