VBA Excel;查找重复单元格并填充偏移单元格
VBA Excel; Finding Duplicate Cells and Populating an Offset Cell
我正在创建一个自动化系统,将无线传输的数据读取到 excel。数据发送一个仪表号和一个值 ex。 89,-.002(#89 号规,值 =-.002)。此数据进入单元格 F2。我已经创建了将仪表编号和值分隔为变量的代码。我在单元格 C5-C11 中有一个固定仪表编号列表,读数根据仪表编号在单元格 G5-G11 中按偏移量填充。我一直无法弄清楚的是,如果我们需要检查零件上的多个点,如何使用相同的仪表读取多个读数。因此,例如,如果仪表 87 需要三个不同的读数(87 将在 C5-C11 中列出三次),我需要代码根据 F2 中输入的仪表编号变量搜索 87 并找到所有87 在单元格 C5-C11 中。然后检查单元格偏移量 4 列是否已经有值。如果确实如此,则继续找到找到的下一个 87,并检查该偏移量是否有值。如果没有,则填充单元格。
我不能硬编码数字,因为我正在创建模板。因此,它必须搜索我认为所有规格编号的倍数。
感谢任何帮助或想法。如果有人可以给我示例代码以集成到我的项目中,那就更好了。
设置 wsInput = Sheet1
使用 wsInput.Range("C5:C24") ' 选择 sheet 1 个范围
Dim C As Range
Dim GageNum As String
Dim reading As String
Dim MyString As String
Dim Stringlen As Single
Dim location As Single
Dim GLOC As Single
Dim Count As Single
Dim Answer As Integer
Dim Count1 As Single
Dim i As Single
MyString = Range("F2").value 'identifies that cell data in F2 as a string variable
location = InStr(MyString, ",") + 1 'finds the comma separator and adds one to not include comma
GLOC = InStr(MyString, ",") - 1 'finds the location of the comma and goes back one to get rid of the comma.
'MsgBox location 'tests to see what character it is starting at
Stringlen = Len(MyString) - location + 1 'creates a new string that has the length of remaining characters
reading = Mid(MyString, location, Stringlen) 'reads string after the comma until the end of the string
'MsgBox Reading 'test to see if it gets the correct reading from gauage
GageNum = Left(MyString, GLOC) 'finds the gauge number by searching left of the comma
'MsgBox GageNum 'test of the guage number
Set C = .Find(GageNum, LookIn:=xlValues)
If Not C Is Nothing Then
Dim FirstAddress As String
Dim Rslt As String
FirstAddress = C.Address
Do
Rslt = Rslt & C.Address & ","
Set C = .FindNext(C)
Loop While C.Address <> FirstAddress
'MsgBox Left(Rslt, Len(Rslt) - 1)
End If
End With
If Len(C.Offset(, 4)) = 0 Then
Range("F2").Select
C.Offset(, 4) = reading 'populates the cell with the reading.
Else
Answer = MsgBox("Error: Gauge " & C & " measurement has already been taken." & vbNewLine & "Do you want to use this new measurement instead?", vbYesNo + vbQuestion, "Re-measure?")
If Answer = vbYes Then
Range("F2").Select
C.Offset(, 4) = reading 'populates the cell with the reading.
Else
'do nothing
End If
End If
这就是我现在所拥有的,但它不搜索重复项,我只得到它找到的第一个。
您的 DO...LOOP WHILE
部分有问题。通过像那样将 While
条件放在末尾,您告诉它继续循环直到 C.Address = FirstAddress
。在这里不要自动将 C 设置为下一个:Set C = .FindNext(C)
,您应该首先测试它以确定 FindNext
是否为您提供了您想要的地址。像这样:
Set TempC = .FindNext(C)
If TempC.Address <> FirstAddress Then
Set C = TempC
Else
Exit Do
End If
注意:这将始终将 C
设置为仪表编号出现的最后一个位置,而不是下一个。要获得下一个,您可以将 FindNext
替换为 FindPrevious
。然后它将以相反的顺序循环并在仪表号的第二个实例处结束。
我正在创建一个自动化系统,将无线传输的数据读取到 excel。数据发送一个仪表号和一个值 ex。 89,-.002(#89 号规,值 =-.002)。此数据进入单元格 F2。我已经创建了将仪表编号和值分隔为变量的代码。我在单元格 C5-C11 中有一个固定仪表编号列表,读数根据仪表编号在单元格 G5-G11 中按偏移量填充。我一直无法弄清楚的是,如果我们需要检查零件上的多个点,如何使用相同的仪表读取多个读数。因此,例如,如果仪表 87 需要三个不同的读数(87 将在 C5-C11 中列出三次),我需要代码根据 F2 中输入的仪表编号变量搜索 87 并找到所有87 在单元格 C5-C11 中。然后检查单元格偏移量 4 列是否已经有值。如果确实如此,则继续找到找到的下一个 87,并检查该偏移量是否有值。如果没有,则填充单元格。
我不能硬编码数字,因为我正在创建模板。因此,它必须搜索我认为所有规格编号的倍数。
感谢任何帮助或想法。如果有人可以给我示例代码以集成到我的项目中,那就更好了。
设置 wsInput = Sheet1 使用 wsInput.Range("C5:C24") ' 选择 sheet 1 个范围
Dim C As Range
Dim GageNum As String
Dim reading As String
Dim MyString As String
Dim Stringlen As Single
Dim location As Single
Dim GLOC As Single
Dim Count As Single
Dim Answer As Integer
Dim Count1 As Single
Dim i As Single
MyString = Range("F2").value 'identifies that cell data in F2 as a string variable
location = InStr(MyString, ",") + 1 'finds the comma separator and adds one to not include comma
GLOC = InStr(MyString, ",") - 1 'finds the location of the comma and goes back one to get rid of the comma.
'MsgBox location 'tests to see what character it is starting at
Stringlen = Len(MyString) - location + 1 'creates a new string that has the length of remaining characters
reading = Mid(MyString, location, Stringlen) 'reads string after the comma until the end of the string
'MsgBox Reading 'test to see if it gets the correct reading from gauage
GageNum = Left(MyString, GLOC) 'finds the gauge number by searching left of the comma
'MsgBox GageNum 'test of the guage number
Set C = .Find(GageNum, LookIn:=xlValues)
If Not C Is Nothing Then
Dim FirstAddress As String
Dim Rslt As String
FirstAddress = C.Address
Do
Rslt = Rslt & C.Address & ","
Set C = .FindNext(C)
Loop While C.Address <> FirstAddress
'MsgBox Left(Rslt, Len(Rslt) - 1)
End If
End With
If Len(C.Offset(, 4)) = 0 Then
Range("F2").Select
C.Offset(, 4) = reading 'populates the cell with the reading.
Else
Answer = MsgBox("Error: Gauge " & C & " measurement has already been taken." & vbNewLine & "Do you want to use this new measurement instead?", vbYesNo + vbQuestion, "Re-measure?")
If Answer = vbYes Then
Range("F2").Select
C.Offset(, 4) = reading 'populates the cell with the reading.
Else
'do nothing
End If
End If
这就是我现在所拥有的,但它不搜索重复项,我只得到它找到的第一个。
您的 DO...LOOP WHILE
部分有问题。通过像那样将 While
条件放在末尾,您告诉它继续循环直到 C.Address = FirstAddress
。在这里不要自动将 C 设置为下一个:Set C = .FindNext(C)
,您应该首先测试它以确定 FindNext
是否为您提供了您想要的地址。像这样:
Set TempC = .FindNext(C)
If TempC.Address <> FirstAddress Then
Set C = TempC
Else
Exit Do
End If
注意:这将始终将 C
设置为仪表编号出现的最后一个位置,而不是下一个。要获得下一个,您可以将 FindNext
替换为 FindPrevious
。然后它将以相反的顺序循环并在仪表号的第二个实例处结束。