
マクロ初心者です。
教えてください。
###前提・実現したいこと
マクロでC列に合計数が書式で出るようにしたい。
###発生している問題・エラーメッセージ
CSVデータをシートに取り込みました。
同一ブック内に集計表の原紙シートを置き、マクロ実行でコピー&集計されるようにしています。(添付画像)
For Each c In .Range("P1,X1,AA1")
のAA1の部分がCSVから取り込んだ合計数なんですが、当然、ただの数値なので、
集計値をいじっても反映されません・・・。
マクロの最後に、
'C列に合計
With .Cells(4, "C").Resize(.Rows.Count - 2)
.FormulaR1C1 = "=SUM(RC[1]:RC[" & dicE.Count & "])"
End With
としましたが、.Rowsで「参照が不正または不完全です。」と言われてしまいます。
Option Explicit Private dstCol As Long Sub 集計表改() Dim c As Range, col As Long Dim 集計表A1 As Variant Dim d As Long Dim v As Long ActiveSheet.Copy Before:=Worksheets("CSV取り込み") 集計表A1 = ActiveSheet.Name dstCol = 0 With Worksheets("CSV取り込み") For Each c In .Range("P1,X1,AA1") cpy c Next c For col = Columns("AN").Column To Columns("IA").Column Step 3 cpy .Cells(1, col) Next col End With '表の色設定() Dim データ範囲 As Range Dim データ数 As Long Dim 行 As Long With Range("A4").CurrentRegion データ数 = .Rows.Count - 1 Set データ範囲 = .Offset(2).Resize(データ数) End With データ範囲.Interior.ColorIndex = xlColorIndexNone For 行 = 2 To データ数 Step 3 データ範囲.Rows(行).Interior.ColorIndex = 15 Next Set データ範囲 = Nothing With ActiveSheet.UsedRange .EntireColumn.AutoFit '行の幅を自動調節 .Borders.LineStyle = xlContinuous '枠線を引く .HorizontalAlignment = xlCenter '数値を中央位置 With .Offset(3, 2).Resize(.Rows.Count - 3, .Columns.Count - 2) .Font.Size = 16 '16point に大きくしています .Font.Bold = True End With End With 'inputboxでシート名の入力 Dim ans As String ' InputBoxの戻り Dim flg As Boolean ' 数値かどうかの判定フラグ flg = False Do ans = InputBox("シート名を入力してください", "シート名入力") If StrPtr(ans) = 0 Then Exit Sub ' キャンセル時に終了 If IsNumeric(ans) Then flg = True Loop Until flg = True ActiveSheet.Name = ans 'ボタンを削除 Dim Obj As Object For Each Obj In ActiveSheet.Shapes Obj.Select Application.DisplayAlerts = False Obj.Delete Application.DisplayAlerts = True Next Obj End Sub Private Sub cpy(Target As Range) Dim st As Worksheet Dim srcCol As Long Dim lastRow As Long Dim srcCells As Range Set st = Target.Parent srcCol = Target.Column lastRow = st.Cells(Rows.Count, srcCol).End(xlUp).Row Set srcCells = Range(st.Cells(2, srcCol), st.Cells(lastRow, srcCol)) srcCells.Copy Cells(4, dstCol + 1) '全体がずれる dstCol = dstCol + 1 ' 'C列に合計 ' With .Cells(4, "C").Resize(.Rows.Count - 2) ' .FormulaR1C1 = "=SUM(RC[1]:RC[" & dicE.Count & "])" ' End With End Sub
回答3件
あなたの回答
tips
プレビュー