首先:变量声明不准确Dim myColors As Long这一句并不好,程序中myColors在1-56之间,声明为long将使程序速度降低。 其次:代码不紧凑 以下代码 If bCancel = True Then bCancel = False On Error GoTo 0 Exit Sub End If 可以修改为 If bCancel = True Then bCancel = False: On Error GoTo 0: Exit Sub 这样显得结构紧凑一些 再次:多余的循环 将下变这一句加在循环语句中间无疑使程序变慢,而事实上根本不需要。无端使程序效率降低........... ColName = Array("Black", "White", "Red", "Bright Green", "Blue", "Yellow", "Pink", "Turquoise", _ "Dark Red", "Green", "Dark Blue", "Dark Yellow", "Violet", "Teal", "Gray-25%", "Gray-50%", "Periwinkle", _ "Plum", "Ivory", "Light Turquoise", "Dark Purple", "Coral", "Ocean Blue", "Ice Blue", "Dark Blue", "Pink", _ "Yellow", "Turquoise", "Violet", "Dark Red", "Teal", "Blue", "Sky Blue", "Light Turquoise", "Light Green", _ "Light Yellow", "Pale Blue", "Rose", "Lavender", "Tan", "Light Blue", "Aqua", "Lime", "Gold", "Light Orange", _ "Orange", "Blue-Gray", "Gray-40%", "Dark Teal", "Sea Green", "Dark Green", "Olive Green", "Brown", _ "Plum", "Indigo", "Gray-80%") 还有:多余的变量 以下两句可以合并。原代码无端端多使用一个变量,也会导致程序效率低下 COLORNAME = ColName(myColors) Cells(myColors, 5) = COLORNAME 可以缩为一句: Cells(myColors, 5) = ColName(myColors) 还有:这两句也是不必要的。在前面的代码加1就可避免这两个操作(先选择然后插入)。况且选择行再插入本就是多余,可以不用选择而直接插入,这是VBA基础课上需要讲的。 Rows("1:1").Select Selection.Insert Shift:=xlDown 最后:多余的赋值操作 下面的代码赋值四次,完全可以一次完成,提升效率。 Range("B1").Value = "Color Index" Range("C1").Value = "HTML Colors" Range("D1").Value = "RGB Index" Range("E1").Value = "Color Name" 以上改为: Range("B1:E1").Value = Array("Color Index", "HTML Colors", "RGB Index", "Color Name") 等等,等等...............
可以精简码为: Sub ShowColorIndex() On Error Resume Next Dim myColors As Byte, COLORNAME As String, HexString As String, HTMLcolor As String, RGBColor As String, ColName As Variant Sheets.Add.Name = "Colors" ColName = Array("Black", "White", "Red", "Bright Green", "Blue", "Yellow", "Pink", "Turquoise", "Dark Red", "Green", _ "Dark Blue", "Dark Yellow", "Violet", "Teal", "Gray-25%", "Gray-50%", "Periwinkle", "Plum", "Ivory", "Light Turquoise", _ "Dark Purple", "Coral", "Ocean Blue", "Ice Blue", "Dark Blue", "Pink", "Yellow", "Turquoise", "Violet", "Dark Red", _ "Teal", "Blue", "Sky Blue", "Light Turquoise", "Light Green", "Light Yellow", "Pale Blue", "Rose", "Lavender", "Tan", _ "Light Blue", "Aqua", "Lime", "Gold", "Light Orange", "Orange", "Blue-Gray", "Gray-40%", "Dark Teal", "Sea Green", _ "Dark Green", "Olive Green", "Brown", "Plum", "Indigo", "Gray-80%") For myColors = 1 To 56 Cells(myColors + 1, 1).Interior.ColorIndex = myColors Cells(myColors + 1, 2) = myColors HexString = Right("000000" & Hex(Cells(myColors, 1).Interior.Color), 6) HTMLcolor = "#" & Right(HexString, 2) & Mid(HexString, 3, 2) & Left(HexString, 2) Cells(myColors + 1, 3) = HTMLcolor RGBColor = Cells(myColors + 1, 1).Interior.Color Mod 256 RGBColor = RGBColor & " " & Int(Cells(myColors + 1, 1).Interior.Color / 256) Mod 256 RGBColor = RGBColor & " " & Int(Cells(myColors + 1, 1).Interior.Color / 256 / 256) Cells(myColors + 1, 4) = RGBColor Cells(myColors + 1, 5) = ColName(myColors - 1) Next Range("B1:E1").Value = Array("Color Index", "HTML Colors", "RGB Index", "Color Name") Columns("B:E").Columns.AutoFit Range("A1").Select On Error GoTo 0 End Sub
[此贴子已经被作者于2008-2-2 15:49:51编辑过] |