excel - 将多列表转换为具有重复模式的单列
问题描述
假设您在 Excel 中有一个列范围(表格),如下所示:
并需要将其转换为如下格式:
我已经搜索了像这里这样的 VBA 脚本,市场上有几种工具,但无法让它在所需的输出中工作。
如何使用常规 Excel 公式或 VBA 脚本做到这一点?
解决方案
尝试:
Option Explicit
Sub test()
Dim LC As Long, LR As Long, Number As Long, i As Long, j As Long, NR As Long
Dim str As String
'Everything included in "With Statement" refer to Sheet1
With ThisWorkbook.Worksheets("Sheet1")
'Find Last Column
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
'Find Las Row
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loop from column two to last column
For i = 2 To LC
'Number get the value of row 1 and column i
Number = .Cells(1, i).Value
'Loop from row two to last row of each i
For j = 2 To LR
'str get the value of row j in column 1
str = .Cells(j, 1).Value
NR = .Cells(.Rows.Count, "A").End(xlUp).Row
.Cells(NR + 1, 1).Value = Number
.Cells(NR + 1, 2).Value = str
.Cells(NR + 1, 3).Value = .Cells(j, i).Value
Next j
Next i
End With
End Sub
输出:
推荐阅读
- twilio - twilio 没有收到来自网站的消息
- visual-studio-code - Visual Studio代码集成终端无响应
- oracle - 更好的是在 for 循环中多次删除
- java - 如何在android webview中保存上次访问的url?
- aws-fargate - 如何将 Fargate 与 Media convert 连接起来?
- python - 如何比较python3中的两个csv文件?
- bash - 为什么此 YAD 列表代码无法正常工作?
- unix - Vim:在普通模式下切换键盘布局
- angular - Spring Gateway 如何路由深度 SPA 链接
- asp.net-core - 将 blazor 组件添加到现有 Asp.net 核心项目的问题