简体   繁体   中英

VBA, Data into Rows instead of Columns and Filldown?

I am writing to column C, but the data goes ex: C1:X1, instead I would like it by row. (ex: C1:C25) I would like it to be transposed vertically down the rows instead of horizontally.

The problem with this is I want the columns A&B to be filled down with this transposition in unisen. So A1:A25 would have the file name and C1:c25 would have my headers.

As I start to add more columns I would like to be able to add more columns and fill down to these aswell. (only filldown columns A and B)

any Ideas?

Code:

  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:

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

You should just be able to change the lOutputCol from an incremented value to a static columnC. And you will have to add a lRow count increment inside the loop.

Change

'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

to

'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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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