[[20170324195145]] 『数式の入った最終行を取得し、貼り付けるには』(takao) ページの最後に飛ぶ

[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]

 

『数式の入った最終行を取得し、貼り付けるには』(takao)

数式の入ったD、E列(値のある最終行)をコピーし同一シート内の
O1へ値貼付と罫線を引きたいのですがご教授お願いします。
以下が取得したマクロです。
Sub Macro2()

    Range("D2:E52").Select
    Selection.Copy

    Range("O1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ThemeColor = 6
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ThemeColor = 6
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ThemeColor = 6
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ThemeColor = 6
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ThemeColor = 6
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ThemeColor = 6
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("N23").Select
End Sub

< 使用 Excel:Excel2013、使用 OS:Windows10 >


 >数式の入ったD、E列(値のある最終行)をコピーし

 数式があっても、値がない(""になっている)ところは転記と罫線設定対象外ですか?
 また、D列とE列の最終セルは同じ行ですか?(E列のほうがD列より下のほうまであるといったことはあるのですか?)

(β) 2017/03/24(金) 20:20


返事ありがとうございます。
>数式があっても、値がない(""になっている)ところは転記と罫線設定対象外ですか?
はいそうです""部分は無視したい。

>また、D列とE列の最終セルは同じ行ですか?(E列のほうがD列より下のほうまであるといったことはあるのですか?)
同じ最終です。
すみません。よろしくお願いします。
(takao) 2017/03/24(金) 20:29


 以下は参考になりますか?

 Sub test()
    Dim f As Range

    Set f = Columns("D").Find(what:="*", LookIn:=xlValues, SearchDirection:=xlPrevious)
    MsgBox f.Address  'または MsgBox f.Row

 End Sub

(β) 2017/03/24(金) 20:38


(β)さん
ありがとうございます。
$D$52と最終行の取得はできました。が
この先どうしていいものか??
・・・
(takao) 2017/03/24(金) 20:50

 >この先どうしていいものか?? 

 本当は Select して Selection相手の処理は感心しませんが、とりあえずは

 Range("D2:E52").Select  これを Range("D2:E" & f.Row).Select
 にすれば、目的は達成できるかと思います。

 ただ、最初に、O:P列の罫線を削除しておいたほうがよろしいですね。
 これは、マクロ記録でコードが入手できます。

(β) 2017/03/24(金) 21:09


 まずは、↑の対応をしてみてください。

 以下は【参考コード】です。

 Sub Sample()
    Dim f As Range

    With Columns("O:P")
        .Borders.LineStyle = xlNone
        .ClearContents
    End With

    Set f = Columns("D").Find(what:="*", LookIn:=xlValues, SearchDirection:=xlPrevious)

    With Range("D2", f).Resize(, 2)
        Range("O1").Resize(.Rows.Count, .Columns.Count).Value = .Value
        With Range("O1").Resize(.Rows.Count, .Columns.Count).Borders
            .LineStyle = xlContinuous
            .ThemeColor = 6
            .Weight = xlThin
        End With
    End With

 End Sub

(β) 2017/03/24(金) 21:19


(β)さん
色々試したり確認やらで
返事遅くなりました
> Range("D2:E52").Select これを Range("D2:E" & f.Row).Select
> にすれば、目的は達成できるかと思います。
範囲の取得が少し解った気がします。で
以下のように、ちょい修正し表らしくなったのですがO列の最下行に
"=SUM(O2:最下行)"といれ合計金額を表示させたいのですができませんか?
こんな感じです よろしくお願いします。
    O            P
1  金額         項目
2  150         ○○
3  320         ◆
4
5
6
・
・
50たとえば50行目が最終行とします。
51 #,##0  合計金額

O51=SUM(O2:O50) P51="合計金額"を挿入

 Sub Sample2()
    Dim f As Range

    With Columns("O:P")
        .Borders.LineStyle = xlNone
        .ClearContents
    End With

    Set f = Columns("D").Find(what:="*", LookIn:=xlValues, SearchDirection:=xlPrevious)

    With Range("D2", f).Resize(, 2)
        Range("O2").Resize(.Rows.Count, .Columns.Count).Value = .Value
        With Range("O1").Resize(.Rows.Count + 1, .Columns.Count).Borders
            .LineStyle = xlContinuous
            .ThemeColor = 6
            .Weight = xlThin
        End With
    End With
Range("O1").Value = "金額"
Range("P1").Value = "項目"
 End Sub
(takao) 2017/03/25(土) 01:20

 いろんな領域指定がありますが一例です。

 Sub Sample3()
    Dim f As Range

    With Columns("O:P")
        .Borders.LineStyle = xlNone
        .ClearContents
    End With

    Set f = Columns("D").Find(what:="*", LookIn:=xlValues, SearchDirection:=xlPrevious)

    With Range("D2", f).Resize(, 2)
        Range("O2").Resize(.Rows.Count, .Columns.Count).Value = .Value
        With Range("O1").Resize(.Rows.Count +2, .Columns.Count).Borders
            .LineStyle = xlContinuous
            .ThemeColor = 6
            .Weight = xlThin
        End With
    End With

    Range("O1:P1").Value = Array("金額", "項目")

    With Range("O" & Rows.Count).End(xlUp).Offset(1)
        .FormulaR1C1 = "=SUM(R2C:R[-1]C)"
        .Offset(, 1).Value = "合計金額"
    End With

 End Sub

(β) 2017/03/25(土) 07:32


(β)さん
外出しており連絡遅くなりました。
+1 がなぜ +2なのか解りませんが
うまくできました。
ありがとうございました。

(takao) 2017/03/25(土) 14:37


コメント返信:

[ 一覧(最新更新順) ]


YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki. Modified by kazu.