メインコンテンツへスキップ

列を行に転置し、新しい行を挿入してExcelでデータを入力するにはどうすればよいですか?

この記事では、以下のスクリーンショットに示すように、列を行に転置し、新しい行を挿入してデータを入力するVBAコードを紹介します。この操作に興味がある場合は、以下にアクセスして詳細を確認してください。
doc転置挿入行1

VBAを使用して行を転置および挿入します


矢印青い右バブル VBAを使用して行を転置および挿入します

範囲を転置して行を挿入して同じデータを入力する直接的な方法はありませんが、ここでは、このタスクをすばやく解決するためのVBAコードを紹介します。

1。 押す Altキー+ F11 開くキー アプリケーション向け Microsoft Visual Basic 窓。

2。 クリック インセット > モジュール、VBAコードの下をモジュールウィンドウに貼り付けます。 スクリーンショットを参照してください:

VBA:行を転置して挿入します。

 Sub TransposeInsertRows()
'UpdatebyExtendoffice20161125
    Dim xRg As Range
    Dim i As Long, j As Long, k As Long
    Dim x As Long, y As Long
    Set xRg = Application.InputBox _
    (Prompt:="Range Selection...", _
    Title:="Kutools For Excel", Type:=8)
    Application.ScreenUpdating = False
    x = xRg(1, 1).Column + 2
    y = xRg(1, xRg.Columns.Count).Column
    For i = xRg(xRg.Rows.Count, 1).Row To xRg(1, 1).Row Step -1
        If Cells(i, x) <> "" And Cells(i, x + 1) <> "" Then
            k = Cells(i, x - 2).End(xlToRight).Column
            If k > y Then k = y
            For j = k To x + 1 Step -1
                Cells(i + 1, 1).EntireRow.Insert
                With Cells(i + 1, x - 2)
                    .Value = .Offset(-1, 0)
                    .Offset(0, 1) = .Offset(-1, 1)
                    .Offset(0, 2) = Cells(i, j)
                End With
                Cells(i, j).ClearContents
            Next j
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

 

doc転置挿入行2

3。 押す F5 キーを押してコードを実行し、転置するデータ範囲を選択して、ポップダイアログに行を挿入します。 スクリーンショットを参照してください:
doc転置挿入行3

4。 クリック OK。 そして、選択した範囲は、以下のスクリーンショットに示すように変換されています。
doc転置挿入行4

Note:コードを実行する前に、元のデータのコピーを保存することをお勧めします。

ヒント。クロステーブルをリストテーブルに、またはその逆にすばやく転置したい場合は、Kutools forExcelを使用してみてください。 転置テーブルの寸法 次のスクリーンショットに示すように。 30日で無制限にフル機能、 今すぐダウンロードして無料トライアルをお試しください。

doc転置クロスをリストに

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Compiler errorSyntax Error
Dim xRg As Range
Dim i As Long, j As Long, k As Long
Dim x As Long, y As Long

This comment was minimized by the moderator on the site
The above code works great. It keeps the first two columns. How would I change the code so that it only keeps the first column?
This comment was minimized by the moderator on the site
How can I transpose the data every two cells

The initial exercise is:


Class1 | Name | Nancy | Judy | Lily | Daniel
Class2 | Name | Bruce | Uee | Perter | Daniel
Class3 | Name | kate | Kelly | Wenddy | Daniel

and I need it to look like this:

Class1 | Name | Nancy | Judy
Class1 | Name | Lily | Daniel
Class 2 | Name | Bruce | Uee
Class 2 | Name | Perter | Daniel
Class3 | Name | kate | Kelly
Class3 | Name | Wenddy | Daniel
This comment was minimized by the moderator on the site
how can I make data 3 and 4 remain in the same column:


the initial exercise is:

Class1 | Name | Nancy | Judy | Lily | Daniel
Class2 | Name | Bruce | Uee | Perter | Daniel
Class3 | Name | kate | Kelly | Wenddy | Daniel

I need to transpose it like this

Class1 | Name | Nancy | Judy
Class1 | Name | Lily | Daniel
Class 2 | Name | Bruce | Uee
Class 2 | Name | Perter | Daniel
Class3 | Name | kate | Kelly
Class3 | Name | Wenddy | Daniel
This comment was minimized by the moderator on the site
how would i change the VBA code so that it will keep more of the columns the same? for example instead of the first two columns staying the same it will be the first 4?
This comment was minimized by the moderator on the site
I am not sure, may be you can try to change x = xRg(1, 1).Column + 2 to x = xRg(1, 1).Column + 4.
This comment was minimized by the moderator on the site
If my cross table's informations come from formulas, those formulas don't always follow when the macro is applied. How could I fix this to make sure that the formulas are kept in the cells?
This comment was minimized by the moderator on the site
Hi, thank u for ur message. For solving your problem, you can apply Kutools' Convert Formula to Text before you running the macro, then after transposing thetable, apply Kutools' Convert Text to Formula. Please Let me know if it works. Thank u.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations