[英]How to make User Form Output at the end of a column in Excel VBA
I'm trying to make this user form output option button results at the end of a column. 我试图使此用户窗体输出选项按钮的结果在列的末尾。 Originally I tried using the same i in the for next loop that I have in my main module, but the user form wouldn't recognize the variable, even after dimensioning it as public. 最初,我尝试在主模块中的for next循环中使用相同的i,但即使将其标注为public,用户窗体也无法识别该变量。 Any ideas? 有任何想法吗?
Private Sub cmdOk_Click()
If opt1 Then
Range(Range(“E3”), Range(“E3”).End(xlDown)) = "None"
ElseIf opt2 Then
Range(Range(“E3”), Range(“E3”).End(xlDown)) = "3/8 Plain"
ElseIf opt3 Then
Range(Range(“E3”), Range(“E3”).End(xlDown)) = "1/2 Plain"
ElseIf opt4 Then
Range(Range(“E3”), Range(“E3”).End(xlDown)) = "1/2 Herringbone"
ElseIf opt5 Then
Range(Range(“E3”), Range(“E3”).End(xlDown)) = "1/2 Diamond"
End If
Unload frmLagging
End Sub
You need to find the last row in that column and then write to it. 您需要在该列中找到最后一行,然后对其进行写入。 Here is an example ( Untested ). 这是一个示例( 未经测试 )。 You can read more about finding the last row Here 您可以在此处阅读有关查找最后一行的更多信息。
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> We add 1 so that we get the next empty cell
lRow = .Range("E" & .Rows.Count).End(xlUp).Row + 1
Set rng = .Range("E" & lRow)
If opt1 Then
rng.Value = "None"
ElseIf opt2 Then
rng.Value = "3/8 Plain"
ElseIf opt3 Then
rng.Value = "1/2 Plain"
ElseIf opt4 Then
rng.Value = "1/2 Herringbone"
ElseIf opt5 Then
rng.Value = "1/2 Diamond"
End If
End With
Unload frmLagging
Also, Here is one of my favorite iif statements, 另外,这是我最喜欢的iif语句之一,
Private Sub CommandButton1_Click()
Dim Rws As Long, Rng As Range, x
Rws = Cells(Rows.Count, "E").End(xlUp).Row
Set Rng = Cells(Rws + 1, "E")
x = IIf(Opt1, "None", IIf(Opt2, "3/8 Plain", IIf(Opt3, "1/2 Plain", IIf(Opt4, "1/2 Herringbone", IIf(Opt5, "1/2 Diamond", "Nada")))))
Rng = x
Unload Me
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.