1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自分がよく使う(かもしれない)Excel VBAのコード

Last updated at Posted at 2018-07-05

空き時間に書いたが、使うか分らない。
でもいつか使うかもしれないものをメモとしてここに記入。
「こんなん作らなくても」ってのもあるかもですけど。
コピペしたり、一部抜き出しで使ったり。

#ファイル一覧表を作る
ダイアログからディレクトリを選んでその中身を一覧で表示させる

まずはこれ

Sub 表作成()
    ActiveSheet.Range(Cells(1, 1), Cells(cell_number, 5)).Borders.LineStyle = xlContinuous
    ActiveSheet.Range(Cells(1, 1), Cells(cell_number, 5)).BorderAround Weight:=xlMedium
    ActiveSheet.Range(Cells(1, 1), Cells(1, 5)).BorderAround Weight:=xlMedium
    ActiveSheet.Range("A1:E1").HorizontalAlignment = xlCenter

    With Activesheet.Range("A1:E1").Interior
        .Color = RGB(248, 203, 173)
    End With

    ActiveSheet.Range("A1").Value = "ファイル名"
    ActiveSheet.Range("B1").Value = "サイズ"
    ActiveSheet.Range("C1").Value = "種類"
    ActiveSheet.Range("D1").Value = "パス"
    ActiveSheet.Range("E1").Value = "タイムスタンプ"

    Columns("A:E").AutoFit
End Sub

Sub ファイル情報取得(path As String)
    Dim file As Object, count As Long
    Dim folder As Object, pathname As String
    
    ActiveSheet.Cells.Clear
    count = 1
    With CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
        For Each folder In .GetFolder(path).SubFolders
            count = count + 1
            ActiveSheet.Cells(count, 1).Value = folder.Name
            ActiveSheet.Cells(count, 2).Value = folder.Size & "B"
            ActiveSheet.Cells(count, 3).Value = .GetFolder(folder).Type
            ActiveSheet.Cells(count, 4).Value = folder & Dir(folder)
            ActiveSheet.Cells(count, 5).Value = FileDateTime(folder)
        Next folder
        
        For Each folder In .GetFolder(path).Files
            count = count + 1
            ActiveSheet.Cells(count, 1).Value = file.Name
            ActiveSheet.Cells(count, 2).Value = FileLen(file) & "B"
            ActiveSheet.Cells(count, 3).Value = .GetFiler(file).Type
            ActiveSheet.Cells(count, 4).Value = file
            ActiveSheet.Cells(count, 5).Value = FileDateTime(file)
        Next folder
    End With
    
    Call 表作成(ActiveSheet.Cells(Rows.count, 1).End(xlUp).Row)
    
End Sub

KB,MB表記もしてみたいのですが、自分ではごちゃごちゃになりそうだったので諦めました。

最後

Sub ダイアログ()
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "フォルダを選択"
        If .Show = True Then
            Call ファイル情報取得(.SelectedItems(1))
        End If
    End With
End Sub

マクロ実行時はこのダイアログ()を選択する

#方眼紙マス作成
見出しの通り

Sub 方眼紙マス作成()
    ActiveSheet.Cells.ColumnWidth = 2
    ActiveSheet.Cells.RowHeight = 15.75
    With ActiveSheet.Cells.Interior
        .Color = RGB(255, 255, 255)
    End With
    ActiveSheet.Cells.Borders.LineStyle = xlDot
    ActiveSheet.Cells.BorderAround Weight:=xlMedium
End Sub

もっとうまく綺麗に書けるのかなと思ってはいますが、とりあえず想定通りに実行できるので残しておきます。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?