循环工作表 - 未写入数组的重复搜索值

时间:2014-05-07 12:08:32

标签: arrays excel-vba vba excel

您好我是VBA的新手,我正在努力实现以下目标:

  1. 将工作簿列的值读入数组
  2. 打开另一个工作簿并在工作表中查找列中的这些值,并复制工作表中相邻列中的数据。 3.将数据写入第1点的工作簿
  3. 这是我到目前为止所写的功能:

    Function getDetailsFromMasterSheet(ByRef numberArray As Variant, ByRef flagNameArray As Variant, nameSuffix As String, flow As String)
    Dim arrayIndex As Integer
    Dim curCell As Long
    Dim pathToFile As String
    Dim pathToXl As String
    Dim orderNumber As Integer
    Dim classType As String
    Dim classNumber As Integer
    Dim className As String
    Dim bNumber As Integer
    Dim bName As String
    Dim cNumber As Integer
    Dim cName As String
    Dim logic As String
    Dim allowClear As String
    Dim testFlagName As String
    Dim flowName As String
    Dim flowNameSuffix As String
    Dim newWorkSheetName As String
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    Dim sheetCount As Integer
    Dim rowStart As Integer
    Dim columnStart As Integer
    Dim cellValue As String
    orderNumber = 1
    rowStart = 4
    columnStart = 1
    Dim retval As Boolean
    pathToFile="C:\Projects\Project_Binning\Standard_Binning_J750HD_rev04.xlsx"
    pathToXl = "C:\Projects\Project_Binning\MockDataBinning.xlsm"
    logic = "S_ANY"
    allowClear = "NO"
    arrayIndex = 0
    
    Set ws3 = Worksheets(newWorkSheetName)
    Set ws = Workbooks.Open(pathToFile).Worksheets("Test")
    ws.Activate
    For curCell = 1 To ws.Cells(ws.Rows.Count, 8).End(xlUp).Row
    If ws.Cells(curCell, 8).Value <> vbNullString Then
      cellValue = ws.Cells(curCell, 8).Value
      If StrComp(cellValue, numberArray(arrayIndex), vbTextCompare) = 0 Then
        classType = ws.Cells(curCell, 8).Offset(0, -7).Value
        classNumber = CInt(ws.Cells(curCell, 8).Offset(0, -6).Value)
        className = ws.Cells(curCell, 8).Offset(0, -5).Value
        If StrComp(flowNameSuffix, "LM", vbTextCompare) = 0 Then
            bNumber = CInt(ws.Cells(curCell, 8).Offset(0, -2).Value)
            bName = ws.Cells(curCell, 8).Offset(0, -1).Value
        Else
           bNumber = CInt(ws.Cells(curCell, 8).Offset(0, -4).Value)
            bName = ws.Cells(curCell, 8).Offset(0, -3).Value
        End If
        cNumber = numberArray(arrayIndex)
        cName = ws.Cells(curCell, 8).Offset(0, 1).Value
        ws3.Cells(rowStart, columnStart) = orderNumber
        ws3.Cells(rowStart, columnStart).Offset(0, 1) = classType
        ws3.Cells(rowStart, columnStart).Offset(0, 2) = classNumber
        ws3.Cells(rowStart, columnStart).Offset(0, 3) = className
        ws3.Cells(rowStart, columnStart).Offset(0, 4) = bNumber
        ws3.Cells(rowStart, columnStart).Offset(0, 5) = bName
        ws3.Cells(rowStart, columnStart).Offset(0, 6) = cNumber
        ws3.Cells(rowStart, columnStart).Offset(0, 7) = cName
        ws3.Cells(rowStart, columnStart).Offset(0, 8) = logic
        ws3.Cells(rowStart, columnStart).Offset(0, 9) = allowClear
        ws3.Cells(rowStart, columnStart).Offset(0, 10) = "'-" & flagNameArray(arrayIndex)
        arrayIndex = arrayIndex + 1
        rowStart = rowStart + 1
        orderNumber = orderNumber + 1
        End If
        End If
        Next curCell
        End Function
    

    我正在复制的工作表中的数据基本上是字符串和整数,格式为

    classType,className,classNumber,bNumber,bName,cNumber,cName

    上面的每个数据都是字符串或数字。这些被复制到变量中,而这些变量又被写入另一个工作表

    当数字数组中没有重复值时,代码按预期工作.i.e。数据将按预期从工作表中读取并写入预期的工作表。但是,当数组中有重复项时,只考虑其中一个值而其余值则不然。例如,如果数组包含{65,75,65,80}。 65的值只写入工作表一次,而重复的值不是。我检查了数组是否正确填充并且它是。不确定我做错了什么。有人可以告诉我吗?

1 个答案:

答案 0 :(得分:0)

我发现了问题并且能够修复它。我注意到在for循环中,控件从上到下发生,它只发生一次。因此,当满足搜索条件一次时,它将再也不会被满足。例如, 如果数字65在数组中存在两次,则只要找到该数字,就会完成对另一个工作表任务的写入。但是,如果数字再次出现,会发生什么,因为一旦再次找不到该数字,迭代已经通过工作表上的数字65。所以,我添加了以下代码行

curCell = 1

之后的第一个if循环结束时
orderNumber = orderNumber+1

这将控件再次设置为工作表的第一行,并将重复的数字成功写入工作表!