LoginSignup
0
1

More than 1 year has passed since last update.

VBAの基礎まとめ

Last updated at Posted at 2021-08-29

いつも、部品ごとにサイトを横断的に調べていたのでまとめています。

'これさえ引用できればVBAだいたいいける説

'変数の宣言=============================================
Dim i As Long
Dim j As Integer
Dim s As String
Dim o As Object

'シートの指定=============================================
Dim wsD As Worksheet
Set wsD = ThisWorkbook.Worksheets("出力用")

'シート削除・追加=============================================
Dim wsN As Worksheet
Set wsN = Worksheets.Add()
wsN.Name = "横山"
wsN.Activate
'------------もしくは
Worksheets.Add.Name = "横山"
End With

'行列削除・追加=============================================
Rows(2).Insert '2行目を挿入
Range("2:3").Insert '2~3行目を挿入
Columns(2).Insert
Range("B:C").Insert

.Delete '削除

'範囲コピー・ペースト=============================================
Range("A1:H9").Copy
Range("J18").PasteSpecial Paste:=xlPasteValues '値貼り

xlPasteFormulas '数式貼付け
xlPasteAll 'すべて貼付け

'最終行の取得=============================================
Dim maxRow As Long
maxRow = Cells(Rows.Count, 1).End(xlUp).Row

'最終列の取得=============================================
Dim maxCol As Long
maxCol = Cells(1, Columns.Count).End(xlToLeft).Column

'全ファイルを一つずつ処理=============================================
Dim buf As String
buf = Dir("C:\Users\YOKOYAMAKOREFUMI\Desktop\*.xlsx")
Do While buf <> ""
    Debug.Print buf 'ファイル名のみ
    filePath = folder & "\" & buf
    buf = Dir() '次のファイル取得
Loop

'フォルダ検索=============================================
Dim buf As String
buf = Dir("*", vbDirectory)
Do While buf <> ""
    Debug.Print buf 'ファイル名のみ
    folderPath = folder & "\" & buf
    buf = Dir() '次のフォルダ取得
Loop

'ファイルを開く・保存して閉じる・保存せず閉じる=============================================
Dim filePath As String
filePath = "~~~"
Workbooks.Open filePath '開く
Workbooks(filePath).Close SaveChanges:=True '保存して閉じる
Workbooks(filePath).Close SaveChanges:=False '保存しないで閉じる

'ファイルの複製・削除・移動=============================================
FileCopy "コピー元", "コピー先"
Kill "ファイルパス"
Name "移動元" As  "移動先"

'繰り返し=============================================
For i = 2 To max
    処理
Next i

'ステータスバーの表示=============================================
Application.Statusbar = "ここに表示"

'メッセージボックス=============================================
MsgBox "完了"

'自動計算On=============================================
Application.Calculation = xlCalculationAutomatic '自動計算モード

'見た目On-Off=============================================
Application.ScreenUpdating = False '見た目OFF
Application.ScreenUpdating = True '見た目ON

0
1
2

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
0
1