人力検索はてな
モバイル版を表示しています。PC版はこちら
i-mobile

エクセル:各支店の売上げファイルをまとめる
処理をVBAでできませんでしょうか?

デスクトップ上のフォルダに各支店の売り上げファイルがまとめてあります。
ファイル名は半角、全角混じりになっています。
アキバ支店.xls
ウエノ支店.xls
イケブクロ支店.xls



各ファイルの内容は、A列に商品、B列売上金額があり、
商品数は各支店によって異なります。
また、末行には、合計、合計金額があります。
A列 B列
商品 金額
カメラ 11,500
テレビ 54,800
・ ・
・ ・
・ ・
合計 xxxxxx

全支店売上ファイルには、A列に各支店名(半角)が
すでに入っています。
B列に各支店の合計金額を転記し、全支店売上ファイルA列に
支店名がないときは、末行にファイル名を半角で入るようにし合計金額を転記したいのです。

A列 B列
アキバ支店 xxxxxx
ウエノ支店 xxxxxx
イケブクロ支店 xxxxxx
サッポロ支店 xxxxxx ←新たに追加

よろしくお願いします。

●質問者: にゃんころね
●カテゴリ:コンピュータ 学習・教育
○ 状態 :終了
└ 回答数 : 1/1件

▽最新の回答へ

1 ● Mook
●500ポイント ベストアンサー

こんなことでしょうか。

Option Explicit

Sub 集計()
 Dim myWS As Worksheet
 Set myWS = ActiveSheet
 
 Dim wsh
 Set wsh = CreateObject("WScript.Shell")

'// ★ デスクトップ上のフォルダ名を設定 : データフォルダ を変更
 Dim dtPath
 dtPath = wsh.SpecialFolders("Desktop") & "\データフォルダ"

 Dim fso
 Set fso = CreateObject("Scripting.FileSystemObject")

 myWS.Columns("B").Font.ColorIndex = 3

 Dim file
 Dim xName
 Dim dstCell As Range
 Dim srcCell As Range
 
 Application.ScreenUpdating = False
 Application.Calculation = xlCalculationManual
 
 For Each file In fso.GetFolder(dtPath).Files
 If InStr(file.Name, ".xls") > 0 Then
 xName = StrConv(Left(file.Name, InStr(file.Name, ".xls") - 1), vbNarrow)
 Set dstCell = myWS.Columns("A").Find(xName, lookat:=xlWhole)
 If dstCell Is Nothing Then
 Set dstCell = myWS.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
 dstCell.Value = xName
 End If
 With Workbooks.Open(file.Path)
 Set srcCell = .Worksheets(1).Columns("A").Find("合計")
 dstCell.Offset(0, 1).Font.ColorIndex = 1
 If srcCell Is Nothing Then
 dstCell.Offset(0, 1).Value = "合計がありません"
 Else
 dstCell.Offset(0, 1).Value = srcCell.Offset(0, 1).Value
 End If
 .Close SaveChanges:=False
 End With
 End If
 Next
 Application.Calculation = xlCalculationAutomatic
 Application.ScreenUpdating = True
End Sub

にゃんころねさんのコメント
希望通りにいきました。ありがとうございます。
関連質問

●質問をもっと探す●



0.人力検索はてなトップ
8.このページを友達に紹介
9.このページの先頭へ
対応機種一覧
お問い合わせ
ヘルプ/お知らせ
ログイン
無料ユーザー登録
はてなトップ