简体   繁体   English

遍历列和行

[英]Iterate over columns and rows

I have an Excel sheet which consists of multiple columns. 我有一个包含多列的Excel工作表。 In each column there are multiple rows - not always the same number of rows, but never more then from row 8 to row 208. I need all values from each column in a new column as one ordered list, beginning with column 3 (being C) till column 23 (being Z). 在每一列中都有多行-不一定总是具有相同的行数,但从第8行到第208行则永远不会多。我需要将新列中每一列的所有值作为一个有序列表,从第3列开始(即C )直到第23栏(Z)。 I need this new column (being at position AB in the same sheet) with ALL values and without empty rows, as it is the source for a drop down list. 我需要具有所有值且没有空行的新列(位于同一工作表中的AB位置),因为它是下拉列表的来源。

This is, what I have so far but it does not work: 到目前为止,这就是我目前无法使用的内容:

Sub createDDICListe()
   Dim c
   Dim counter
   Dim cnt
   Dim Start
   Dim Ende
   Start = 8
   Ende = 208
   counter = 8
   cnt = Start

   Set Spalten = ["C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"]

   ActiveSheet.Range("AB7") = "DDIC-Liste:"
   For Each c In Spalten
       cnt = Start
       On Error Resume Next
       For cnt = Start To cnt <= Ende
          Dim Cell
          Cell = Cells(cnt, c)
          If IsError(Cell) Or Cell.Value = False Or Cell.Value = "" Then
             GoTo weiter
        Else
            Cells(counter, "AB").Value = Cell.Value
            counter = counter + 1
        End If
        cnt = cnt + 1
    Next
weiter:
    Next
End Sub

How to iterate over the columns and in this "for each loop" iterate over the rows and copy the cell-value in the new column row by row? 如何遍历列,并在此“ for each loop”中遍历行,并逐行复制新列中的单元格值?

Should do the job very quickly: 应该很快完成工作:

Sub MacroMan()

Dim x, y
    x = [C8:Z208]
    Range("AB7").Value = "DDIC-Liste:"
    For Each y In x
        If Not y = vbNullString Then Range("AB" & Rows.count).End(xlUp).Offset(1, 0).value = y
    Next
End Sub

This should do the job: 这应该做的工作:

Dim pasteCell As Range, origin As Range
Set pasteCell = [AB1] 'Destination
Dim firstRow as Long
firstRow = 1
For columnIndex = 3 To 26 Step 1 'Columns from C to Z
    Set origin = Range(Cells(firstRow , columnIndex), Cells(firstRow , columnIndex).End(xlDown))
    origin.Copy
    pasteCell.Insert xlShiftDown, origin
Next columnIndex
Set origin = Nothing

You can adapt the Destination cell and of course the first copied row of each column. 您可以调整目标单元格,当然也可以调整每列的第一行复制。

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

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