【问题标题】:VBA Check Row For Value Then Another Value and Paste If TrueVBA 检查行的值,然后是另一个值,如果为真则粘贴
【发布时间】:2016-12-09 23:41:46
【问题描述】:

我有一个带有简短描述和详细描述的零件编号列表。该列表的格式设置为在第一列中有一个部件号。在第二列中有一个描述代码。第三列有说明。描述可以是名称、简短描述或基于前面描述代码的详细描述。

查看截图示例: Original Dataset

如您所见,有些部分包含所有三个描述,有些则没有。

我正在尝试从表 1 中获取数据并将信息粘贴到表 2 中,该表具有合并且正确的行结构,即。零件号,名称,短,长。

查看截图示例:New Dataset

这是我一直在使用的一些代码。我觉得我已经接近了,或者至少在正确的轨道上,但它肯定不起作用,目前正在抛出一个没有错误的下一个。

Dim i As Long

For i = 1 To Rows.Count
With ActiveWorkbook
.Sheets("Parts List").Range("A1").Select
    If .Sheets("Parts List").Cells(i, 1).Value = .Sheets("Product Description").Cells(i, 1) Then
    If .Sheets("Product Description").Cells(i, 2).Value = "DES" Then
    .Sheets("Parts List").Cells(i, 2).Value = .Sheets("Product Description").Cells(i, 2).Value
    ElseIf .Sheets("Product Description").Cells(i, 2).Value = "EXT" Then
    .Sheets("Parts List").Cells(i, 5).Value = .Sheets("Product Description").Cells(i, 2).Value
    ElseIf .Sheets("Product Description").Cells(i, 2).Value = "MKT" Then
    .Sheets("Parts List").Cells(i, 3).Value = .Sheets("Product Description").Cells(i, 2).Value
End If
Next i
End With

我们将不胜感激任何和所有的帮助。我真的只是想让它遍历这张纸并提取东西并将它们放在另一张纸上。听起来很容易。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我会对您的代码进行一些小的更改,主要是为了在目标表上保留您正在写入的行的计数器,以及相当多的外观更改:

    Dim srcRow As Long
    Dim dstRow As Long
    
    Dim srcWs As Worksheet
    Dim dstWs As Worksheet
    
    Set srcWs = ActiveWorkbook.Worksheets("Product Description")
    Set dstWs = ActiveWorkbook.Worksheets("Parts List")
    dstRow = 9  'Initially point to the header row
    
    'Only do your For loop for cells that contain a product code, rather than
    'for the 1 million rows in the worksheet
    For srcRow = 1 To srcWs.Range("A" & srcWs.Rows.Count).End(xlUp).Row
        '.Sheets("Parts List").Range("A1").Select  'Not needed
        If dstWs.Cells(dstRow, "A").Value <> srcWs.Cells(srcRow, "A") Then
            'Increment destination row
            dstRow = dstRow + 1
            'Store part number
            dstWs.Cells(dstRow, "A").Value = srcWs.Cells(srcRow, "A").Value
        End If
        'Store other data
        Select Case srcWs.Cells(srcRow, "B").Value
            Case "DES"
                dstWs.Cells(dstRow, "B").Value = srcWs.Cells(srcRow, "C").Value
            Case "EXT"
                dstWs.Cells(dstRow, "E").Value = srcWs.Cells(srcRow, "C").Value
            Case "MKT"
                dstWs.Cells(dstRow, "C").Value = srcWs.Cells(srcRow, "C").Value
        End Select
    Next
    

    您得到的“Next without a For”错误是由于不匹配的If 语句(您的第一个If 语句没有对应的End If)和您的With 块在您的For 内开始循环但在循环结束后完成。总是/始终如一地缩进代码很容易发现这种类型的错误。以下是原始代码缩进后的样子:

    Dim i As Long
    
    For i = 1 To Rows.Count
        With ActiveWorkbook
            .Sheets("Parts List").Range("A1").Select
            If .Sheets("Parts List").Cells(i, 1).Value = .Sheets("Product Description").Cells(i, 1) Then
                If .Sheets("Product Description").Cells(i, 2).Value = "DES" Then
                    .Sheets("Parts List").Cells(i, 2).Value = .Sheets("Product Description").Cells(i, 2).Value
                ElseIf .Sheets("Product Description").Cells(i, 2).Value = "EXT" Then
                    .Sheets("Parts List").Cells(i, 5).Value = .Sheets("Product Description").Cells(i, 2).Value
                ElseIf .Sheets("Product Description").Cells(i, 2).Value = "MKT" Then
                    .Sheets("Parts List").Cells(i, 3).Value = .Sheets("Product Description").Cells(i, 2).Value
                End If
            'Notice that the Next i is not lined up with the For i
            Next i
        End With
    'Notice that we haven't ended up back at the left - so we must be missing
    'the end of some sort of "block"
    

    【讨论】:

    • 嗯......这越来越接近(并且比我的原版更整洁)。这将获取零件编号并将其正确移动,但其他列仅显示描述代码。第 1 部分,DES, MKT, " ", EXT // 第 2 部分, DES, MKT, " ", EXT // 第 3 部分, DES, MKT, " ", EXT // 等等。
    • @Frethy - 抱歉,我没有注意到你写的是 B 列而不是 C 列(我只是复制/粘贴了你的大部分代码) - 将答案编辑为指向 C 列。 (编辑只是在复制数据时将“2”更改为“3”。)
    • @Frethy - 我又做了一次编辑 - 将列引用从数字更改为字母。它会稍微减慢执行速度,但会更清楚地显示哪些列被引用。
    • 是的。太棒了。 @YowE3K 这是完美的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2022-01-17
    • 2017-10-12
    相关资源
    最近更新 更多