2016-03-03 36 views
0

经与用户表单的一些问题和设置的初始值,我尝试运行如下:VBA多值设置

Sub colorme() 
For Each cell In Selection 
    If cell.Interior.ColorIndex = UserForm1.colorcodeinit.Value Then 
    With cell 
    .Interior.ColorIndex = UserForm1.colorcodefin.Value 
    End With 
End If 
Next cell 
End Sub 

然而,当我尝试运行它没有注册为初始颜色我设置,以下工作就好:

Sub colorme() 
For Each cell In Selection 
    If cell.Interior.ColorIndex = -4142 Then 
    With cell 
    .Interior.ColorIndex = UserForm1.colorcodefin.Value 
    End With 
End If 
Next cell 
End Sub 

感谢您的任何和所有帮助提前!

回答

0

根据您的colorcodeinit控件值的设置方式,您可能需要确保表单已完全加载并打开,然后再尝试读取任何值。

我正在重新格式化您的代码以在SO/Markdown中呈现。代码本身没有出现任何问题,它可能仅仅是,当代码运行时就是问题所在。

请注意,ColorIndex在所有Excel用户中可能不可靠。

块1:

'Your first code block 
Sub colorme() 
    For Each cell In Selection 
    If cell.Interior.ColorIndex = UserForm1.colorcodeinit.Value Then 
     With cell 
     .Interior.ColorIndex = UserForm1.colorcodefin.Value 
     End With 
    End If 
    Next cell 
End Sub 

块2:

'Your second code block 
Sub colorme() 
    For Each cell In Selection 
    If cell.Interior.ColorIndex = -4142 Then 
     With cell 
     .Interior.ColorIndex = UserForm1.colorcodefin.Value 
     End With 
    End If 
    Next cell 
End Sub