業務効率化&Web制作学習におすすめ!Udemyをチェックする! Udemy公式HP

Power Automate for desktop Excel転記事例 予定に合わせて行・列をずらしながら

当ページのリンクには広告が含まれています。
オカヘイ
Power Automate for desktopを使ってExcelの転記をしたいと思ったことはないでしょうか。またスケジュールに合わせて日にちや予定ごとに行・列をずらしながら書き込みたいときは、どんな風にフローを作っていけばいいでしょうか。自動で行数の判定も行い、足りない時は行を挿入しながら書き込む。今回は実際に作成したデモも交えて紹介します。
目次

予定に合わせてExcelの行・列をずらしながら転記 完成形

今回想定しているパターンは以下の通りです。

①日時の含まれる予定一覧(Excel)から、➁チームで共有する予定表(Excel)に月・日を判別しながら自動入力する。

【①日時の含まれる予定一覧(反映用.xlsx)】Y列の内容を

【➁チームで共有する予定表(予定表.xlsx)】この予定表に月・日を合わせて入力させる。今回は三郎さんの予定に追加。

【完成形】各自の予定の行はもともと4行でしたが、9/5の予定は6件あり、予定が行数を上回っています。その時は判定させ自動で行を挿入(15・16行目)したうえで書き込みさせています。

完成形のデモはこちら。

それでは詳細をお伝えしていきます。

予定に合わせてExcelの行・列をずらしながら転記 準備編

準備物は大きく3つあります。

ポイント

  1. 日時の含まれる予定一覧(反映用.xlsx)
  2. 書き込み先の予定表(予定表.xlsx)
  3. 今回実施するPADのフロー

一つ一つ詳細をご説明します。

1.日時の含まれる予定表(反映用.xlsx)

反映用シート

ポイント

A列~E列まではOutlookの予定表からエクスポートしたCSVを貼り付けています。(F~V列は特に不要なので非表示にしています)

WXY列には関数をいれて転記する際に使用します。ここを手で追加しておきます。

2行目に入れている関数は以下の通りです。

W列(月)=MONTH(B2)

X列(日)=DAY(B2)

Y列(内容)=A2&” ”&TEXT(C2,”hh:mm”)&”~”&TEXT(E2,”hh:mm”)
Y列はA列の件名+開始時間~終了時間をとってきています。この内容を転記します。

2行目に入れてたら必要であろう行数までオートフィルしてください。

行カウント用シート

別シート(行カウント用)に自身の行数カウント判定させるための関数も作成します。

ポイント

A2=MODE.MULT(IF(反映用!X:X<>0,反映用!X:X))

B2=COUNTIFS(反映用!X:X,行カウント用!A2,反映用!X:X,”<>0″)

反映用X:Xは反映用シートの日にちを参照しています。

MODE.MULT関数で一番重複が多い日を表示させ、COUNTIFS関数で実際に何件あるのかをカウントしています。

5日が一番多く、件数は6件であることが分かります。

2.書き込み先の予定表(予定表.xlsx)

次に予定表です。

月でシートがまず分かれています。

列は1日ずつ、行は4行ごとに人が割り振られているイメージです。

この表での加工は1点だけです。

後程PADで自分の行数を判定・4行をこえて入力が必要な時は行を自動挿入させます。

その時のアクションで必要になるので、三郎さんの下の四郎さんのセルを名前つきセルにします

今回は「先頭行」という名前をつけました。

3.Power Automate for desktop(PAD)のフロー

いよいよPADのフローについて解説していきます。

Excel起動から、読み取りまで

ポイント

【3行目】アクティブなExcelワークシートの設定は名前でシート名を指定

【4行目】Excelワークシートから列における最初の空の行を取得
で反映用.xlsxの予定が入っている行数をカウント(下図赤枠)

【5行目】Excelワークシートから読み取る
4行目でとったA列の行数(変数:FirstFreeROnColumn)から-1することで最終行までの範囲を選択(下図青枠)
変数:ExcelDataに青枠部分が指定されてたらOK
詳細で「範囲の最初の行に列名が含まれています」をON

対象月判定

ポイント

【7行目】変数の設定で読み取った「月」を設定する

【8~12行目】予定表.xlsxのシートが4月スタートのため、IF文をつかって条件分岐する
9月のときは9-3=6 変数:Monthを調整し、数をワークシートのインデックス番号と合わせる

【13行目】インデックスで指定

【予定表.xlsxのシート部分/インデックス番号:6】

行追加判定

ポイント

【15~16行目】反映用.xlsxの行カウント用シートB2セルをExcelData2に保存

【17~18行目】予定表.xlsxで設定した名前付きセル「先頭行」をアクティブ化し、アクティブなセルを取得で変数:ActiveCellRowIndexに保存

【19~20行目】変数:RowsCountに一旦ExcelData2に入れたのち、変数:ActiveCellRowIndex-11(この11は三郎さんの書き込み開始行数)分小さくする
ここで三郎さんの最終行-開始行をすることで今三郎さんは何行設定あるのかを判定する

ポイント

【21~34行目】RowsCountが今追加すべき行数になっているので、Switch・Caseでそれぞれ行数を追加する
今回は3行まで追加し、4行以上ではメッセージを出す仕様

入力日の複数行に対する変数設定

ポイント

【36~66行目】書き込み時に行数をずらすための変数:NewVar1~31(月の日付分)を設定する

For each を使用して日数にあわせて書き込み

ポイント

【67~160行目】変数:CurrentItem[23]は「日」、変数:CurrentItem[24]は書き込む「内容」、+11は三郎さんの開始行数を設定

最後に完了した旨のメッセージを表示すれば終了です。

必要があればExcelの保存や閉じるアクションを追加してください。

注意点

今回の仕様は以下の点に注意が必要です。

注意ポイント

  • 月またぎには非対応、あくまで反映用.xlsxの最初の行の月数にて予定表の入力シート判別しています。
  • 書き込み先の予定表.xlsxは行の自動挿入があった場合、罫線等の見た目がくずることあります。すみませんが、そこは手修正ください。
  • 書き込み先の予定表.xlsxの書き込むセルは結合しないでください。
  • 行挿入は3行までで設定しています、それ以上の場合はフローの修正が必要です【32~34あたり】。

まとめ

予定に合わせて行・列ずらしながらExcel転記ついてご紹介しました。

全く同じ環境ではないと思うので、そのまま使うことは難しいと思いますが、カウントの方法など使えそうな部分はよかったら取り入れてみてください。

またもっと上手な書き方があればぜひメッセージくださると嬉しいです。

読んでくださった方の参考に少しでもなれば幸いです。

おまけ 今回のコード

最後に今回のコードを掲載します。

PADに貼り付けして、適宜必要な個所は変更してみてくださいね。

 html
Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $”’C:\\Users\\user\\Desktop\\Outlook予定\\反映用.xlsx”’ Visible: True ReadOnly: False Instance=> ExcelInstance
Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $”’C:\\Users\\user\\Desktop\\Outlook予定\\予定表.xlsx”’ Visible: True ReadOnly: False Instance=> ExcelInstance2
Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: $”’反映用”’
Excel.GetFirstFreeRowOnColumn Instance: ExcelInstance Column: 1 FirstFreeRowOnColumn=> FirstFreeRowOnColumn
Excel.ReadFromExcel.ReadCells Instance: ExcelInstance StartColumn: $”’A”’ StartRow: 1 EndColumn: $”’Y”’ EndRow: FirstFreeRowOnColumn – 1 ReadAsText: False FirstLineIsHeader: True RangeValue=> ExcelData
# 対象月判定
SET Month TO ExcelData[0][22]
IF Month >= 4 THEN
Variables.DecreaseVariable Value: Month DecrementValue: 3
ELSE
Variables.IncreaseVariable Value: Month IncrementValue: 9
END
Excel.SetActiveWorksheet.ActivateWorksheetByIndex Instance: ExcelInstance2 Index: Month
# 行追加判定
Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: $”’行カウント用”’
Excel.ReadFromExcel.ReadCell Instance: ExcelInstance StartColumn: $”’B”’ StartRow: 2 ReadAsText: False CellValue=> ExcelData2
Excel.ActivateCellInExcel.ActivateNamedCell Instance: ExcelInstance2 CellName: $”’先頭行”’
Excel.GetActiveCell Instance: ExcelInstance2 ActiveCellColumnIndex=> ActiveCellColumnIndex ActiveCellRowIndex=> ActiveCellRowIndex
SET RowsCount TO ExcelData2
Variables.DecreaseVariable Value: RowsCount DecrementValue: ActiveCellRowIndex – 11
SWITCH RowsCount
CASE <= 0
CASE = 1
Excel.InsertRow Instance: ExcelInstance2 Index: ActiveCellRowIndex
CASE = 2
Excel.InsertRow Instance: ExcelInstance2 Index: ActiveCellRowIndex
Excel.InsertRow Instance: ExcelInstance2 Index: ActiveCellRowIndex
CASE = 3
Excel.InsertRow Instance: ExcelInstance2 Index: ActiveCellRowIndex
Excel.InsertRow Instance: ExcelInstance2 Index: ActiveCellRowIndex
Excel.InsertRow Instance: ExcelInstance2 Index: ActiveCellRowIndex
DEFAULT
Display.ShowMessageDialog.ShowMessage Message: $”’追加行数が4行以上です!PADを止めて確認してください。”’ Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True ButtonPressed=> ButtonPressed
END
# 入力日の複数行に対する変数設定
SET NewVar1 TO 0
SET NewVar2 TO 0
SET NewVar3 TO 0
SET NewVar4 TO 0
SET NewVar5 TO 0
SET NewVar6 TO 0
SET NewVar7 TO 0
SET NewVar8 TO 0
SET NewVar9 TO 0
SET NewVar10 TO 0
SET NewVar11 TO 0
SET NewVar12 TO 0
SET NewVar13 TO 0
SET NewVar14 TO 0
SET NewVar15 TO 0
SET NewVar16 TO 0
SET NewVar17 TO 0
SET NewVar18 TO 0
SET NewVar19 TO 0
SET NewVar20 TO 0
SET NewVar21 TO 0
SET NewVar22 TO 0
SET NewVar23 TO 0
SET NewVar24 TO 0
SET NewVar25 TO 0
SET NewVar26 TO 0
SET NewVar27 TO 0
SET NewVar28 TO 0
SET NewVar29 TO 0
SET NewVar30 TO 0
SET NewVar31 TO 0
LOOP FOREACH CurrentItem IN ExcelData
SWITCH CurrentItem[23]
CASE = 1
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar1 + 11
Variables.IncreaseVariable Value: NewVar1 IncrementValue: 1
CASE = 2
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar2 + 11
Variables.IncreaseVariable Value: NewVar2 IncrementValue: 1
CASE = 3
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar3 + 11
Variables.IncreaseVariable Value: NewVar3 IncrementValue: 1
CASE = 4
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar4 + 11
Variables.IncreaseVariable Value: NewVar4 IncrementValue: 1
CASE = 5
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar5 + 11
Variables.IncreaseVariable Value: NewVar5 IncrementValue: 1
CASE = 6
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar6 + 11
Variables.IncreaseVariable Value: NewVar6 IncrementValue: 1
CASE = 7
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar7 + 11
Variables.IncreaseVariable Value: NewVar7 IncrementValue: 1
CASE = 8
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar8 + 11
Variables.IncreaseVariable Value: NewVar8 IncrementValue: 1
CASE = 9
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar9 + 11
Variables.IncreaseVariable Value: NewVar9 IncrementValue: 1
CASE = 10
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar10 + 11
Variables.IncreaseVariable Value: NewVar10 IncrementValue: 1
CASE = 11
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar11 + 11
Variables.IncreaseVariable Value: NewVar1 IncrementValue: 1
CASE = 12
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar12 + 11
Variables.IncreaseVariable Value: NewVar12 IncrementValue: 1
CASE = 13
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar13 + 11
Variables.IncreaseVariable Value: NewVar13 IncrementValue: 1
CASE = 14
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar14 + 11
Variables.IncreaseVariable Value: NewVar14 IncrementValue: 1
CASE = 15
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar15 + 11
Variables.IncreaseVariable Value: NewVar15 IncrementValue: 1
CASE = 16
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar16 + 11
Variables.IncreaseVariable Value: NewVar16 IncrementValue: 1
CASE = 17
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar17 + 11
Variables.IncreaseVariable Value: NewVar17 IncrementValue: 1
CASE = 18
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar18 + 11
Variables.IncreaseVariable Value: NewVar18 IncrementValue: 1
CASE = 19
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar19 + 11
Variables.IncreaseVariable Value: NewVar19 IncrementValue: 1
CASE = 20
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar20 + 11
Variables.IncreaseVariable Value: NewVar20 IncrementValue: 1
CASE = 21
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar21 + 11
Variables.IncreaseVariable Value: NewVar21 IncrementValue: 1
CASE = 22
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar22 + 11
Variables.IncreaseVariable Value: NewVar22 IncrementValue: 1
CASE = 23
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar23 + 11
Variables.IncreaseVariable Value: NewVar23 IncrementValue: 1
CASE = 24
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar24 + 11
Variables.IncreaseVariable Value: NewVar24 IncrementValue: 1
CASE = 25
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar25 + 11
Variables.IncreaseVariable Value: NewVar25 IncrementValue: 1
CASE = 26
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar26 + 11
Variables.IncreaseVariable Value: NewVar26 IncrementValue: 1
CASE = 27
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar27 + 11
Variables.IncreaseVariable Value: NewVar27 IncrementValue: 1
CASE = 28
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar28 + 11
Variables.IncreaseVariable Value: NewVar28 IncrementValue: 1
CASE = 29
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar29 + 11
Variables.IncreaseVariable Value: NewVar29 IncrementValue: 1
CASE = 30
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar30 + 11
Variables.IncreaseVariable Value: NewVar30 IncrementValue: 1
CASE = 31
Excel.WriteToExcel.WriteCell Instance: ExcelInstance2 Value: CurrentItem[24] Column: CurrentItem[23] + 1 Row: NewVar31 + 11
Variables.IncreaseVariable Value: NewVar31 IncrementValue: 1
END
END
Display.ShowMessageDialog.ShowMessage Message: $”’作業が完了しました!”’ Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: True ButtonPressed=> ButtonPressed2
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次