使用VBA Userform格式化行
我创建了一个使用VBA Userform格式化行,vba,excel,userform,code-formatting,Vba,Excel,Userform,Code Formatting,我创建了一个userform,我有一个小难题。如果选择了userform中的值,如何将文本设置为特定的颜色?我想做的是,如果组合框中的SP.值为“Yes”,那么我希望整个iRow文本为红色,如果ST.值为“Yes”,那么我希望整个iRow文本为蓝色。我希望这有意义?SP.Value和ST.Value都是userform中的组合框,只有“Yes/No”选项 我得到的错误与对象必须是用户定义的类型,对象或变量 Private Sub NL_Click() Dim iRow As Long Dim
userform
,我有一个小难题。如果选择了userform
中的值,如何将文本设置为特定的颜色?我想做的是,如果组合框中的SP.值为“Yes”,那么我希望整个iRow文本为红色,如果ST.值为“Yes”,那么我希望整个iRow文本为蓝色。我希望这有意义?SP.Value和ST.Value都是userform中的组合框,只有“Yes/No”选项
我得到的错误与对象必须是用户定义的类型,对象或变量
Private Sub NL_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sp Br")
iRow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
If SP.Value = "Yes" Then
With iRow
.colour = -16776961
.TintAndShade = 0
Sheets("Spec Break").Range("B2").Value = Customer.Value
Sheets("Spec Break").Range("B3").Value = Project.Value
Sheets("Spec Break").Range("B4").Value = Format(Now, ["DD/MM/YYYY"])
Sheets("Spec Break").Range("B5").Value = RSM.Value
ws.Cells(iRow, 1).Value = Cf.Value
ws.Cells(iRow, 2).Value = RT.Value
ws.Cells(iRow, 3).Value = MEqu.Value
ws.Cells(iRow, 4).Value = hmm.Value
ws.Cells(iRow, 5).Value = wmm.Value
ws.Cells(iRow, 6).Value = Opt.Value
ws.Cells(iRow, 7).Value = Tap.Value
ws.Cells(iRow, 8).Value = Fing.Value
ws.Cells(iRow, 9).Value = col.Value
ws.Cells(iRow, 10).Value = Pr.Value
ws.Cells(iRow, 11).Value = Qt.Value
End With
End If
'Insert a row beneath the data to push down footer image
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
'clear form values
CustRef.Value = ""
RadType.Value = ""
MysonEquiv.Value = ""
heightmm.Value = ""
widthmm.Value = ""
Output.Value = ""
Tapping.Value = ""
Fixing.Value = ""
colour.Value = ""
Price.Value = ""
Qty.Value = ""
End Sub
正如SJR所指出的,你的iRow有一个很长的数值,12345578等等,所以你不能真正用它做任何事情(好吧,你可以,但这不是重点)。您已经有了ws.cells代码;iRow保存行号,您可以指定一列。因此,删除with块,并对前几行使用单元格和行引用:
If SP.Value = "Yes" Then
Rows(iRow).colour = -16776961
Rows(iRow).TintAndShade = 0
Sheets("Spec Break").Range("B2").Value = Customer.Value
Sheets("Spec Break").Range("B3").Value = Project.Value
Sheets("Spec Break").Range("B4").Value = Format(Now, ["DD/MM/YYYY"])
Sheets("Spec Break").Range("B5").Value = RSM.Value
ws.Cells(iRow, 1).Value = Cf.Value
' etc
对不起,我不是故意点击那个。。。我已经增加了答案。感谢您将我发送到正确的方向,遗憾的是,提供的解决方案仍然返回了一个或两个错误。在查阅颜色托盘和MSDN后,我发现将代码更改为下面的代码已经奏效了
Private Sub NL_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Spec Break")
iRow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
If Specials.Value = "Yes" Then
With Rows(iRow)
.Font.Color = RGB(255, 0, 0)
Sheets("Spec Break").Range("B2").Value = Customer.Value
Sheets("Spec Break").Range("B3").Value = Project.Value
Sheets("Spec Break").Range("B4").Value = Format(Now, ["DD/MM/YYYY"])
Sheets("Spec Break").Range("B5").Value = RSM.Value
ws.Cells(iRow, 1).Value = Cf.Value
ws.Cells(iRow, 2).Value = RT.Value
ws.Cells(iRow, 3).Value = MEqu.Value
ws.Cells(iRow, 4).Value = hmm.Value
ws.Cells(iRow, 5).Value = wmm.Value
ws.Cells(iRow, 6).Value = Opt.Value
ws.Cells(iRow, 7).Value = Tap.Value
ws.Cells(iRow, 8).Value = Fix.Value
ws.Cells(iRow, 9).Value = col.Value
ws.Cells(iRow, 10).Value = Pr.Value
ws.Cells(iRow, 11).Value = Qt.Value
End With
End If
End Sub
异常的是,可能错误消息很好地总结了问题。您正在使用iRow
跟踪With
语句,这是一个很长的语句。也许你的意思是行(iRow)
?