简体   繁体   English

宏/ VBA合并非唯一行

[英]Macro/VBA to merge rows for non-unique

I have a spreadsheet with a data connection to a tab delineated text file. 我有一个电子表格,该表格具有与标签所描述的文本文件的数据连接。 The text file is generated by using the export feature from a program called NetScan by SoftPerfect. 文本文件是使用SoftPerfect从名为NetScan的程序中使用导出功能生成的。 Each report is real-time and does not include data from computers that are not currently turned on or connected to the network. 每个报告都是实时的,不包括当前未打开或未连接到网络的计算机中的数据。 This means that a newer report might be missing a computer that an older report had but it might also contain updated information for a computer that had more memory added or something else like that. 这意味着较新的报告可能会缺少较旧的报告所拥有的计算机,但它可能还包含有关添加了更多内存或类似内容的计算机的更新信息。

So, on the spreadsheet I can refresh the data connection to update from the text file but since the text file is being appended to rather than replaced each time I wind up with a LOT of duplicate computer entries (the entire row may or may not be completely duplicate). 因此,在电子表格上,我可以刷新数据连接以从文本文件进行更新,但是由于每次我用很多重复的计算机条目结束时,文本文件都会被附加到而不是被替换(整个行可能会也可能不会完全重复)。 I'd like to merge the rows so that I have one row for each computer. 我想合并这些行,以便每台计算机都有一行。

Small piece of sample data (all fake) - note that there are many more columns in the full spreadsheet but this sample data contains all the info that I plan to filter/merge by: 一小部分示例数据(全部为假数据)-请注意,完整电子表格中还有更多列,但是此示例数据包含我计划过滤/合并的所有信息:

A               B           C                   D               E       F
IP Address      Host Name   MAC Address         Response Time   Dups    Serial Number
192.168.1.110   CLT-001     XX:XX:XX:XX:XX:C5   0 ms            2       ABC123
192.168.1.174   CLT-001     XX:XX:XX:XX:XX:FE   5 ms            1       ABC123
192.168.1.110   CLT-001     XX:XX:XX:XX:XX:C5   1 ms            2       ABC123
192.168.1.138   CLT-004     XX:XX:XX:XX:XX:04   1 ms            2       DEF456
192.168.1.146   CLT-009     XX:XX:XX:XX:XX:8E   1 ms            2       GHI789
192.168.1.121   CLT-004     XX:XX:XX:XX:XX:04   1 ms            2       DEF456
192.168.1.146   NA          XX:XX:XX:XX:XX:8E   1 ms            2       NA
192.168.1.152   CLT-005     XX:XX:XX:XX:XX:6B   1 ms            1       JKL012

The Serial Number is how to identify a unique computer but sometimes the scan cannot obtain the S/N (WMI error) in which case the next best bet is MAC Address. 序列号是识别唯一计算机的方法,但是有时扫描无法获得S / N(WMI错误),在这种情况下,下一个最佳选择是MAC地址。 The problem with MAC Address is that most of these laptops will eventually wind up registering 2 per computer - one for wired and one for wireless. MAC地址的问题在于,大多数便携式计算机最终将在每台计算机上注册2个-一台用于有线,一台用于无线。

I tried using =COUNTIF(Row,ItemToCount) (that's what column E is in the example data) but I'm not sure how to use the information I get from it. 我尝试使用=COUNTIF(Row,ItemToCount) (这就是示例数据中的E列),但是我不确定如何使用从中获取的信息。 Also, this whole thing needs to be done in VBA so I can include it in my data cleanup button on the main sheet of the workbook. 另外,这一切都需要在VBA中完成,因此我可以将其包含在工作簿主表的数据清除按钮中。 My goal is to end up with data that looks like this: 我的目标是最终获得如下所示的数据:

A               B           C                   D               E       F
IP Address      Host Name   MAC Address         Response Time   Dups    Serial Number
192.168.1.110   CLT-001     XX:XX:XX:XX:XX:C5   0 ms            1       ABC123
192.168.1.146   CLT-009     XX:XX:XX:XX:XX:8E   1 ms            1       GHI789
192.168.1.121   CLT-004     XX:XX:XX:XX:XX:04   1 ms            1       DEF456
192.168.1.152   CLT-005     XX:XX:XX:XX:XX:6B   1 ms            1       JKL012

One concept I considered was using the Response Time to indicate whether the connection was wired or wireless which should be accurate enough for my purposes. 我考虑过的一个概念是使用响应时间来指示连接是有线还是无线,这对于我的目的应该足够准确。 0 or 1ms indicates wired while 2ms or more indicates wireless. 0或1ms表示有线,而2ms或更多表示无线。 The other option is to add more WMI calls to identify the type of network card that way and filter them out before the report even gets to the exported text file. 另一个选择是添加更多WMI调用,以这种方式识别网卡的类型,并在报告甚至到达导出的文本文件之前将其过滤掉。

At this point, any help would be appreciated. 在这一点上,任何帮助将不胜感激。 The closest thing I could find on these forums was the Conditional Removal of Duplicates from Excel question. 在这些论坛上我能找到的最接近的东西是有条件地从Excel问题中删除重复项 Other forums such as mrexcel, excelforum, and ozgrid didn't have any obvious answers either although there are so many posts with nearly similar questions that finding what I'm looking for is rather difficult. 其他论坛(例如mrexcel,excelforum和ozgrid)也没有任何明显的答案,尽管有如此之多的帖子都包含几乎相似的问题,以至于很难找到我想要的东西。 Thanks! 谢谢!

You could try this: I find formulas work so much easier/faster than vba .. so I try to use vba as little as possible. 您可以尝试以下方法:我发现公式比vba更容易/更快捷地工作..因此,我尝试尽可能少地使用vba。

First, create a new column, with the following formula: 首先,使用以下公式创建一个新列:

=IF(ISNA(MATCH(F2,OFFSET(F2,-1*(ROW(G2)-1),0,ROW(G2)-1,1),0)), FALSE, TRUE)

The intent of that formula is to return TRUE if the current record is a duplicate of anything above it. 该公式的目的是,如果当前记录与上面的任何记录重复,则返回TRUE。 For now, it just checks the Serial Number, however, in the FALSE section, you could dup the logic a little and change the columns to check the MAC Address in a similar fashion if you wanted. 现在,它只是检查序列号,但是,如果需要,您可以在FALSE部分中略微复制逻辑并更改列以类似的方式检查MAC地址。

I added that formula in column G. In cell H1, I added this formula: 我在G列中添加了该公式。在单元格H1中,我添加了此公式:

=COUNTA(G:G)

(I also named this cell "numrows" ) (我也将此单元格命名为“ numrows”)

Then in vba, I did the following: 然后在vba中,我执行了以下操作:

  Sub Macro1()
    Dim r As Integer

    r = Range("numrows").Value

    For i = r To 2 Step -1
      If Cells(i, 7).Value Then
          Rows(i).Select
          Selection.Delete Shift:=xlUp
          Cells(i, 1).Select
      End If
    Next i
  End Sub

and that seems to do the trick. 这似乎可以解决问题。 by keeping the formula outside of vba, it should make it a bit easier to tweak it to help id the records. 通过将公式保留在vba之外,可以对其进行调整以使记录更容易些。 Then the vba script simply looks for that "trigger" column, and deletes based on this. 然后,vba脚本仅查找该“触发”列,并根据此列将其删除。 This actually allows you to "override" manually if you wanted, to delete a record or two that you know you want gone. 实际上,如果需要,这实际上允许您手动“覆盖”,以删除您知道要删除的一两个记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM