匹配logging时出错

问题:

代码返回0匹配项。

代码:

 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 

屏幕截图:

所有品牌 工作表Sheet1

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

编辑:

问题定位。 COLUMN G中的值有空格

像这样改变代码的一部分:

 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 

然后,当你到达stop ,你应该在立即窗口中有3个不同的值。 仔细看看他们,分析他们并相应修复整个代码。


编辑:可能是错误来自这个想法,你可以使用这样的东西:

Dim rows As Long

因此,当你说rows.Count时,VBA不知道你的意思。 Dim lngRows as longDim rows as Long改为Dim lngRows as long并相应地修改即可。

我一直使用.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 

在这里输入图像说明

随意问问你有没有问题。