Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA将行转换为列_Vba_Excel - Fatal编程技术网

VBA将行转换为列

VBA将行转换为列,vba,excel,Vba,Excel,我在excel中有一个工作表,如下所示 name variant1 value1 variant2 value2 ----- -------- ------ -------- ------ name1 variantA1 valueA1 variantB1 valueB1 name2

我在excel中有一个工作表,如下所示

name          variant1          value1          variant2          value2
-----         --------          ------          --------          ------    
name1         variantA1         valueA1         variantB1         valueB1
name2         variantC2         valueC2         variantD2         valueD2
我想把它转换成这样一种结构:

name          variant          value
----          --------         ------
name1         
              variantA1        valueA1
              variantB1        valueB2
name2         
              variantC2        valueC2
              variantD2        valueD2
我会经常使用这个宏,每个名称可能有10个不同的变体,我不知道工作表中有多少行

我已经编写了VBA宏,但不确定为什么它不起作用:

Sub import_format()

Dim ws As Worksheet
Dim LastRow As Long, i As Long
Dim NumberOfVariants As Long
Dim j As Long
Dim ColumnIndex As Long


Set ws = Sheets("Sheet1") '~~> Name of the sheet which has the list
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
NumberOfVariants = 2
ColumnIndex = 2 '<~~ index of the first variant


For i = 2 To LastRow '<~~ Set 2 if row 1 has headers
    i = i + 1
    ws.Rows(i & ":" & i + NumberOfVariants).Insert shift:=xlDown
    i = i - 1
    For j = 0 To (NumberOfVariants * 2) - 1
        ws.Range(Cells(ColumnIndex + (j * 2), i), Cells(ColumnIndex + (j * 2) + 1, i)).Select
        Selection.Cut
        ws.Range(Cells(ColumnIndex + (j * 2), i + j + 1), Cells(ColumnIndex + (j * 2) + 1, i + j + 1)).Paste
    Next j
Next i

End Sub
子导入_格式()
将ws设置为工作表
昏暗的最后一排一样长,我一样长
变暗数变长
Dim j尽可能长
暗列索引与长列索引相同
设置ws=Sheets(“Sheet1”)~~>包含列表的工作表的名称
LastRow=ws.Range(“A”&Rows.Count).End(xlUp).Row
NumberOfVariants=2
ColumnIndex=2'试试这个:)

选项显式
子导入_格式()
将ws设置为工作表
Dim i、j、LastRow、ColumnIndex、NumberOfVariants作为整数
设置ws=Sheets(“Sheet1”)~~>包含列表的工作表的名称
LastRow=ws.Range(“A”&Rows.Count).End(xlUp).Row
NumberOfVariants=4

ColumnIndex=2'您能详细说明一下为什么它不起作用吗?当前结果是什么?转换后的工作表中的
valueB1
在哪里?无需使用
Cut
Paste
。例如,将直接赋值与
Range(“A1”).Resize(10,3)。Value=Range(“K1”)。Resize(10,3)。Value
Option Explicit
Sub import_format()

Dim ws As Worksheet
Dim i, j, LastRow, ColumnIndex, NumberOfVariants As Integer


Set ws = Sheets("Sheet1") '~~> Name of the sheet which has the list
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
NumberOfVariants = 4
ColumnIndex = 2 '<~~ index of the first variant


For i = 2 To LastRow + (LastRow - 2) * (NumberOfVariants - 1) Step NumberOfVariants '<~~ Set 2 if row 1 has headers; Actual last row = current last row + lines which will be inserted
    ws.Rows(i + 1 & ":" & i + NumberOfVariants - 1).Insert shift:=xlDown 'Insert rows based on number of variants
    For j = 2 To (NumberOfVariants - 1) * 2 Step 2 'Walking through the variants, starting in column 2, variant + value are 2 columns, therefore using step 2
        ws.Range(Cells(i, ColumnIndex + j), Cells(i, ColumnIndex + j + 1)).Cut _
        Destination:=ws.Range(Cells(i + j / 2, ColumnIndex), Cells(i + j / 2, ColumnIndex + 1))
    Next j
Next i

End Sub