[[20120529141709]] 『VBA実行時エラー380Listプロパティを設定できませ』(ゆうな) ページの最後に飛ぶ

[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]

 

『VBA実行時エラー380Listプロパティを設定できません。プロパティの値が無効です。』(ゆうな)
Excel2007、Windows 7、VBA6.5

フォームを表示したとき登録済みのデータをリストボックスに表示させる。
H列に1が入力されているときは表示させない。(削除行のため)
.List(.ListCount - 1, 9) = Cells(i, 10).Valueまでは大丈夫なのに
なぜか.List(.ListCount - 1, 10) = Cells(i, 11).Value以降はエラーになります。
どうしてでしょうか。

Private Sub UserForm_Initialize()
'初期化処理
Worksheets("顧客マスタ").Activate

    'リストボックスの設定
    With lstCustomer
    .Font.Size = 10
    .ColumnCount = 14
    .ColumnWidths = "40;90;100;50;20;150;60;40;60;40;40;40;40;40"
    .TextAlign = fmTextAlignLeft
    .Font.Name = "MS ゴシック"

    Dim i As Integer
    Dim LastRow As Integer
    LastRow = Range("A65536").End(xlUp).Row
    For i = 2 To LastRow
        If Cells(i, 15).Value <> 1 Then
            .AddItem Cells(i, 1).Value
            .List(.ListCount - 1, 1) = Cells(i, 2).Value
            .List(.ListCount - 1, 2) = Cells(i, 3).Value
            .List(.ListCount - 1, 3) = Cells(i, 4).Value
            .List(.ListCount - 1, 4) = Cells(i, 5).Value
            .List(.ListCount - 1, 5) = Cells(i, 6).Value
            .List(.ListCount - 1, 6) = Cells(i, 7).Value
            .List(.ListCount - 1, 7) = Cells(i, 8).Value
            .List(.ListCount - 1, 8) = Cells(i, 9).Value
            .List(.ListCount - 1, 9) = Cells(i, 10).Value
            .List(.ListCount - 1, 10) = Cells(i, 11).Value
            .List(.ListCount - 1, 11) = Cells(i, 12).Value
            .List(.ListCount - 1, 12) = Cells(i, 13).Value
            .List(.ListCount - 1, 13) = Cells(i, 14).Value

        End If
     Next
    End With
End Sub

Private Sub btnEntry_Click()

    If Not IsEntryData(Me) Then Exit Sub

    '各テキストボックスの値をシートに転記
    Dim TargetRow As Integer
    TargetRow = Range("A65536").End(xlUp).Offset(1).Row
    Range("A" & TargetRow).Value = TargetRow - 1
    Range("B" & TargetRow).Value = txtDate.Text
    Range("C" & TargetRow).Value = txtArea.Text
    Range("D" & TargetRow).Value = txtBusiness.Text
    Range("E" & TargetRow).Value = cboCategory.Text
    Range("F" & TargetRow).Value = txtCustomName.Text
    Range("G" & TargetRow).Value = cboProgress.Text
    Range("H" & TargetRow).Value = txtCharge.Text
    Range("I" & TargetRow).Value = txtNext.Text
    Range("J" & TargetRow).Value = txtTime.Text
    Range("K" & TargetRow).Value = txtTEL.Text
    Range("L" & TargetRow).Value = txtNote.Text
    Range("M" & TargetRow).Value = txtURL.Text
    Range("N" & TargetRow).Value = txtEmail.Text

    'リストボックスに追加
    lstCustomer.AddItem TargetRow - 1
    lstCustomer.List(lstCustomer.ListCount - 1, 1) = txtDate.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 2) = txtArea.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 3) = txtBusiness.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 4) = cboCategory.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 5) = txtCustomName.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 6) = cboProgress.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 7) = txtCharge.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 8) = txtNext.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 9) = txtTime.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 10) = txtTEL.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 11) = txtNote.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 12) = txtURL.Text
    lstCustomer.List(lstCustomer.ListCount - 1, 13) = txtEmail.Text

    'コントロールのクリア
    txtDate.Text = ""
    txtArea.Text = ""
    txtBusiness.Text = ""
    cboCategory.Text = ""
    txtCustomName.Text = ""
    cboProgress.Text = ""
    txtCharge.Text = ""
    txtNext.Text = ""
    txtTime.Text = ""
    txtTEL.Text = ""
    txtNote.Text = ""
    txtURL.Text = ""
    txtEmail.Text = ""

End Sub

こちらも同じくlstCustomer.List(lstCustomer.ListCount - 1, 10) = txtTEL.TextのところでListプロパティを設定できません。プロパティの値が無効です。とでるのです。
どうしたらいいでしょうか


 AddItemではColumnは10までしか処理できないはずです。
 一旦配列に取得して、配列を拡張して値を挿入し再度表示させる。
 例(未検証)

    Dim a(), x As Long
    a = Me.lstCustomer.Column
    ReDim Preserve a(UBound(a, 1), UBound(a, 2) + 1)
    x = UBound(a, 2)
    With Me
        a(0, x) = .txtDate.Text
        a(1, x) = .txtArea.Text
        a(2, x) = .txtBusiness.Text
        a(3, x) = .boCategory.Text
        a(4, x) = .txtCustomName.Text
        a(5, x) = .cboProgress.Text
        a(6, x) = .txtCharge.Text
        a(7, x) = .txtNext.Text
        a(8, x) = .txtTime.Text
        a(9, x) = .txtTEL.Text
        a(10, x) = .txtNote.Text
        a(11, x) = .txtURL.Text
        a(12, x) = .txtEmail.Tex
        .lstCustomer.Column = a
    End With
(seiya)

ありがとうございます。
全くの初心者でとても困っていたところでした。
書いてくださっていたので
これでいいのかわからないんですけれどやってみました。

   Dim i As Integer
    Dim LastRow As Integer
    LastRow = Range("A65536").End(xlUp).Row
    For i = 2 To LastRow
        If Cells(i, 15).Value <> 1 Then

    Dim a(), x As Long
    a = Me.lstCustomer.Column
    ReDim Preserve a(UBound(a, 1), UBound(a, 2) + 1)
    x = UBound(a, 2)
    With Me
        a(0, x) = .txtDate.Text
        a(1, x) = .txtArea.Text
        a(2, x) = .txtBusiness.Text
        a(3, x) = .cboCategory.Text
        a(4, x) = .txtCustomName.Text
        a(5, x) = .cboProgress.Text
        a(6, x) = .txtCharge.Text
        a(7, x) = .txtNext.Text
        a(8, x) = .txtTime.Text
        a(9, x) = .txtTEL.Text
        a(10, x) = .txtNote.Text
        a(11, x) = .txtURL.Text
        a(12, x) = .txtEmail.Text
        .lstCustomer.Column = a
    End With

それで

 a(11, x) = .txtURL.Text
までいくと
実行時エラー9
インデックスが有効範囲にありません
となってしまいました。
私がとんちんかんなことをしているのでしょうか?
とにかく本を読んでやってみたという状態なので
教えていただいていることもすっきり理解できた!
とはいかないような状態で。。。すいません。
(ゆうな)

 Debug Window が表示された状態で(エラー発生時) UBound(a,1)の値を読んでもらえますか?
 カーソルをUbound(a,1)の上に移動するとポップアップが表示されるとおもうのですが?
 (seiya)

a(11, x) = .txtURL.Textまでは
UBound(a,1)=9
ですが、エラーになると画面が切り替わってしまってよめません。
(ゆうな)

 OKです

 投稿された最初のコードから、私が理解した内容は

 1) lstCustomerには、予め13列のリストが表示されている。
 2) そのリストの最下行に一行挿入してリストを拡張する。

 どこか違いますか?

 違う場合は、何をどのようにしたいのか詳しく説明してください。
 (seiya)

そのほかは
a(UBound(a, 1), UBound(a, 2) + 1)=<インデックスが有効範囲にありません>
UBound(a, 2)=0
となっています。

本当にこんな質問に付き合っていただいてありがとうございます。

ええと、
1)lstCustomerのリストボックスには14列のリストを表示したいのですが、エラーで出てきません。
2)そのリストの最下行に1行挿入して新しく登録したデータを表示させる。

です。
お忙しい中すいません。
(ゆうな)


 1) UserForm moduleに追加

 Private Sub UserForm_Initialize()
    With Me.lstCustomer
        .List = Range("a2", Range("a" & Rows.Count).End(xlUp)).Resize(, 13).Value
        .ColumnCount = 13
    End With
End Sub

 2) CommandButton1を追加して、下記コードをUserForm moduleに挿入

 Private Sub CommandButton1_Click()
    Dim a(), x As Long
    a = Me.lstCustomer.Column
    ReDim Preserve a(UBound(a, 1), UBound(a, 2) + 1)
    x = UBound(a, 2)
    With Me
        a(0, x) = .txtDate.Text
        a(1, x) = .txtArea.Text
        a(2, x) = .txtBusiness.Text
        a(3, x) = .boCategory.Text
        a(4, x) = .txtCustomName.Text
        a(5, x) = .cboProgress.Text
        a(6, x) = .txtCharge.Text
        a(7, x) = .txtNext.Text
        a(8, x) = .txtTime.Text
        a(9, x) = .txtTEL.Text
        a(10, x) = .txtNote.Text
        a(11, x) = .txtURL.Text
        a(12, x) = .txtEmail.Tex
        .lstCustomer.Column = a
    End With
 End Sub

 これで試してください。
 (seiya)


seiyaさん、ありがとうございます。
完璧にできました!!!
お忙しい中長い時間付き合ってくださって本当にありがとうございました。
お礼のしようもありません。
書いていただいたのをそのままにせずに頑張って理解するように努力しますね!
本当に本当にお世話になりました。拝みたい気持ちです。
またなにかわからないことがあれば質問させていただきます。
その時は見かけられたらまたよろしくお願いします。
(サクッとわからないのでスルーされるかも・・(笑))
(ゆうな)

 ご自分で解明してみてください。
 解らない部分は質問すれば、誰か(私も含めて)が回答してくれますから。
 (seiya)

わかりました。
本当にお世話になりました。
めげずに頑張ります!
(ゆうな)

コメント返信:

[ 一覧(最新更新順) ]


YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki. Modified by kazu.