VBA,数据分为行而不是列和填充?

时间:2016-02-29 16:46:04

标签: excel vba

我正在写C列,但数据是ex:C1:X1,而不是我想按行排序。 (例如:C1:C25)我希望它可以垂直向下移动而不是水平移动。

这个问题是我希望A& B列能够以unifen方式填充此转置。所以A1:A25会有文件名而C1:c25会有我的标题。

当我开始添加更多列时,我希望能够添加更多列并填写这些列。 (仅填写A和B列)

任何想法?

代码:

  Dim iIndex As Integer
    Dim lCol As Long
    Dim lOutputCol As Long


'Loop through the worksheets in the current workbook.
For iIndex = 1 To wb.Worksheets.Count

    'Set the current worksheet
    Set ws = Application.Worksheets(iIndex)

    'List out the workbook and worksheet names
    wsReport.Range("A" & lRow).Value = wb.Name
    wsReport.Range("B" & lRow).Value = ws.Name

    'Start a counter of the columns that we are writing to
    lOutputCol = 3

    'Loop through the columns.
    For lCol = 1 To ws.UsedRange.Columns.Count
        'Write the header
        wsReport.Range(Col_Letter(lOutputCol) & lRow).Value = ws.Range(Col_Letter(lCol) & "1").Value

        'Increment our column counters.
        lOutputCol = lOutputCol + 1
    Next lCol

    'Increment the row we are writing to
    lRow = lRow + 1
Next iIndex

功能:

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function

1 个答案:

答案 0 :(得分:0)

您应该能够将lOutputCol从递增值更改为静态columnC。并且您必须在循环内添加lRow计数增量。

更改

'Loop through the columns.
For lCol = 1 To ws.UsedRange.Columns.Count
    'Write the headers
    wsReport.Range(Col_Letter(lOutputCol) & lRow).Value = ws.Range(Col_Letter(lCol) & "1").Value

    'Increment our column counters.
    lOutputCol = lOutputCol + 1
Next lCol

'Loop through the columns.
For lCol = 1 To ws.UsedRange.Columns.Count

    'List out the workbook and worksheet names
    wsReport.Range("A" & lRow).Value = wb.Name
    wsReport.Range("B" & lRow).Value = ws.Name

    'Write the headers
    wsReport.Range("C" & lRow).Value = ws.Range(Col_Letter(lCol) & "1").Value

    'Increment our column counters.
    lOutputCol = lOutputCol + 1

    'Increment the row we are writing to
    lRow = lRow + 1

Next lCol