用户窗体不会在初始化代码时填充
Userform does not populate at initialize code
我正在使用 Excel VBA 使用单元格内部颜色和多个颜色空间来创建和组织调色板和渐变。我成功地获得了公式和代码以获取所选单元格的数字 RGB 值,但是当我尝试将这些值放入自定义用户窗体时,由于某种原因它没有填充。
有问题的用户表单。 R、G 和 B 文本框应填充 191、143、0
这是 运行 用户表单之前的子部分。它被设置为 运行 不同的用户表单或消息框,具体取决于我如何为渐变选择单元格。
Sub RunColorMixer()
Dim CellCount As Integer, RowCount As Integer, ColCount As Integer
Dim ActiveRow As Integer, ActiveCol As Integer
Dim SelectRow As Integer, SelectCol As Integer
Dim Orientation As String
'Input Phase
CellCount = Application.Selection.Cells.Count
RowCount = Application.Selection.Rows.Count
ColCount = Application.Selection.Columns.Count
ActiveRow = ActiveCell.Row
ActiveCol = ActiveCell.Column
SelectRow = Application.Selection.Row
SelectCol = Application.Selection.Column
Range("D1").Value = CellCount
Range("E1").Value = RowCount
Range("F1").Value = ColCount
Range("G1").Value = ActiveRow
Range("H1").Value = ActiveCol
Range("I1").Value = SelectRow
Range("J1").Value = SelectCol
Range("K1").Value = Orientation
'Determine Orientation
If CellCount = 1 Then 'Case 1 Single Cell
ColorpickerSingle.Show
ElseIf RowCount > 1 And ColCount > 1 Then 'Case 2 Diagonal
MsgBox "Diagonals not supported! Please keep gradients on 1 row or column only!"
Else
ColorpickerGradient.Show
End If
End Sub
这是初始化代码
Private Sub ColorpickerGradient_Initialize()
Dim CellCount As Integer, RowCount As Integer, ColCount As Integer
Dim ActiveRow As Integer, ActiveCol As Integer
Dim SelectRow As Integer, SelectCol As Integer
Dim Orient As String
Dim ColorValue1 As Variant, ColorValue2 As Variant
'Input Phase
CellCount = Application.Selection.Cells.Count
RowCount = Application.Selection.Rows.Count
ColCount = Application.Selection.Columns.Count
ActiveRow = ActiveCell.Row
ActiveCol = ActiveCell.Column
SelectRow = Application.Selection.Row
SelectCol = Application.Selection.Column
'Determine Orientation
If ActiveRow = SelectRow And ActiveCol = SelectCol Then 'Either Down or Right
If RowCount > 1 Then 'Case 3 Down
Orient = "Down"
Else 'Case 4 Right
Orient = "Right"
Orientation.Text = "Right"
End If
Else 'Either Up or Left
If RowCount > 1 Then 'Case 5 Up
Orient = "Up"
Orientation.Text = "Up"
Else 'Case 6 Left
Orient = "Left"
Orientation.Text = "Left"
End If
End If
'Input Color
ColorValue1 = ActiveCell.Interior.Color
Select Case Orientation
Case "Up"
ColorValue2 = ActiveCell.Offset(-(CellCount - 1), 0).Interior.Color
Case "Left"
ColorValue2 = ActiveCell.Offset(0, -(CellCount - 1)).Interior.Color
Case "Down"
ColorValue2 = ActiveCell.Offset((CellCount - 1), 0).Interior.Color
Case "Right"
ColorValue2 = ActiveCell.Offset(0, (CellCount - 1)).Interior.Color
End Select
sR1.Value = ColorValue1 Mod 256
sG1.Value = (ColorValue1 \ 256) Mod 256
sB1.Value = ColorValue1 \ 65536
sR2.Value = ColorValue2 Mod 256
sG2.Value = (ColorValue2 \ 256) Mod 256
sB2.Value = ColorValue2 \ 65536
End Sub
我还确保我已经正确命名了我的文本框。它应该至少填充 R 通道文本框
Initialize
事件处理方法没有使用表单的名称,所以应该是
Private Sub UserForm_Initialize()
而不是
Private Sub ColorpickerGradient_Initialize()
我正在使用 Excel VBA 使用单元格内部颜色和多个颜色空间来创建和组织调色板和渐变。我成功地获得了公式和代码以获取所选单元格的数字 RGB 值,但是当我尝试将这些值放入自定义用户窗体时,由于某种原因它没有填充。
有问题的用户表单。 R、G 和 B 文本框应填充 191、143、0
这是 运行 用户表单之前的子部分。它被设置为 运行 不同的用户表单或消息框,具体取决于我如何为渐变选择单元格。
Sub RunColorMixer()
Dim CellCount As Integer, RowCount As Integer, ColCount As Integer
Dim ActiveRow As Integer, ActiveCol As Integer
Dim SelectRow As Integer, SelectCol As Integer
Dim Orientation As String
'Input Phase
CellCount = Application.Selection.Cells.Count
RowCount = Application.Selection.Rows.Count
ColCount = Application.Selection.Columns.Count
ActiveRow = ActiveCell.Row
ActiveCol = ActiveCell.Column
SelectRow = Application.Selection.Row
SelectCol = Application.Selection.Column
Range("D1").Value = CellCount
Range("E1").Value = RowCount
Range("F1").Value = ColCount
Range("G1").Value = ActiveRow
Range("H1").Value = ActiveCol
Range("I1").Value = SelectRow
Range("J1").Value = SelectCol
Range("K1").Value = Orientation
'Determine Orientation
If CellCount = 1 Then 'Case 1 Single Cell
ColorpickerSingle.Show
ElseIf RowCount > 1 And ColCount > 1 Then 'Case 2 Diagonal
MsgBox "Diagonals not supported! Please keep gradients on 1 row or column only!"
Else
ColorpickerGradient.Show
End If
End Sub
这是初始化代码
Private Sub ColorpickerGradient_Initialize()
Dim CellCount As Integer, RowCount As Integer, ColCount As Integer
Dim ActiveRow As Integer, ActiveCol As Integer
Dim SelectRow As Integer, SelectCol As Integer
Dim Orient As String
Dim ColorValue1 As Variant, ColorValue2 As Variant
'Input Phase
CellCount = Application.Selection.Cells.Count
RowCount = Application.Selection.Rows.Count
ColCount = Application.Selection.Columns.Count
ActiveRow = ActiveCell.Row
ActiveCol = ActiveCell.Column
SelectRow = Application.Selection.Row
SelectCol = Application.Selection.Column
'Determine Orientation
If ActiveRow = SelectRow And ActiveCol = SelectCol Then 'Either Down or Right
If RowCount > 1 Then 'Case 3 Down
Orient = "Down"
Else 'Case 4 Right
Orient = "Right"
Orientation.Text = "Right"
End If
Else 'Either Up or Left
If RowCount > 1 Then 'Case 5 Up
Orient = "Up"
Orientation.Text = "Up"
Else 'Case 6 Left
Orient = "Left"
Orientation.Text = "Left"
End If
End If
'Input Color
ColorValue1 = ActiveCell.Interior.Color
Select Case Orientation
Case "Up"
ColorValue2 = ActiveCell.Offset(-(CellCount - 1), 0).Interior.Color
Case "Left"
ColorValue2 = ActiveCell.Offset(0, -(CellCount - 1)).Interior.Color
Case "Down"
ColorValue2 = ActiveCell.Offset((CellCount - 1), 0).Interior.Color
Case "Right"
ColorValue2 = ActiveCell.Offset(0, (CellCount - 1)).Interior.Color
End Select
sR1.Value = ColorValue1 Mod 256
sG1.Value = (ColorValue1 \ 256) Mod 256
sB1.Value = ColorValue1 \ 65536
sR2.Value = ColorValue2 Mod 256
sG2.Value = (ColorValue2 \ 256) Mod 256
sB2.Value = ColorValue2 \ 65536
End Sub
我还确保我已经正确命名了我的文本框。它应该至少填充 R 通道文本框
Initialize
事件处理方法没有使用表单的名称,所以应该是
Private Sub UserForm_Initialize()
而不是
Private Sub ColorpickerGradient_Initialize()