セルA1〜A9にいくつかの値を含むExcelシートがあります。私はテキストボックスとコマンドボタン1( "前"に改名)とコマンドボタン2( "次へ"に改名)を持っています。テキストボックスにセルの値を表示し、vbaを使用してExcelで移動する
[次へ]ボタンをクリックすると、A1からA9までのセルの値がA1からA9までのテキストボックスに表示され、前のボタンがクリックされたときにその逆に動作するはずです。オートシェイプのテキストボックスと2つのボタンを皮切り
セルA1〜A9にいくつかの値を含むExcelシートがあります。私はテキストボックスとコマンドボタン1( "前"に改名)とコマンドボタン2( "次へ"に改名)を持っています。テキストボックスにセルの値を表示し、vbaを使用してExcelで移動する
[次へ]ボタンをクリックすると、A1からA9までのセルの値がA1からA9までのテキストボックスに表示され、前のボタンがクリックされたときにその逆に動作するはずです。オートシェイプのテキストボックスと2つのボタンを皮切り
。 標準モジュールに次のように入力します
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()
を割り当てる:
上記より、Next
をクリックすると、ガンマが入力されます。 Previous
をクリックすると、アルファがボックスに入ります。
EDIT#1:
は私がのTextBoxに現在ある項目のうちトラック............そのように保つためにPublic
変数を使用潜水艦次のまたは前の値に到達することができます。
すべての3つのシェイプ(テキストボックスと2つのボタン)がオートシェイプから簡単に入手できます。エクセルの私のバージョンで
、そのメニューは挿入タブです。最初に始めたときには、TextBoxには何もないので、CStr()のテストの理由があります。
EDIT#2:
Prevv()
このバージョンの使用、WhereAmI
をさの初期値を有する場合を扱うために:
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
を編集:溶液を添加フォームコントロールの "B"
ソリューションは、ActiveXのためのは
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)私は `Public`変数と` CStr関数(WhereAmI) `の使用を把握することができない "次へ" ボタンと "前へ" ボタン
あなたのアプローチは本当に素晴らしいです! –
@ Gary'sStudent:ありがとう! – user3598756
に
SkipToPrevious()
にSkipToNext()
を割り当てます。私はあなたが 'TextBox'フォームコントロールを参照していると思いますが、私はExcel 2010でそれを描くことはできません:どうすればいいですか? – user3598756@ user3598756私の**編集#1を参照してください:** –
@ゲーリースラムの素敵な答え。次を押して最後のエントリに行くとサブを終了しますが、最初のエントリに戻る場合は、変数を1に戻すだけですか? –