エクセルのVBAについて質問させてください。
添付の図のように、A列にはファイルのパスが昇順で何千行と並んでおり、B列にはそのデータサイズが並んでいるリストがあります。
このリストに対して、A列のパスの1つ下の階層(\で区切られた2つめの文字列)の合計値をC列に入れて同階層の範囲で結合し、同様にA列のパスのTOP階層(\で区切られた1つめの文字列)の合計値をD列に入れて同階層の範囲で結合‥を行いたいと思った際にはどのようにVBAを記述すれば良いでしょうか?
(A6のようにTOP階層しかないものはC行はそのまま値が入ればOKにしたい)
エクセルのバージョンは2007になります。
よろしくお願いします。
No.1ベストアンサー
- 回答日時:
こんにちわ
Option Explicit
'"\" を 円マークに変える必用があるかも
Sub ファイルのパス別のサイズ()
Dim maxRow As Long, RowKey1 As Long, RowKey2 As Long
Dim i As Long, j As Long
Dim myDic As Object, myKey
Dim Temp, TempArray, TotalArray
Dim Key1 As String, Key2 As String
Set myDic = CreateObject("Scripting.Dictionary")
With Worksheets("Sheet1") 'シート名は、実際の物にする
maxRow = .Cells(Rows.Count, "A").End(xlUp).Row
ReDim TotalArray(1 To maxRow, 1 To 6)
For i = 1 To maxRow
TempArray = Split(.Cells(i, 1).Value, "\")
Key1 = TempArray(0)
If UBound(TempArray) > 0 Then
Key2 = Key1 & "\" & TempArray(1)
Else
Key2 = Key1
End If
If Not myDic.exists(Key1) Then
RowKey1 = RowKey1 + 1
myDic(Key1) = RowKey1
TotalArray(RowKey1, 4) = i
TotalArray(RowKey1, 5) = i
TotalArray(RowKey1, 6) = .Cells(i, 2).Value
Else
RowKey1 = myDic(Key1)
TotalArray(RowKey1, 5) = i
TotalArray(RowKey1, 6) = TotalArray(RowKey1, 6) + .Cells(i, 2).Value
End If
If Not myDic.exists(Key2) Then
RowKey2 = RowKey2 + 1
myDic(Key2) = RowKey2
TotalArray(RowKey2, 1) = i
TotalArray(RowKey2, 2) = i
TotalArray(RowKey2, 3) = .Cells(i, 2).Value
Else
RowKey2 = myDic(Key2)
TotalArray(RowKey2, 2) = i
TotalArray(RowKey2, 3) = TotalArray(RowKey2, 3) + .Cells(i, 2).Value
End If
Next i
For i = 1 To maxRow
If TotalArray(i, 1) = 0 Then Exit For
.Range(.Cells(TotalArray(i, 1), "C"), .Cells(TotalArray(i, 2), "C")).Merge
.Range(.Cells(TotalArray(i, 1), "C"), .Cells(TotalArray(i, 2), "C")).BorderAround Weight:=xlMedium
.Cells(TotalArray(i, 1), "C").Value = TotalArray(i, 3)
Next i
For i = 1 To maxRow
If TotalArray(i, 4) = 0 Then Exit Sub
.Range(.Cells(TotalArray(i, 4), "D"), .Cells(TotalArray(i, 5), "D")).Merge
.Range(.Cells(TotalArray(i, 4), "D"), .Cells(TotalArray(i, 5), "D")).BorderAround Weight:=xlMedium
.Cells(TotalArray(i, 4), "D").Value = TotalArray(i, 6)
Next i
End With
End Sub
ki-aaaさん
ご回答いただいてありがとうございます!
バッチリ動作するのを確認できました。
しかも罫線を入れるところまでカバー頂いて‥
今は教えて頂いた記述の後にさらに別の動作を追記すると、そちらが動作せず原因を探っているところです。
ですが大変助かりました!感謝に堪えません。
No.2
- 回答日時:
> どのようにVBAを記述すれば良いでしょうか?
マクロを人任せで作らせても勉強にならないと思います。
Sub Sumple1()
Dim xRng As Range
Dim arry() As Variant, i As Long
Dim sr As Long, lr As Long
Dim r As Range
Application.ScreenUpdating = False
sr = 1
lr = Cells(Rows.Count, 1).End(xlUp).Row
Set xRng = Range("A" & sr & ":B" & lr)
xRng.Offset(, 2).MergeCells = False
arry = xRng.Value
For i = 1 To UBound(arry)
arry(i, 1) = Left(arry(i, 1), _
InStr(InStr(arry(i, 1) & "\", "\") + 1, arry(i, 1) & "\", "\"))
Next i
Call ttl(arry, 3)
arry = xRng.Value
For i = 1 To UBound(arry)
arry(i, 1) = Left(arry(i, 1), InStr(arry(i, 1) & "\", "\"))
Next i
Call ttl(arry, 4)
For Each r In xRng.Offset(, 2)
If r.Value = "" Then
r.Offset(-1, 0).Resize(2, 1).merge
End If
Next r
Set xRng = Nothing
Application.ScreenUpdating = True
End Sub
Sub ttl(arry() As Variant, c As Integer)
Dim i As Long
For i = UBound(arry) - 1 To 1 Step -1
If arry(i, 1) = arry(i + 1, 1) Then
arry(i, 2) = arry(i, 2) + arry(i + 1, 2)
End If
Next i
For i = 2 To UBound(arry)
If arry(i - 1, 1) = arry(i, 1) Then
arry(i, 2) = vbNullString
End If
Next i
For i = 1 To UBound(arry)
Cells(i, c).Value = arry(i, 2)
Next i
End Sub
d-q-t-pさん
おっしゃる通りに思います。
ですがそれでもご回答いただいてありがとうございます!
バッチリ動作することを確認しました。
実業務ではエクセルをほとんど使わないため勉強はほんの少しずつになっていますが
いただいた回答を紐解ける状態に辿り着けるよう、平行して地道に頑張りたいと思います。
感謝しております!
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
列方向、行方向の定義
-
「段」と「行」の違いがよくわ...
-
エクセルのソートで、数字より...
-
エクセル マクロ 範囲指定で...
-
エクセルで離れた列を選択して...
-
VBA 指定した列にある日時デー...
-
【マクロ】【VBA】条件付き書式...
-
Excel文字列一括変換
-
VBAで結合セルを転記する法を教...
-
エクセル 1つのシートを日付で...
-
LEFT関数とIF関数の組み合わせ...
-
Excel 2007で複合グラフ(折線...
-
エクセル マクロ 範囲の値を上...
-
csvデータの列の入れ替えができ...
-
エクセル 重複 隣の列 一番...
-
エクセル重複行統合マクロの意味
-
エクセルで最初の行や列を開け...
-
アクセス 取り込み時に、桁数(...
-
エクセルVBAでデータ最終行取得...
-
リストからデータを紐付けしたい
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
「段」と「行」の違いがよくわ...
-
エクセルで離れた列を選択して...
-
VLOOKUPの列番号の最大は?
-
LEFT関数とIF関数の組み合わせ...
-
Excelの行数、列数を増やしたい...
-
列方向、行方向の定義
-
VBA 指定した列にある日時デー...
-
エクセルマクロPrivate Subを複...
-
Excel文字列一括変換
-
エクセル マクロ 範囲指定で...
-
Alt+Shift+↑を一括で行うには、...
-
CSVファイルの「0落ち」にVBA
-
VBAで結合セルを転記する法を教...
-
エクセルで複数列の検索をマク...
-
リストからデータを紐付けしたい
-
横軸を日付・時間とするグラフ化
-
エクセルで最初の行や列を開け...
-
エクセルのソートで、数字より...
-
エクセルマクロの組み方
-
☆Excel VBAでAVERAGE関数を使う...
おすすめ情報