简体   繁体   English

根据A列中的值(1、2或3)复制粘贴数据

[英]Copy paste data based on value (1, 2, or 3) in column A

I have Sheet1 which contains multiple columns of data. 我有Sheet1,其中包含多列数据。 My macro is supposed to grab specific columns from Sheet1, open up Sheet2, and then paste the data into Sheet2 using other logic. 我的宏应该从Sheet1中获取特定的列,打开Sheet2,然后使用其他逻辑将数据粘贴到Sheet2中。

In column A, I have item numbers that range from 1-4, which have the corresponding Item Name in Column B. The top level item will always be 1 (whole product), but depending on the what creates the final product, it can have multiple item number 2, 3, and 4 which are nested underneath each other. 在A列中,我的项目编号范围是1-4,在B列中具有相应的项目名称。顶层项目始终为1(整个产品),但是根据最终产品的创建方式,它可以有多个编号为2、3和4的项目,它们相互嵌套。 For visual purposes: 出于视觉目的:

1 Phone 2 Battery 3 Lithium 3 LithiumX 2 Camera 3 Glass 4 Bulb 4 Lens

Of course in excel, all the numbers will be aligned in column A and the names will aligned in column B. I'm trying to create a VBA code/logic that when the item number is 1, we copy that item name into column B of the new sheet. 当然在excel中,所有数字都将在A列中对齐,名称将在B列中对齐。我正在尝试创建一个VBA代码/逻辑,当项目编号为1时,我们将该项目名称复制到B列中新工作表。 If item number is 2, we copy that item name into column C of the new sheet, so on and so forth for 3 and 4. The only tricky part is that if the Item name is different for item numbers 2, 3 or 4, the logic needs to catch that copy/paste the correct names. 如果项目编号为2,则将该项目名称复制到新工作表的C列中,依此类推,以此类推,直到3和4。唯一棘手的部分是,如果项目编号2、3或4的项目名称不同。逻辑需要捕获该副本/粘贴正确的名称。 So my end data would look something like this. 所以我的最终数据看起来像这样。

|A| B | C | D | E | |1|Phone| | | |2|Phone| Battery | | |4|Phone| Battery | Lithium | |5|Phone| Battery | LithiumX| |6|Phone| Battery | LithiumX| |7|Phone| Camera | | |8|Phone| Camera | Glass | |9|Phone| Camera | Glass | Bulb |

My Current VBA Code is below, which doesn't do much but copy and paste the data from sheet1 to sheet2: 下面是我当前的VBA代码,它没有做很多事情,但是将数据从sheet1复制并粘贴到sheet2:

` `

cls = Array("A1", "B1")

Set sh1 = Sheets("Sheet1")
'Set sh2 = ThisWorkbook.Sheets(2)
Set sh2 = Worksheets.Add(Type:=xlWorksheet, After:=Application.ActiveSheet)
On Error Resume Next
sh2.Name = "Test"
On Error GoTo 0

'Clear sheet 2
sh2.Cells.Clear

'cut specific headers from Sheet 1 and paste to sheet 2
With sh2
    LR = WorksheetFunction.Max(1, .Range("A" & Rows.Count).End(xlUp).Row)
    For n = LBound(cls) To UBound(cls)
        Me.Range(cls(n)).Copy Destination:=.Cells(LR, n + 1)
    Next n
End With

Let lrow1 = sh1.Range("A65356").End(xlUp).Row

For i = 2 To lrow1
    Let lrow3 = sh2.Range("A65356").End(xlUp).Row
        sh2.Cells(lrow3 + 1, 1) = sh1.Cells(i, 1)
        sh2.Cells(lrow3 + 1, 2) = sh1.Cells(i, 2)

Next i

` Any help would be appreciated! `任何帮助将不胜感激!

Ok, so what I've done here is used a loop to check through every entry in the A column and used some if/elseif statements to determine what values need to be updated. 好的,所以我在这里所做的工作是使用循环检查A列中的每个条目,并使用一些if/elseif语句来确定需要更新哪些值。 So long as the data is always as you have formatted above I believe this should work. 只要数据始终如您所格式化,我相信这应该可以工作。

It updates the appropriate slot of data as it progresses down the list of levels, but upon hitting a number, it'll set every level that comes after it to be NULL . 它随着在级别列表中向下移动而更新适当的数据插槽,但是一旦击中一个数字,它将把其后的每个级别设置为NULL I used NULL because it prints the same as a "" in excel, but it uses less memory. 我使用NULL是因为它在excel中的输出与“”相同,但它使用的内存更少。

I've tried to speed it up a bit by doing most of the comparison in memory by taking all the data at once and using arrays. 我试图通过一次获取所有数据并使用数组来进行内存中的大多数比较,从而加快了速度。 It should be noted, however, you could also make it so that you only perform the print operation once by using a second variant array that you re-dimension as you go, but I've chosen not to here. 应该注意的是,但是,您也可以使用第二个变量数组来执行一次打印操作,该数组在您进行操作时会重新定义维数,但是我在这里选择了不做。 If there are a large number of items, it is worth considering as continuously accessing the sheet will severely slow down your program. 如果有大量项目,则值得考虑,因为连续访问工作表将严重降低程序速度。

Option Explicit

Sub CascadingList()
    Dim Levels(1 To 4) As String
    Dim Subcount As Long
    Dim cell As Variant
    Dim Lastrow As Long
    Dim Data() As Variant

    Lastrow = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    Data = ActiveSheet.Range("A1:B" & Lastrow).Value
    Subcount = 1

    For cell = 1 To UBound(Data, 1)
        If Data(cell, 1) = 1 Then
            Levels(1) = Data(cell, 2)
            Levels(2) = vbNullString
            Levels(3) = vbNullString
            Levels(4) = vbNullString
        ElseIf Data(cell, 1) = 2 Then
            Levels(2) = Data(cell, 2)
            Levels(3) = vbNullString
            Levels(4) = vbNullString
        ElseIf Data(cell, 1) = 3 Then
            Levels(3) = Data(cell, 2)
            Levels(4) = vbNullString
        ElseIf Data(cell, 1) = 4 Then
            Levels(4) = Data(cell, 2)
        End If
        ActiveWorkbook.Worksheets(2).Range("A" & Subcount & ":D" & Subcount).Value = Levels
        Subcount = Subcount + 1
    Next cell
End Sub

To clarify, this will start looking at the levels from A1 downwards and will paste values into A1:D1 of the second sheet downwards. 为了清楚起见,这将开始从A1向下看,并将值向下粘贴到第二张纸的A1:D1中。 Please alter the ranges so that it works with your code. 请更改范围,使其与您的代码一起使用。

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

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