简体   繁体   English

将Excel行的一部分转置为其他行VBA中的自身副本?

[英]Transpose part of an excel row to a copy of itself in other rows VBA?

I need to transpose yearly expense data currently stored in a single row in excel, to a copied version of itself in multiple rows. 我需要将当前存储在excel中单行中的年度费用数据转换成多行自身的复制版本。 to clarify what i mean, here is an example 为了澄清我的意思,这是一个例子

original data source returns this: 原始数据源返回以下内容:

Name    Title   Year1Expense Year2Expense Year3Expense Year4Expense  other1   other 2   etc
Bob     Tech       30,000       17,000      20,000       18,000

i need the data to look like this 我需要数据看起来像这样

Name    Title   Year    Expense   other1   other2   etc
Bob     Tech     1       30,000
Bob     Tech     2       17,000
Bob     Tech     3       20,000
Bob     Tech     4       18,000

I have thousands of names, and the data pulled is almost always different counts of names, so it needs to be able to dynamically convert this data as well. 我有成千上万个名称,提取的数据几乎总是具有不同的名称计数,因此也需要能够动态转换此数据。 Has anyone used VBA to do something similar in Excel? 有没有人使用VBA在Excel中做类似的事情? thanks for any responses in advance 感谢您的任何提前回复

Just create a button on the original data source and set it to use this macro when clicked. 只需在原始数据源上创建一个按钮,并将其设置为在单击时使用此宏即可。

Sub Transpose_Click()

Dim numberOfRows, numberOfColumns, numberOfYears, counter, nextRow As Integer, originalSheet As String
originalSheet = ActiveSheet.Name
nextRow = 2
numberOfColumns = Range("A1").End(xlToRight).Column
numberOfYears = 0
counter = 5
For i = 1 To numberOfColumns
    If LCase(Left(CStr(Cells(1, i).Value), 4)) = "year" Then
        numberOfYears = numberOfYears + 1
    End If
Next
numberOfRows = Range("A1").End(xlDown).Row

Dim wsSheet As Worksheet
On Error Resume Next
Set wsSheet = Sheets("TransposedData")
On Error GoTo 0
If Not wsSheet Is Nothing Then
MsgBox "Sheet already exists, overwriting"
Else
MsgBox "Creating new sheet"
Sheets.Add.Name = "TransposedData"
End If

With Sheets("TransposedData")
    .Cells.Delete
    .Range("A1").Value = "Name"
    .Range("B1").Value = "Title"
    .Range("C1").Value = "Year"
    .Range("D1").Value = "Expense"
    For i = numberOfYears + 3 To numberOfColumns
        .Cells(1, counter).Value = Sheets(originalSheet).Cells(1, i).Value
        counter = counter + 1
    Next
    For i = 2 To numberOfRows
        For j = 1 To numberOfYears
            .Cells(nextRow, 1).Value = Sheets(originalSheet).Cells(i, 1).Value
            .Cells(nextRow, 2).Value = Sheets(originalSheet).Cells(i, 2).Value
            .Cells(nextRow, 3).Value = j
            .Cells(nextRow, 4).Value = Sheets(originalSheet).Cells(i, 2 + j).Value
            For k = 1 To (numberOfColumns - 2 - numberOfYears)
                .Cells(nextRow, 4 + k).Value = Sheets(originalSheet).Cells(i, 2 + numberOfYears + k).Value
            Next
            nextRow = nextRow + 1
        Next
    Next
End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM