vba Excel 顺时针旋转(不转置)表格 90 度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40778265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:35:59  来源:igfitidea点击:

Excel rotate (NOT transpose) a table 90 degrees clockwise

excelexcel-vbams-officevba

提问by Quan Nguyen

I have a table in Excel. How can I rotate (NOT transpose) a table 90 degrees clockwise?

我有一张 Excel 表格。如何将桌子顺时针旋转(不转置)90 度?

Example

例子

cell1  |  cell2  | cell3
cell4  |  cell5  | cell6

Expected

预期的

cell3  |  cell6
cell2  |  cell5
cell1  |  cell4

P/S: If I use transpose option when paste, I will have unexpected result

P/S: 如果我在粘贴时使用转置选项,我会得到意想不到的结果

cell1  |  cell4
cell2  |  cell5
cell3  |  cell6

Thanks

谢谢

回答by rohrl77

Assuming you want to do this manually, and that it doesn't need to be done via functions:

假设您想手动执行此操作,并且不需要通过函数来​​完成:

  1. First transpose your range using the regular copy transpose paste function
  2. Next, create an artificial sorting key (simply number is 1 to n) infront of the first column and sort in a descending order
  3. Delete the sorting column you created in front of the first row
  1. 首先使用常规复制转置粘贴功能转置您的范围
  2. 接下来,在第一列的前面创建一个人工排序键(简单的数字是1到n)并按降序排序
  3. 删除你在第一行前面创建的排序列

回答by L.Dutch - Reinstate Monica

You have to go in a two steps process:

您必须分两步走:

  1. transpose your table (you have already done it)
  2. "mirror" your table
  1. 转置你的表格(你已经完成了)
  2. “镜像”你的桌子

To do this last step, consider that you have a table M rows x N columns, which is transposed into a N rows x M columns. The cell located at row iand column jhas to be moved to the row i-1 + Nand column j.

要完成这最后一步,请考虑您有一个 M 行 x N 列的表,该表被转置为 N 行 x M 列。位于第i行和第j列的单元格必须移动到第i-1 + N 行和第j列。

In your example, after transposition cell 1 is in (1,1) with N = 3, and it will go to (1-1+3,1)=(3,1) and so on.

在您的示例中,在换位后,单元格 1 位于 (1,1) 且 N = 3,它将变为 (1-1+3,1)=(3,1) 等等。

You need to use a short macro to do the transformation.

您需要使用一个简短的宏来进行转换。