エクセルへの入力に関して
ボタンを押すとエクセルに入力するプログラムを作りたいのですが、
以下のプログラムでは新しいセルに入力が入らず毎度同じ場所(A3)に入ってしまいます。
どなたか解決方法をご教示頂ければ助かります。
宜しくお願い致します。
Imports Microsoft.Office.Interop
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
Inherits System.Windows.Forms.Form
'フォームがコンポーネントの一覧をクリーンアップするために dispose をオーバーライドします。 <System.Diagnostics.DebuggerNonUserCode()> Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub 'Windows フォーム デザイナーで必要です。 Private components As System.ComponentModel.IContainer 'メモ: 以下のプロシージャは Windows フォーム デザイナーで必要です。 'Windows フォーム デザイナーを使用して変更できます。 'コード エディターを使って変更しないでください。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.TextBox2 = New System.Windows.Forms.TextBox() Me.DateTimePicker2 = New System.Windows.Forms.DateTimePicker() Me.Button2 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(51, 198) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(75, 39) Me.Button1.TabIndex = 1 Me.Button1.Text = "出勤" Me.Button1.UseVisualStyleBackColor = True ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(131, 208) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(0, 18) Me.Label1.TabIndex = 3 ' 'Label2 ' Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(48, 152) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(0, 18) Me.Label2.TabIndex = 4 ' 'DateTimePicker1 ' Me.DateTimePicker1.CustomFormat = "yyyy/MM/dd HHmm" Me.DateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom Me.DateTimePicker1.Location = New System.Drawing.Point(51, 108) Me.DateTimePicker1.Name = "DateTimePicker1" Me.DateTimePicker1.Size = New System.Drawing.Size(253, 25) Me.DateTimePicker1.TabIndex = 5 ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(51, 152) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(300, 25) Me.TextBox1.TabIndex = 6 ' 'TextBox2 ' Me.TextBox2.Location = New System.Drawing.Point(374, 152) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(298, 25) Me.TextBox2.TabIndex = 7 ' 'DateTimePicker2 ' Me.DateTimePicker2.CustomFormat = "yyyy/MM/dd HHmm" Me.DateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Custom Me.DateTimePicker2.Location = New System.Drawing.Point(374, 108) Me.DateTimePicker2.Name = "DateTimePicker2" Me.DateTimePicker2.Size = New System.Drawing.Size(261, 25) Me.DateTimePicker2.TabIndex = 8 ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(374, 198) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(73, 39) Me.Button2.TabIndex = 9 Me.Button2.Text = "退勤" Me.Button2.UseVisualStyleBackColor = True ' 'Form1 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(10.0!, 18.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(800, 450) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.DateTimePicker2) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.DateTimePicker1) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents Button1 As Button Friend WithEvents Label1 As Label Friend WithEvents Label2 As Label Friend WithEvents DateTimePicker1 As DateTimePicker Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim valueString As String Dim text As String 'ボタンが押された回数 Dim count As Integer = 0 '変数宣言 Dim ex As New Microsoft.Office.Interop.Excel.Application Dim sh As Microsoft.Office.Interop.Excel.Worksheet Dim wb As Microsoft.Office.Interop.Excel.Workbook valueString = DateTimePicker1.Value.ToShortDateString() text = DateTimePicker1.Text TextBox1.Text = ControlChars.NewLine & text If count = 0 Then '----1回目 count = count + 1 'ファイルオープン wb = ex.Workbooks.Open(“D:\NewExcelBook.xlsx”) sh = wb.Sheets(“Sheet1”) sh.Range(“A3”).Value = ControlChars.NewLine & text wb.Save() ex.Quit() MsgBox(“終了”) ElseIf count = 1 Then '----2回目 count = count + 1 'ファイルオープン wb = ex.Workbooks.Open(“D:\NewExcelBook.xlsx”) sh = wb.Sheets(“Sheet1”) sh.Range(“A4”).Value = ControlChars.NewLine & text wb.Save() ex.Quit() MsgBox(“終了”) ElseIf count = 2 Then '----3回目 count = count + 1 'ファイルオープン wb = ex.Workbooks.Open(“D:\NewExcelBook.xlsx”) sh = wb.Sheets(“Sheet1”) sh.Range(“A5”).Value = ControlChars.NewLine & text wb.Save() ex.Quit() MsgBox(“終了”) Else '----4回目 count = 0 '元に戻す 'ファイルオープン wb = ex.Workbooks.Open(“D:\NewExcelBook.xlsx”) sh = wb.Sheets(“Sheet1”) sh.Range(“A6”).Value = ControlChars.NewLine & text wb.Save() ex.Quit() MsgBox(“終了”) End If
End Sub Friend WithEvents TextBox1 As TextBox Friend WithEvents TextBox2 As TextBox Friend WithEvents DateTimePicker2 As DateTimePicker Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged End Sub Friend WithEvents Button2 As Button Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim valueString As String Dim text As String valueString = DateTimePicker2.Value.ToShortDateString() text = DateTimePicker2.Text TextBox2.Text = ControlChars.NewLine & text End Sub
End Class