質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Q&A

解決済

2回答

368閲覧

VBAの継承後のセル挿入ができない

Eston

総合スコア67

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

0グッド

1クリップ

投稿2020/09/25 10:49

編集2020/09/25 11:55

いつもお世話になっております。

現在、VBAの課題に取り組んでおり、行き詰まっています
Excel for 2016を使用しています

問題
・ユーザーフォームに入力した値が、セルに繁栄されない

試したこと
・.Cellを使用すると挿入されるます(btnOk_Click)が、なぜ継承を使うと挿入できないのか?が疑問です

稚拙な質問かも知れませんが、ご存知の方がいらっしゃれば、ご教授願いたいです

下記の画像で、Add transactionをクリックすると、画像2のシートに保存されるようにしたいのですが、クリックしても何も起こりません。

UserForm(画像1)

TransactionHistory(画像2)

課題の図

UserForm

1Private Sub btnAddtransaction_Click() 2Dim newTransaction As New transaction 3 4newTransaction.TransactionDate = Now() 5 6If (optCredit) Then 7newTransaction.transactionType = Credit 8End If 9If (optDebit) Then 10newTransaction.transactionType = Debit 11End If 12 13save newTransaction 14newTransaction.CryptCurrency = cmBoxCurrency.Value 15newTransaction.Quantity = txtboxQuantity.Value 16newTransaction.ExchangeRate = txtboxExchangerate.Value 17 18End Sub 19Public Sub save(transaction As transaction) 20Dim validator As New TransactionValidator 21If (validator.isValid(transaction)) Then 22Worksheets("TransactionHistory").Rows(2).Insert Shift:=xlDown 23 24Sheets("TransactionHistory").Range("A2") = transaction.TransactionDate 25 26If (transaction.transactionType = Credit) Then 27Sheets("TransactionHistory").Range("B2") = "Credit" 28Else 29Sheets("TransactionHistory").Range("B2") = "Debit" 30End If 31 32Sheets("TransactionHistory").Range("C2") = transaction.CryptCurrency 33Sheets("TransactionHistory").Range("D2") = transaction.Quantity 34Sheets("TransactionHistory").Range("E2") = transaction.ExchangeRate 35End If 36 37End Sub 38 39 40Private Sub btnOk_Click() 41MsgBox "Test2" 42Dim lastRow As Long 43With Worksheets("TransactionHistory") 44lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1 45.Cells(lastRow, 1).Value = Me.txtboxQuantity 46.Cells(lastRow, 2).Value = Me.cmBoxCurrency 47.Cells(lastRow, 3).Value = Me.txtboxExchangerate 48End With 49Unload Me 50 51End Sub

Transaction

1Public CryptCurrency As String 2Public ExchangeRate As Double 3Public Quantity As Double 4Public TransactionDate As Date 5Public transactionType As transactionType 6 7Public Function tostring() As String 8tostring = CryptCurrency & " " & transcriptionType & " " & "quantity: " & Quantity & "rate: " & ExchangeRate 9End Function

TransactionValidator

1Public Function isValid(transaction As transaction) As Boolean 2 3If (transaction Is Nothing) Then 4isValid = False 5End If 6 7If (transaction.Quantity <= 0) Then 8isValid = False 9End IF 10If (transaction.CryptCurrency <= 0) Then 11isValid = False 12End If 13 14End Function

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

meg_

2020/09/25 23:09

> クリックしても何も起こりません。 デバッグはされましたか?
guest

回答2

0

ベストアンサー

なぜ継承を使うと挿入できないのか?が疑問です

ど素人なので間違ってるかも知れませんが、こういうのは継承っていうのかなぁ?

.Cellを使用すると挿入されるます

「表にデータを挿入」という意味だと思いますが、
単に空白欄に記入ではないですか?
エクセルで挿入というと、少し違った意味になりますので、
混乱します。

で、本題。

Private Sub btnAddtransaction_Click() Dim newTransaction As New transaction

↑ここがまずいです。
プロシージャの中で変数を宣言しているので、
他のプロシージャからその中身を参照できません。
いろんなプロシージャで使うなら、
宣言セクションで変数を宣言してください。

<ユーザーフォームのモジュール>

ExcelVBA

1Option Explicit 2 3Dim myClass As Class1 4 5Private Sub UserForm_Initialize() 6 Set myClass = New Class1 7 myClass.Initialize ThisWorkbook.Worksheets(1).UsedRange 8 With Me.ListBox1 9 .ColumnCount = 5 10 .ColumnWidths = "50;50;50;50;50" 11 .ColumnHeads = True 12 End With 13 Me.CommandButton1.Enabled = False 14 InputClear 15End Sub 16 17Private Sub OptionButton1_Change() 18 If Not IsNull(Me.OptionButton1.Value) Then 19 myClass.DeditCredit = IIf(Me.OptionButton1.Value, "Dedit", "Credit") 20 End If 21 InputCheck 22End Sub 23 24Private Sub ComboBox1_AfterUpdate() 25 myClass.pCurrency = Me.ComboBox1.Value 26 InputCheck 27End Sub 28 29Private Sub TextBox1_AfterUpdate() 30 myClass.Quantity = Me.TextBox1.Value 31 InputCheck 32End Sub 33 34Private Sub TextBox2_AfterUpdate() 35 myClass.Rate = Me.TextBox2.Value 36 InputCheck 37End Sub 38 39Private Sub CommandButton1_Click() 40 With myClass 41 .AddTransaction 42 .Initialize ThisWorkbook.Worksheets(1).UsedRange 43 End With 44 InputClear 45End Sub 46 47Private Sub InputClear() 48 Dim c As Control 49 50 For Each c In Me.Controls 51 Select Case TypeName(c) 52 Case "ListBox": c.RowSource = myClass.DataBody.Address 53 Case "OptionButton": c.Value = Null 54 Case "TextBox": c.Value = "" 55 Case "ComboBox": c.Value = "" 56 End Select 57 Next 58 Me.CommandButton1.Enabled = False 59End Sub 60 61Private Function InputCheck() As Boolean 62 Dim c As Control 63 64 For Each c In Me.Controls 65 Select Case TypeName(c) 66 Case "OptionButton", "TextBox", "ComboBox" 67 If IsNull(c.Value) Or Len(c.Value) = 0 Then Exit Function 68 End Select 69 Next 70 Me.CommandButton1.Enabled = True 71 InputCheck = True 72End Function 73

<クラスモジュール>

ExcelVBA

1Option Explicit 2 3Private mDate As Date 4Private mDeditCredit As Variant 5Private mCurrency As Double 6Private mQuantity As Double 7Private mRate As Double 8Private mTable As Range 9Private mDataBody As Range 10Private mNewData As Range 11 12Public Property Let RegisteredDate(ByVal d As Date) 13 mDate = d 14End Property 15 16Public Property Let DeditCredit(ByVal CorD As Variant) 17 mDeditCredit = CorD 18End Property 19 20Public Property Let pCurrency(ByVal c As Double) 21 mCurrency = c 22End Property 23 24Public Property Let Quantity(ByVal q As Double) 25 mQuantity = q 26End Property 27 28Public Property Let Rate(ByVal r As Double) 29 mRate = r 30End Property 31 32Public Property Get DataBody() As Range 33 Set DataBody = mDataBody 34End Property 35 36Public Sub Initialize(ByRef Rng As Range) 37 mDate = 0 38 mDeditCredit = Empty 39 mCurrency = 0 40 mQuantity = 0 41 mRate = 0 42 43 With Rng 44 Set mTable = .Cells 45 Set mDataBody = Intersect(.Cells, .Offset(1)) 46 Set mNewData = .Rows(.Rows.Count + 1) 47 End With 48End Sub 49 50Public Function AddTransaction() As Boolean 51 Dim v As Variant 52 mDate = Now() 53 v = Array(mDate, mDeditCredit, mCurrency, mQuantity, mRate) 54 mNewData.Value = v 55 AddTransaction = True 56End Function

クラスモジュールを普段使わないので、
勉強で書いてみました。拙いですが参考になれば。
(読み返すと個人的に不満が多々あるけど、突っ込んで勉強する時間がないので、ここまでにします)

でも、変数の置き場で使うだけなら、
ユーザーフォームのコントロールがすでに変数の変わりが出来ているので、
敢えてクラスモジュールを使うのは二度手間なきがします。

継承がテーマなら、
https://sites.google.com/site/compositiosystemae/home/vbaworld/upper/interface
こことか参考にならないですかね。。。。。

投稿2020/09/29 11:26

mattuwan

総合スコア2136

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

.Rangeに対して

VBA

1.Range("B2") = "Credit"

各所この様に書かれていますが

VBA

1.Range("B2").Value = "Credit"

ではないですか?

投稿2020/09/25 21:16

kuma_kuma_

総合スコア2506

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問