VBA,数据到行而不是列和向下?
我正在写C列,但数据如前所示:C1:X1,而不是按行排列。 (例如:C1:C25)我希望它被垂直换行而不是水平。
这个问题是我想要A和B列在unisen这个换位。 所以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:
Function Col_Letter(lngCol As Long) As String Dim vArr vArr = Split(Cells(1, lngCol).Address(True, False), "$") Col_Letter = vArr(0) End Function
您应该可以将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