Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 搜索以连接列,并在两者之间从另一个工作簿中选择应增加的额外金额_Excel_Vba_Conditional Statements_Increment - Fatal编程技术网

Excel 搜索以连接列,并在两者之间从另一个工作簿中选择应增加的额外金额

Excel 搜索以连接列,并在两者之间从另一个工作簿中选择应增加的额外金额,excel,vba,conditional-statements,increment,Excel,Vba,Conditional Statements,Increment,我有一个通过VBA修改的文件。 它连接图纸中的三列以创建名称 但是,需要连接其他信息以创建新数据。 需要通过从另一个工作簿中的数据推断某些内容来创建数据 在SCSpecific列中,始终使用相同的名称(但其位置可以更改,但是在工作表中),宏需要查找特定信息。有四种可能 一旦确定了这种可能性,一旦术语与这四个术语中的任何一个匹配,VBA应增加工作簿中术语末尾的数字 第一个工作簿中的结构如下所示: Nip Nup Noupx 对于“Noup”,有四种情况:Noupx、Noupy、Noupu、No

我有一个通过VBA修改的文件。 它连接图纸中的三列以创建名称

但是,需要连接其他信息以创建新数据。 需要通过从另一个工作簿中的数据推断某些内容来创建数据

在SCSpecific列中,始终使用相同的名称(但其位置可以更改,但是在工作表中),宏需要查找特定信息。有四种可能

一旦确定了这种可能性,一旦术语与这四个术语中的任何一个匹配,VBA应增加工作簿中术语末尾的数字

第一个工作簿中的结构如下所示:

  • Nip Nup Noupx
对于“Noup”,有四种情况:Noupx、Noupy、Noupu、Noupa

  • VBA协议:NipNupNoupa
(或者可能是NipNupNoupx,NipNupNoupu…)

然后VBA应该进入另一个工作簿,查找术语“Noupa”、“Noupu”、“Noupx”、“Noupy”

对于每一项,应确定“努帕”(或其他)之后的具体数字,并应通过添加“+1”来增加该数字

因此,结果将是:

  • Noupa002(源于Noupa001的识别)
  • Noupu034(由Noupu033标识产生)
目前,我有以下VBA代码,我不知道如何在另一个工作簿中查找数据并将其递增

Sub TralaNome()

    Const q = """"

    ' get source data table from sheet 1
    With ThisWorkbook.Sheets(1).Cells(1, 1).CurrentRegion

   ' check if data exists
        If .Rows.Count < 2 Or .Columns.Count < 2 Then
            MsgBox "No data table"
            Exit Sub
        End If

   ' retrieve headers name and column numbers dictionary
        Dim headers As Dictionary
        Set headers = New Dictionary
        Dim headCell
        For Each headCell In .Rows(1).Cells
            headers(headCell.Value) = headers.Count + 1
        Next

   ' check mandatory headers

        For Each headCell In Array(("Costumer", "ID", "Zone“,  "Product Quali", "Spec A", "Spec B", "Spec_C", "Spec_D", "Spec_1",  " Spec_2", " Spec_3", " Spec_4", " Spec_5", " Spec_6", " Spec_7", "Chiavetta", "Tipo_di _prodotto",  "Unicorno_Cioccolato", “cacao tree“)
            If Not headers.Exists(headCell) Then
                MsgBox "Header '" & headCell & "' doesn't exists"
                Exit Sub
            End If
        Next
        Dim data

 ' retrieve table data
        data = .Resize(.Rows.Count - 1).Offset(1).Value
    End With

   ' process each row in table data
    Dim result As Dictionary
    Set result = New Dictionary
    Dim i
    For i = 1 To UBound(data, 1)
                    MsgBox "Empty row"
                    Exit For
                    result(result.Count) = _
                        q & "ID " & data(i, headers("ID ")) & _
                        q & " Tipo_di _prodotto " & data(i, headers("Tipo_di _prodotto")) & _
                        q & " cacao tree " & data(i, headers("Nupu")) & _
                        q
        End Select

    Next

    ' output result data to sheet 2
    If result.Count = 0 Then
        MsgBox "No result data for output"
        Exit Sub
    End If
    With ThisWorkbook.Sheets(2)
        .Cells.Delete
        .Cells(1, 1).Resize(result.Count).Value = _
            WorksheetFunction.Transpose(result.Items())
    End With
    MsgBox "Completed"


End Sub
(对不起,这可能应该是一个评论,但我还没有足够的声誉)。 然而,即使没有详细检查您的代码,在某个for循环中,如果没有<代码>,如果在某些条件下避免了,则我会看到<代码>出口。这可能意味着,无论循环中写在这行下面的内容是什么,都永远不会完成——除了第一个实例,循环对任何东西都没有好处。(带注释的循环处理表数据中的每一行)


你试过一步一步地运行这个程序吗?(在打开测试数据集的情况下进入VBEditor,然后点击F8或调试工具栏中的“单步执行”按钮)

我已经尝试过了。我在堆栈溢出中格式化不正确。我的主要和可怕的问题是,我需要在VBA中以一种集体的方式进行多个不同和复杂的操作。这很复杂。正确的代码关闭循环。谢谢你指出这一点。我改正了。然而,在我的实际代码中,除了我的问题中的另一点之外,我得到了一个“编译错误”“未定义用户定义类型”“我设法解决了“未定义用户定义类型”编译错误。但是,现在,在运行宏时,我一直将我的一个列的名称系统地作为一条消息,后跟“不存在”。尽管我甚至将此选项用作要搜索的标题列表,但我仍然收到此消息。使用F8方法,此错误发生在哪一行?
Function GetLastRowWithData(WorksSheetNoupa As Worksheet, Optional NoupaLastCol As Long) As Long
    Dim lCol, lRow, lMaxRow As Long
    If NoupaLastCol = 0 Then
        NoupaLastCol = wsSheet.Columns.Count
    End If
    lMaxRow = 0
    For lCol = NoupaLastCol To 1 Step -1
        lRow = wsSheet.Cells(wsSheet.Rows.Count, lCol).End(xlUp).Row
        If lRow > lMaxRow Then
            lMaxRow = lRow
        End If
    Next
    GetLastRowWithData = lMaxRow
End Function