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

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

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

VB(ビジュアルベーシック)はマイクロソフトによってつくられたオブジェクト指向プログラミング言語のひとつで、同社のQuickBASICが拡張されたものです。VB6の進化版といわれています。

Q&A

1回答

934閲覧

VBA オブジェクト(セレクトに格納した値)を別のブックに表示

maxsbezitaburu

総合スコア0

VB

VB(ビジュアルベーシック)はマイクロソフトによってつくられたオブジェクト指向プログラミング言語のひとつで、同社のQuickBASICが拡張されたものです。VB6の進化版といわれています。

0グッド

0クリップ

投稿2021/07/12 12:39

編集2021/07/12 23:36

分からない点はsheet2.Cells(row2, col2) = colect.item(i) をコレクトの値をループさせて別のブックのセルに表示する方法**
Const ST_ROW As Long=4
Const ST_COL As Long=2
Sub 数と日付()
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim ary() As String
Dim row As Long
Dim col As Long
Dim sheet1 As Worksheet
Dim sheet2 As Worksheet
Dim lastRow As Long
Dim lastcol As Long
Dim index As Long
Dim index2 As Long
Dim colect As Collection, item As Variant Set colect=New Collection sPath="Book11.xlsx"
Set Wb1=ThisWorkbook 'コピー先ブック

Set Wb2 = Workbooks("Book11.xlsx") 'コピー元(参照する)ブック Set
sheet1=Wb2.Worksheets("コピー元")
Set sheet2=Wb1.Worksheets("コピー先")
lastRow=sheet1.Cells(Rows.Count, ST_COL).End(xlUp).row lastcol=sheet1.Cells(ST_ROW, Columns.Count).End(xlToLeft).Column ReDim ary(0, lastcol - ST_COL) For row=ST_ROW To lastRow
If sheet1.Cells(row, 4) >= 2 Then
For col=ST_COL To lastcol
ary(index, index2) = sheet1.Cells(row, col)
index2=index2+1
Next colect.Add ary End If index2 = 0
Next Dim row2 As Long
Dim col2 As Long
Dim i As Long

ここから下が不明点
For row2=2 To colect.Count
For col2=1 To 4 sheet2.Cells(row2,
col2)=colect.item(i)
col2=col2+1 Next col2=0 Next
sheet2.Range("A1:D1").Value=sheet1.Range("B3:E3").Value With ActiveSheet .Range("A1").Sort key1:=.Range("D1"), order1:=xlAscending, Header:=xlYes End WithEnd Sub

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

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

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

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

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

neconekocat

2021/07/12 13:54

読みにくいとかそういう次元ではないのでMarkdownの記法に従って修正をお願いします。
meg_

2021/07/12 14:49

コードは「コードの挿入」で記入してください。
guest

回答1

0

(とりあえず整形)

VBA

1Const ST_ROW As Long = 4 2Const ST_COL As Long = 2 3 4Sub 数と日付() 5 Dim Wb1 As Workbook 6 Dim Wb2 As Workbook 7 Dim ary() As String 8 Dim row As Long 9 Dim col As Long 10 Dim sheet1 As Worksheet 11 Dim sheet2 As Worksheet 12 Dim lastRow As Long 13 Dim lastcol As Long 14 Dim index As Long 15 Dim index2 As Long 16 Dim colect As Collection, item As Variant 17 Set colect = New Collection 18 sPath = "Book11.xlsx" 19 Set Wb1 = ThisWorkbook 'コピー先ブック 20 21 Set Wb2 = Workbooks("Book11.xlsx") 'コピー元(参照する)ブック 22 Set sheet1 = Wb2.Worksheets("コピー元") 23 Set sheet2 = Wb1.Worksheets("コピー先") 24 lastRow = sheet1.Cells(Rows.Count, ST_COL).End(xlUp).row 25 lastcol = sheet1.Cells(ST_ROW, Columns.Count).End(xlToLeft).Column 26 ReDim ary(0, lastcol - ST_COL) 27 For row = ST_ROW To lastRow 28 If sheet1.Cells(row, 4) >= 2 Then 29 For col = ST_COL To lastcol 30 ary(index, index2) = sheet1.Cells(row, col) 31 index2 = index2 + 1 32 Next 33 colect.Add ary 34 End If 35 index2 = 0 36 Next 37 Dim row2 As Long 38 Dim col2 As Long 39 Dim i As Long 40 41 ここから下が不明点 42 For row2 = 2 To colect.Count 43 For col2 = 1 To 4 44 sheet2.Cells(row2, col2) = colect.item(i) 45 col2 = col2 + 1 46 Next 47 col2 = 0 48 Next 49 sheet2.Range("A1:D1").Value = sheet1.Range("B3:E3").Value 50 With ActiveSheet 51 .Range("A1").Sort key1:=.Range("D1"), order1:=xlAscending, Header:=xlYes 52 End With 53End Sub

<追記>
あなたにとって i とは何なのか、見失ってはいませんか。

VBA

1 For i = 1 To colect.Count 2 sheet2.Cells(i + 1, 1).Resize(, 4).Value = colect.item(i) 3 Next

これが正しいかどうかは、私にもわかりません。
あなた自身で確かめてみてください。

投稿2021/07/12 23:43

編集2021/07/13 00:05
jinoji

総合スコア4585

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問