私が言われたことと私が読んだことからこれは間違っています
ActiveCell.Value = TextBox3.Text
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = ComboBox1.Text
ActiveCell.Offset(1, -1).Select
これは機能しますが、可能な場合は.selectキーワードを使用しないように言われています。コードを再利用可能にするには、変数を作成する必要があることを読みました。プロの開発者はこのコードをどのように書くのでしょうか、それをより少ない行で書くことができ、selectを使用せずにアクティブセルオフセットを参照するにはどうすればよいですか?
TextBox3
列AComboBox1
と列Bが必要だと想定しています。別の列が必要な場合は、文字の参照を変更してください。
Sub OnClick() 'whatever your current sub is called.
Dim LastRow As Long, ws As Worksheet
Set ws = Sheets("Name of Sheet where data is going")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
ws.Range("A" & LastRow).Value = TextBox3.Text 'Adds the TextBox3 into Col A & Last Blank Row
ws.Range("B" & LastRow).Value = ComboBox1.Text 'Adds the ComboBox1 into Col B & Last Blank Row
End Sub
以下を使用する方法が必要な場合Offset()
:
Sub OnClickwithOffset() 'whatever your current sub is called.
Dim LastRow As Long, ws As Worksheet
Set ws = Sheets("Name of Sheet where data is going")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
ws.Range("A" & LastRow).Value = TextBox3.Text 'Adds the TextBox3 into Col A & Last Blank Row
ws.Range("A" & LastRow).Offset(0, 1).Value = ComboBox1.Text 'Adds the ComboBox1 into next cell to the right of TextBox3 data.
End Sub
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加