我有一个用户窗体文本框,我想与一个旋转按钮关联。当我插入产品ID号时,我希望旋转按钮能够在列中上下移动并相应地更改文本框的值。我尝试使用循环来执行此操作,但是由于某种原因,当满足循环条件时,它不会执行我的if then语句。我想知道是否有人可以对此问题提供任何启示。下面是我的便于查看的代码。
Private Sub SpinButton1_SpinDown()
Dim R As Integer
Let Lastrow = Worksheets("Petrobras").Cells(Rows.Count, 22).End(xlUp).Row
For R = 2 To Lastrow
OPPID = Worksheets("Petrobras").Cells(R, 22).Value
If TXTOpp_Num_Pbras.Value = OPPID Then
Worksheets("Petrobras").Cells(R, 22).Offset(-1, 0).Value
TXTOpp_Num_Pbras.Value = ActiveCell.Value
End If
Next
End Sub
答案 0 :(得分:0)
这有两个文本框和一个旋转按钮。您在第一个框中输入一个ID,它会得到一个行号。旋转会更改该行中的列,然后将单元格的值发送到第二个文本框。
Option Explicit
Public rownum As Long
Public colnum As Long
Private Sub SpinButton1_SpinDown()
Dim text2val As String
If Not colnum = 1 Then
colnum = colnum - 1
End If
text2val = ActiveSheet.Cells(rownum, colnum).Value 'Change ActiveSheet to the real sheet name
Me.Controls("TextBox2").Value = text2val
End Sub
Private Sub SpinButton1_SpinUp()
Dim text2val As String
colnum = colnum + 1
text2val = ActiveSheet.Cells(rownum, colnum).Value 'Change ActiveSheet to the real sheet name
If Not text2val = "" Then
Me.Controls("TextBox2").Value = text2val
Else
Me.Controls("TextBox2").Value = "No More Data"
End If
End Sub
Private Sub TextBox1_Change()
Dim val As String
val = "ID" & Me.Controls("TextBox1").Value 'Concatenating ID because my Column labels have 1, 2, 3, 4
If Not val = "" Then
rownum = ActiveSheet.UsedRange.Find(val).Row 'Change ActiveSheet to the real sheet name
End If
End Sub