简体   繁体   English

如何在距离我所在的单元格X列的单元格中写入(X是在另一个单元格中定义的值)

[英]How can I write in a cell that is X number of column away from the cell I am on (X is a value defined in another cell)

I have a set of items which are classified as A/B/C . 我有一组物品被归类为A/B/C For example: 例如:

Item & category : 项目和类别:

在此输入图像描述

I want to make A/B/C as columns but place each items spanning across. 我想将A / B / C作为列,但是将每个项目放在一起。 For example: 例如:

Each item as columns : 每个项目作为列:

在此输入图像描述

How can I do this when there can be any number of items under each category? 如果每个类别下可以有任意数量的项目,我该怎么办?

I think this is possible with macros, but I am hoping to steer clear from macros as much as possible. 我认为宏可以实现这一点,但我希望尽可能避免从宏中清除。

Modify the code and try: 修改代码并尝试:

Option Explicit

Sub TEST()

    Dim LastRow As Long, LastColumn As Long, LastColumn2 As Long, i As Long, y As Long
    Dim arr As Variant

    With ThisWorkbook.Worksheets("Sheet1")

        arr = Array("A", "B", "C") '<- Create an array with the desirable Items

        For i = LBound(arr) To UBound(arr) '<- Loop array
            'Find Last column of row 12 in order to import Titles
            LastColumn = .Cells(12, .Columns.Count).End(xlToLeft).Column

            If LastColumn = 1 And .Cells(11, LastColumn).Value = "" Then
                .Cells(11, LastColumn).Value = arr(i)
            Else
                .Cells(11, LastColumn + 1).Value = arr(i)
            End If
            'Find Last row of column B in order to create the loop range
            LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
            'Loop column B from 1 to last row of column B
            For y = 1 To LastRow
                'Check if looping value is the same with array value
                If .Range("B" & y).Value = arr(i) Then
                    'Find Last column of row 12 in order to import values
                    LastColumn2 = .Cells(12, .Columns.Count).End(xlToLeft).Column

                    If LastColumn2 = 1 And .Cells(12, LastColumn2).Value = "" Then
                        .Cells(12, 1).Value = .Range("A" & y).Value
                    Else
                        .Cells(12, LastColumn2 + 1).Value = .Range("A" & y).Value
                    End If

                End If

            Next y

        Next i

    End With

End Sub

Results: 结果:

在此输入图像描述

暂无
暂无

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

相关问题 如何在 C 列(如果单元格为空)中放置一个数字 X,其中 X 等于 B 列中的同一行减 1? - How can I place a number X in Column C(if cell is empty) where X is equal to the same row in column B minus 1? 基于另一个单元格的值自动填充x行数的代码 - Code to autofill x number of rows based on a value from another cell 如何编写公式,其中单元格引用的数字部分来自另一个单元格? - how do I write a formula where the number portion of a cell reference comes from another cell? VBA如果Column(“ A”)。Cell(i).Value以“ x”开头,则Column(“ G”)。Cell(i).value =“ String” - VBA if Column(“A”).Cell(i).Value begins with “x” then Column(“G”).Cell(i).value = “String” 如果另一个单元格是值X,则将一个单元格设置为另一个单元格的值 - Setting a cell to a value of another cell IF another cell was value X 如何根据另一个单元格的值更改单元格的公式 - How can I change the formula of the cell according to the value of another cell 如何在另一个单元格中将一个单元格值重复X次? - How to repeat one cell value X times in another cell? 如果单元格值为x,则删除列 - Deleteing column if the cell value is x 在Excel中,如何在单元格X中更改单元格Y的值,而仅在单元格X中键入 - How to , in excel, change the value of cell Y from cell X, while only typing in cell X 如何使一个单元格等于另一个单元格返回的单元格地址的值? - How do I make a cell equal to the value of the cell address returned from another cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM