我在Excel中使用VBA非常新。我想完成的是这个。当用户输入5的长度时,则必须将5列显示为红色。然后,当用户输入6的宽度时,则必须将6行勾勒为红色。例如:根据用户提供的宽度和高度绘制表格
我迄今这段代码:
表上的变化:
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Address = "$A$2") Then
Call Draw2DTankl
ElseIf (Target.Address = "$B$2") Then
Call Draw2DTankw
End If
End Sub
Draw2DTankl:
Sub Draw2DTankl()
On Error Resume Next
Cells(2, 4).Value = ""
Dim x As Range
Set x = Worksheets("Sheet1").Cells
x.Borders.LineStyle = xNone
Range("A1") = "Length"
Dim Length As Integer
Length = CInt(Cells(2, 1).Value)
If (Length > 30) Then
MsgBox "A length of a maximum 30 is allowed"
Exit Sub
End If
If (Length < 0) Then
MsgBox "Invalid length value entered"
Exit Sub
End If
Dim Rws As Long, Rng As Range, r As Range
If (Length > 0) Then
Rws = 20
Set Rng = Range(Cells(20, "H"), Cells(Rws, 8 + Length - 1))
For Each r In Rng.Cells
With r.Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = 3
End With
Next r
End If
If (Err.Number <> 0) Then
MsgBox Err.Description
End If
End Sub
Draw2DTankw:
Sub Draw2DTankw()
On Error Resume Next
Cells(2, 4).Value = ""
Dim x As Range
Set x = Worksheets("Sheet1").Cells
x.Borders.LineStyle = xNone
Range("B1") = "Width"
Dim Width As Integer
Width = CInt(Cells(2, 2).Value)
If (Width > 30) Then
MsgBox "A width of a maximum 30 is allowed"
Exit Sub
End If
If (Width < 0) Then
MsgBox "Invalid Width value entered"
Exit Sub
End If
Dim Col As Long, Rng As Range, r As Range
If (Width > 0) Then
Col = 21
Set Rng = Range(Cells(21, "H"), Cells(Col, 8 + Length - 1))
For Each r In Rng.Cells
With r.Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = 3
End With
Next r
End If
If (Err.Number <> 0) Then
MsgBox Err.Description
End If
End Sub
请帮助我。我的代码不起作用。长度有效,但是当我改变宽度时会刹车。
进入我的长平:
这是正确的。不过,如果我进入6宽度发生这种情况:(我的长短也自败)
我为此很长的帖子道歉!
绘制长度后,当您调用子绘图的宽度时,您正在擦除和重新绘制。由于长度在你的宽度绘制子中被明确指定为2,所以你将绘制你的坦克2个单位长。如果任何一个单元格被更改,你应该将这些子集合成一个绘制长度和宽度的子集,如Jonathan的答案如下。此外,在格式和结构化问题上做得很好。您显然已经付出了一些努力来开发您的解决方案。 – asp8811
我明白你的意思了!谢谢!我一定会考虑这一点。此外,谢谢你对帖子的赞扬。 – naheiwProg