在文本框中显示单元格值并使用vba在excel中移动

时间:2016-05-21 15:43:09

标签: excel vba excel-vba

我有一张Excel工作表,其中包含单元格A1到A9中的一些值。我有一个文本框和命令按钮1(重命名为“previous”)和命令按钮2(重命名为“Next”)。

单击“下一步”按钮时,单元格中从A1到A9的值应显示在我从A1到A9的顺序的文本框中,单击“上一步”按钮时,它应该反向运行。 / p>

Sample

2 个答案:

答案 0 :(得分:1)

从TextS和AutoShapes的两个按钮开始。在标准模块中输入以下内容:

Public WhereAmI As Long

Sub Nextt()
    Dim s As Shape
    Set s = ActiveSheet.Shapes("TextBox 1")
    If CStr(WhereAmI) = "" Then
        WhereAmI = 1
        s.TextFrame.Characters.Text = Range("A1").Text
    Else
        If WhereAmI = 9 Then Exit Sub
        WhereAmI = WhereAmI + 1
        s.TextFrame.Characters.Text = Cells(WhereAmI, 1).Text
    End If
End Sub

<击>

<击>
Sub Prevv()
    Dim s As Shape
    Set s = ActiveSheet.Shapes("TextBox 1")
    If CStr(WhereAmI) = "" Then
        WhereAmI = 2
        s.TextFrame.Characters.Text = Range("A2").Text
    Else
        If WhereAmI = 1 Then Exit Sub
        WhereAmI = WhereAmI - 1
        s.TextFrame.Characters.Text = Cells(WhereAmI, 1).Text
    End If
End Sub

<击>

然后将Nextt()分配到“下一步”按钮,并将Prevv()分配给“上一步”按钮:

enter image description here

如上所述,如果点击Next gamma 将出现在框中。如果您点击Previous alpha 将出现在框中。

修改#1:

我使用Public变量来跟踪哪些项目当前位于 TextBox ............这样潜艇可以获得到下一个或上一个值。

AutoShapes可以轻松获得所有三个Shapes (文本框和两个按钮)

enter image description here

在我的Excel版本中,该菜单位于 插入 标签中。首次开始时, TextBox 中没有任何内容,这就是 CStr()测试的原因。

修改#2:

要处理初始值WhereAmI 0 的情况,请使用此版本的Prevv()

Sub Prevv()
    Dim s As Shape
    Set s = ActiveSheet.Shapes("TextBox 1")
    If CStr(WhereAmI) = "" Then
        WhereAmI = 2
        s.TextFrame.Characters.Text = Range("A2").Text
    Else
        If WhereAmI = 1 Then Exit Sub
        If WhereAmI = 0 Then WhereAmI = 2
        WhereAmI = WhereAmI - 1
        s.TextFrame.Characters.Text = Cells(WhereAmI, 1).Text
    End If
End Sub

答案 1 :(得分:1)

已修改:为表单控件添加解决方案“B”

用于ActiveX控件的

解决方案A

1)双击“上一步”按钮,VBA将您带到

的工作表代码窗格中
Private Sub CommandButton1_Click()'<~~ maybe your "Previous" button was not the 1st ActiveX button you inserted in the sheet so the sub title has a different number in it: don't bother and just keep it as you find it

End Sub

你填写如下:

Private Sub CommandButton1_Click() '<~~ remember: keep the number you already have there in the sub name
    UpdateTextBox 1
End Sub

2)双击“下一步”按钮,VBA将您带到

的工作表代码窗格中
Private Sub CommandButton2_Click() '<~~ maybe your "Next" button was not the 2d ActiveX button you inserted in the sheet so the sub title has a different number in it: don't bother and just keep it as you find it

End Sub

你填写如下:

Private Sub CommandButton2_Click()'<~~ remember: keep the number you already have there in the sub name
    UpdateTextBox -1
End Sub

3)将此代码放在任何模块代码窗格

Option Explicit    

Sub UpdateTextBox(shift As Long)
    Dim found As Range, myRange As Range
    Dim s As OLEObject
    Dim index As Long

    With ActiveSheet
        Set s = .OLEObjects("TextBox1") '<~~ set the name of the ActiveX TextBox control
        Set myRange = .Range("A1:A9") '<~~ set the range you want to scroll up and down
    End With

    index = 1 '<~~ default index position should textbox be empty or filled with non valid value
    With myRange
        If s.Object.Value <> "" Then '<~~ get current textbox value index in range
            Set found = .Find(what:=s.Object.Value, LookIn:=xlValues, lookat:=xlWhole) '<~~ search for the current text current textbox value index in range
            If Not found Is Nothing Then index = found.Row - .Rows(1).Row + 1
        End If

        index = index + shift '<~~ make the shift
        Select Case index
            Case Is > .Rows.Count
                index = .Rows.Count  '<~~ limit max index to range last row
            Case Is < 1
                index = 1 '<~~ limt min index to range first row
        End Select

        s.Object.Value = .Rows(index) '<~~ update textbox value
    End With
End Sub
表单控件的

解决方案B

1)在任何模块代码窗格中添加它

Option Explicit

Sub SkipToNext()
    UpdateTextBox2 1
End Sub


Sub SkipToPrevious()
    UpdateTextBox2 -1
End Sub


Sub UpdateTextBox2(shift As Long)
    Dim s As Shape    
    Dim found As Range, myRange As Range
    Dim index As Long

    With ActiveSheet
        Set s = .Shapes("TextBox 1") '<~~ set the name of the Form TextBox control
        Set myRange = .Range("A1:A9") '<~~ set the range you want to scroll up and down
    End With

    index = 1 '<~~ default index position should textbox be empty or filled with non valid value
    With myRange
        If s.TextFrame.Characters.Text <> "" Then '<~~ get current textbox value index in range
            Set found = .Find(what:=s.TextFrame.Characters.Text, LookIn:=xlValues, lookat:=xlWhole) '<~~ search for the current text current textbox value index in range
            If Not found Is Nothing Then index = found.Row - .Rows(1).Row + 1
        End If

        index = index + shift '<~~ make the shift
        Select Case index
            Case Is > .Rows.Count
                index = .Rows.Count  '<~~ limit max index to range last row
            Case Is < 1
                index = 1 '<~~ limt min index to range first row
        End Select

        s.TextFrame.Characters.Text = .Rows(index) '<~~ update textbox value
    End With
End Sub

2)将SkipToNext()分配给“下一步”按钮,将SkipToPrevious()分配给“上一步”按钮