匹配记录时出错

时间:2017-07-27 09:43:29

标签: excel vba excel-vba

问题:

代码返回dataType: 'json'次匹配。

代码:

0

屏幕截图:

ALL BRANDS Sheet1

我错过了什么吗?有什么想法吗?

修改

找到问题。 Sub searchNames() Dim loc As String Call location(loc) Dim loadWb As Workbook Dim loadWs As Worksheet ' ~~ Load file location Set loadWb = Workbooks.Open(loc) Set loadWs = loadWb.Sheets("Sheet1") ' ~~ Init rows in loaded excel Dim lrow As Long With loadWs ' ~~ Set range for lookup value lrow = .Range("G" & .rows.Count).End(xlUp).Row End With ' ~~ Loop to remove trailing spaces Dim TrimCounter As String Dim NewString As String For ind = 2 To lrow ' ~~ Set rows for trim TrimCounter = loadWs.Range("G" & ind).Value NewString = Trim(TrimCounter) ' ~ Write trimmed values loadWs.Range("G" & ind).Value = NewString Next ind ' ~~ Set output worksheet Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("ALL BRANDS") Dim lrowWs As Long With ws lrowWs = .Range("D" & .rows.Count).End(xlUp).Row End With Dim counter As Long Dim rows As Long Dim nameCounter As String counter = 0 ' ~~ Get controlPointNumber in ALL BRANDS For ind = 2 To lrowWs ' ~~ Set controlPointNumber nameCounter = ws.Range("D" & ind).Value ' ~~ Start with row 2 in loaded Excel to omit header For ind2 = 2 To lrow ' ~~ Check if the name matches in ALL BRANDS If loadWs.Range("G" & ind2).Value = nameCounter Then counter = counter + 1 End If Next ind2 ' ~~ Write the value in Worksheet 'ALL BRANDS' equal to the results ws.Range("L" & ind).Value = counter ' ~~ Init counter to 0 and check other controlPointNumber counter = 0 rows = rows + 1 Next ind ' ~~ Close workbook ~ Byeee loadWb.Close False MsgBox "Scan finished! Scanned " & rows & " rows" End Sub

中的值中有空格

2 个答案:

答案 0 :(得分:3)

更改代码的一部分,如下所示:

    socket.on("username", function(username){
            res.redirect(__dirname + "/index.html");
    })

然后,当您到达For ind = 2 To lrowWs Debug.Print lrowWs nameCounter = ws.Range("D" & ind).value Debug.Print nameCounter For ind2 = 2 To lrow If loadWs.Range("G" & ind2).value = nameCounter Then Debug.Print loadWs.Range("G" & ind2).value counter = counter + 1 End If Next ind2 ws.Range("L" & ind).value = counter Stop counter = 0 rows = rows + 1 Next ind 时,您应该在即时窗口中有3个不同的值。仔细看看它们,分析它们并相应修复整个代码。

修改 可能错误来自于这个想法,你可以使用这样的东西:

stop

因此,当你说Dim rows As Long时,VBA不知道你的意思。长话短说,将rows.Count更改为Dim rows as Long并相应地修改到处。

答案 1 :(得分:1)

我一直使用.find方法。对我而言,它更容易,如果你将它与字典结合起来,你可以做到整个范围,并且可以确保不会丢失任何价值。代码将采用A列值的范围,并计算值在范围内出现的频率。希望代码可以帮助你。

    Sub Makro1()
'Excel objects.
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim rngLockin As Range
    Dim rngFind As Range

    Dim idx As Integer
    Dim idxRow As Integer
    idxRow = 2
    Dim strAddress As String

    'Initialize the Excel objects.
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Tabelle1")


    Set dicSearch = CreateObject("Scripting.Dictionary")
    LastRow = ws.UsedRange.Rows.Count
    Set rngLockin = ws.Range("A2:A22").SpecialCells(xlCellTypeConstants)

    For Each rngcell In rngLockin



        'I Value is not in dic, insert it and start counting

        If Not dicSearch.Exists(rngcell.Value) Then
            dicSearch.Add rngcell.Value, ""

            'Search the four columns for any constants.


            'Retrieve all columns that contain X. If there is at least one, begin the DO/WHILE loop.
            idx = 0
            With rngLockin
                Set rngFind = .Find(What:=rngcell.Value, LookIn:=xlValues)
                If Not rngFind Is Nothing Then
                    strAddress = rngFind.Address
                    idx = idx + 1
                    rngFind.Select
                    'Unhide the column, and then find the next X.
                    Do
                        rngFind.EntireColumn.Hidden = False
                        Set rngFind = .FindNext(rngFind)
                        rngFind.Select
                        If Not rngFind Is Nothing And rngFind.Address <> strAddress Then idx = idx + 1

                    Loop While Not rngFind Is Nothing And rngFind.Address <> strAddress
                End If
            End With
            Cells(idxRow, 3) = rngcell.Value
            Cells(idxRow, 4).Value = idx
            idxRow = idxRow + 1

        End If
    Next
End Sub

enter image description here

可以随意询问您是否有疑问。