'UserFormのモジュールに書くコード Option Explicit Option Base 1
Dim myClass1(5) As New Class1 Private Sub UserForm_Initialize() Dim myTextbox As New Collection Dim i As Integer With myTextbox .Add Item:=TextBox1 .Add Item:=TextBox2 .Add Item:=TextBox3 .Add Item:=TextBox4 .Add Item:=TextBox5 End With
For i = 1 To 5 Set myClass1(i) = New Class1 With myClass1(i) .txt = myTextbox(i) .Index = i End With Next End Sub
Private WithEvents myTxt As MSForms.TextBox Private idx As Integer Public Property Get txt() As MSForms.TextBox Set txt = myTxt End Property Public Property Let txt(ByVal txtNew As MSForms.TextBox) Set myTxt = txtNew End Property Public Property Get Index() As Integer Index = idx End Property Public Property Let Index(ByVal idxNew As Integer) idx = idxNew End Property Private Sub myTxt_Change() MsgBox "textbox(" & idx & ")を変更しました" End Sub
Range の CopyFromRecordset メソッドで Range のある Worksheet では なくて ActiveSheet の Range の位置の NumberFormatLocal が変わって しまうというバグを見つけたんだが、こんなデカいバグがパッチ当てられて いないほど DAO って使われてないのか?
Sub test() Dim dbnam As String dbnam = ThisWorkbook.FullName & ".dbo" If Dir(dbnam) <> "" Then Kill dbnam Dim db As Database Set db = DBEngine.CreateDatabase(dbnam, dbLangGeneral) Dim table As TableDef Set table = db.CreateTableDef("data") table.Fields.Append table.CreateField("date", dbDate) db.TableDefs.Append table db.Execute "insert into data values ('2003/06/01')" db.Execute "insert into data values ('2003/06/02')" db.Execute "insert into data values ('2003/06/03')" Dim rs As Recordset Set rs = db.OpenRecordset("select * from data") Dim ws As Worksheet Set ws = ActiveSheet Dim wsData As Worksheet Set wsData = ThisWorkbook.Worksheets.Add ws.Activate ws.Cells(1, 1).NumberFormatLocal = "G/標準" ws.Cells(1, 2).NumberFormatLocal = "G/標準" ws.Cells(1, 3).NumberFormatLocal = "G/標準" wsData.Range("a1").CopyFromRecordset rs Application.DisplayAlerts = False wsData.Delete Application.DisplayAlerts = True Debug.Print ws.Cells(1, 1).NumberFormatLocal Debug.Print ws.Cells(1, 1).NumberFormatLocal Debug.Print ws.Cells(1, 1).NumberFormatLocal End Sub
days = 0 For k = 1 To m - 1 days = days + mk(k) Next k week = (zure + days + d - 1) Mod 7 If week = 0 Then TextBox4.Text = "Sunday" ElseIf week = 1 Then TextBox4.Text = "Monday" ElseIf week = 2 Then TextBox4.Text = "Tuesday" ElseIf week = 3 Then TextBox4.Text = "Wednesday" ElseIf week = 4 Then TextBox4.Text = "Thursday" ElseIf week = 5 Then TextBox4.Text = "Friday" ElseIf week = 6 Then TextBox4.Text = "Saturday" Else TextBox4.Text = "Error" End If End Sub
オレだったらこんなコードにする。 Private Sub CommandButton1_Click() Dim strWeekday() strWeekday = Array("", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat") If IsDate(Me.TextBox1.Value) = False Then MsgBox "日付が誤っています。" Me.TextBox1.SetFocus Exit Sub End If If (Me.TextBox1.Value <> Format(Me.TextBox1.Value, "yyyy/mm/dd")) And _ (Me.TextBox1.Value <> Format(Me.TextBox1.Value, "yyyy/m/d")) Then MsgBox "日付が誤っています。" Me.TextBox1.SetFocus Exit Sub End If MsgBox strWeekday(Weekday(Me.TextBox1.Value)) End Sub