2016-05-17 17 views
0

このマクロは、一度に1つのセルに適用する(または複数の行にドラッグすると、一番左上のセルの行に作用します)場合に機能します。ユーザーが一括して行を変更できるように、選択したすべてのセルの行に変更を適用するマクロを取得するために、それをさらに微調整する方法はありますか?1つのセルから複数のセルへのExcel VBAマクロ

最後に8行に1つの行を分割するマクロを記録しましたcolumns J:Q ロジックは、選択されたセル(マージ対象のセルの下に存在する)の上に7行挿入し、 columns A:I

のためのオリジナルの既存の行と行は、これは私にA:Iのための1つのセルと、私はこのコードのより多くの意味を理解し、ずっとそれを作るためにいくつかの調整を行った

*See macro below 



Sub splitrowsandmerge() 
' 
' splitrowsandmerge Macro 
' add 7 rows and merge 8 rows for first 9 columns 
' 
' Keyboard Shortcut: Ctrl+Shift+E 
' 
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select 
Selection.Insert Shift:=xlDown 
Selection.Insert Shift:=xlDown 
Selection.Insert Shift:=xlDown 
Selection.Insert Shift:=xlDown 
Selection.Insert Shift:=xlDown 
Selection.Insert Shift:=xlDown 
Selection.Insert Shift:=xlDown 
ActiveCell.Offset(-1, 0).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlLTR 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
ActiveCell.Offset(0, 1).Range("A1:A8").Select 
With Selection 
    .HorizontalAlignment = xlCenter 
    .VerticalAlignment = xlTop 
    .WrapText = True 
    .Orientation = 0 
    .AddIndent = False 
    .IndentLevel = 0 
    .ShrinkToFit = False 
    .ReadingOrder = xlContext 
    .MergeCells = False 
End With 
Selection.Merge 
End Sub 
+0

私は、複数の行を選択することによって何を意味するかについてもう少し詳しく知る必要があると思います。したがって、A1とB5を選択した場合、A1とB5から実行したいのですが、異なる列で同じ結果が得られますか? – Histerical

+1

最初の問題は「ActiveCell」を使用していることです。 ActiveCellは1つのセルで、選択されたセルの左上のほとんどのセルとして定義されます。 「選択」を使いたい「ActiveCell」の代わりに – OpiesDad

+1

2番目の問題は、ユーザーがばらばらの範囲を選択した場合です。これを越えるには、split関数を使います:rangeArray = Split(Selection.Address、 "、")そして配列をループします。 – OpiesDad

答えて

0

J:Rowエンドのための8行を与えます容易に読むことができるgh。これは、私があなたがしようとしていることを理解するためにもう少し情報が必要なので、あなたの元の質問に答えるものではありません。しかし、これは自分自身と他の人がコードを読みやすくするのに役立ちます。

私は、あなたが選択した行のA列からI列までのすべての列を下に挿入された7行とマージするようにしたい場合、あなたが探していたものを推測し、

Sub splitrowsandmerge() 
' 
' splitrowsandmerge Macro 
' add 7 rows and merge 8 rows for first 9 columns 
' 
' Keyboard Shortcut: Ctrl+Shift+E 
' 

Dim RowArray() As Integer 

check = 0 

For Each cell In Selection 
    If firstTime <> 1 Then 
     ReDim RowArray(0) As Integer 
     RowArray(0) = cell.Row 
     firstTime = 1 
    Else 

     For i = LBound(RowArray) To UBound(RowArray) 
      If RowArray(i) = cell.Row Then 
       check = 1 
       Exit For 
      End If 
     Next i 

     If check <> 1 Then 
      addOne = addOne + 1 
      ReDim Preserve RowArray(addOne) As Integer 
      RowArray(addOne) = cell.Row 
     End If 

     check = 0 
    End If 
Next cell 

RowArray = BubbleSrt(RowArray, False) 
For i = LBound(RowArray) To UBound(RowArray) 

    startCell = RowArray(i) 
    Rows(startCell + 1).EntireRow.Resize(7).Insert 

    With Range(Cells(startCell, 1), Cells(startCell + 7, 9)) 
     .HorizontalAlignment = xlCenter 
     .VerticalAlignment = xlTop 
     .WrapText = True 
     .Orientation = 0 
     .AddIndent = False 
     .IndentLevel = 0 
     .ShrinkToFit = False 
     .ReadingOrder = xlContext 
     .MergeCells = False 
    End With 

    For j = 1 To 9 
     Range(Cells(startCell, j), Cells(startCell + 7, j)).Merge 
    Next j 
Next i 

End Sub 

Public Function BubbleSrt(ArrayIn, Ascending As Boolean) 

Dim SrtTemp As Variant 
Dim i As Long 
Dim j As Long 


If Ascending = True Then 
    For i = LBound(ArrayIn) To UBound(ArrayIn) 
     For j = i + 1 To UBound(ArrayIn) 
      If ArrayIn(i) > ArrayIn(j) Then 
       SrtTemp = ArrayIn(j) 
       ArrayIn(j) = ArrayIn(i) 
       ArrayIn(i) = SrtTemp 
      End If 
     Next j 
    Next i 
Else 
    For i = LBound(ArrayIn) To UBound(ArrayIn) 
     For j = i + 1 To UBound(ArrayIn) 
      If ArrayIn(i) < ArrayIn(j) Then 
       SrtTemp = ArrayIn(j) 
       ArrayIn(j) = ArrayIn(i) 
       ArrayIn(i) = SrtTemp 
      End If 
     Next j 
    Next i 
End If 

BubbleSrt = ArrayIn 

End Function 
+0

現在、私は1行のデータを持っています。私は列Aのための1つのセルを持っていると思います:私は8つのセルを列Aの1つのセルに対して相対的な列J:Q:私は例:空白のワークシートで私はA1:A8のセルをマージした場合、私は私の結果を持っています。 – James

+0

したがって、A1:A8をインスタンス化し、すべての列を同じ方法でマージするようにしますか?私はあなたがJ:Qの意味を混同しています。 – Histerical

+0

A1:A8がマージされるのを訂正してください。私は既にデータを持っている100以上の行にこれを適用する必要があるため、挿入行の手法を使用しました。 – James

関連する問題