在MS Excel中使用VBA标记单词

时间:2013-12-29 01:53:58

标签: excel vba excel-vba vb6 excel-2007

我无法将其整理出来。我有一个excel工作表。 sheet1的列A包含大约300,000个令牌,而sheet2的列A包含大约15,000个令牌,其中可能的词性标签与每个令牌在同一行上展开。格式示例:

A in sheet1        (A            B    C    D     E) in sheet2
now                may           NN   MD
earth              but           CC   CJS
between            can           MD   NN
been               think         VB   VBP  VBZ  NN
think              now           NN   JJ   RB   IN
may                between       IN   CC   CJS
red                ...
between
now
think
may
now           
...

我的任务是在A(表2)中取每个单词,在A(表1)中搜索它。如果找到,请复制其行上的标签,并将标签粘贴到与A(sheet1)中找到的所有单词相邻的单元格中。也就是说,如果找到想到,之间等等,那么我将在sheet1中输出:

A          B     C    D   E
now               
earth 
between    IN   CC   CJS            
been               
think      VB   VBP  VBZ  NN          
may                
red               
between    IN   CC   CJS
now
think      VB   VBP  VBZ  NN
may
now           
...

我有这个代码只搜索A(sheet1)和A(sheet2)中的非空单元格,检查A(sheet2)中的每个单词是否在A(sheet1)中。如果为true,则将复制单词(而不是关联的标签)并将其粘贴到与A(sheet1)中的结果相邻的C列中的单元格中。结果看起来像下面而不是上面。

A          B      C        D    E
now               
earth 
between          between            
been               
think            think         
may                
red               
between          between
now
think            think
may
now           
...

以下是代码:

Sub CopyDataToPlan()
Dim LData As String
Dim LData2 As String
Dim iVal As Integer
Dim iVal2 As Integer


iVal = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
iVal2 = Worksheets("Sheet2").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count

For LData_counter = 1 To iVal
    LData = Sheets("Sheet1").Cells(LData_counter, 1).Value

    For LData2_counter = 1 To iVal
        LData2 = Sheets("Sheet2").Cells(LData2_counter, 1).Value
        If (LData2 = LData) Then
            Sheets("Sheet1").Cells(LData_counter, 3) = LData2
            'Selection.Copy
        End If

    Next LData2_counter

Next LData_counter
End Sub

另外,这个代码当我尝试使用它超过50,000字时,它会运行并挂起一半或导致溢出。我的目标:我在A(表1和表2)中准备数据,目的是将我开发的POS标签标记为A(表1)中的单词。 A(sheet2)中的单词是具有最可能标记的唯一单词。我用于此任务的许多用户对MS Office更熟悉。所以,我决定让他们的工作更简单。这个系统将为他们提供一个单词和该单词的所有可能标签,而不是通过大约100的标签并在A(sheet1)中找到单词的标签。他们现在要做的就是在A(sheet1)中单词,查看提供的标签并选择一个标签。请!我需要帮助。谢谢你们!

1 个答案:

答案 0 :(得分:1)

您也可以使用工作表公式执行此操作。例如

Sheet1!B1:  =IF(LEN(IFERROR(VLOOKUP($A1,Sheet2!$A$1:$E$15000,COLUMNS($A:B),FALSE),""))=0,"",VLOOKUP($A1,Sheet2!$A$1:$E$15000,COLUMNS($A:B),FALSE))

直接填写E1;然后选择B1:E1并填充到行300000