宏工作在旧工作表,新工作表循环无休止

时间:2016-08-04 18:54:38

标签: excel vba

使用代码删除所有标点符号和奇数字符。我尝试添加一个部分,它将在单元格的开头和结尾添加一个空格,以确保以后的搜索工作。它无休止地循环,所以我把新的代码拿出来,但它仍然无休止地循环。

我尝试关闭并重新打开文件,它仍然无休止地循环。一个旧的副本工作得很好。为什么是这样? (我不确定如何附加文件。任何帮助都会很棒。)

旧代码和此代码完全相同(我复制粘贴,并手动重新输入,多次确认)。

我添加了另一个功能,但它是一个不同宏中的简单单元格计数器。我不明白为什么会影响这一点。

Option Explicit

Sub RemovePunctuation()
Dim cell As Range
With CreateObject("vbscript.regexp")
  .Pattern = "[^A-Za-z\ ]"
  .Global = True
  For Each cell In Worksheets("Raw").Range("A2:A1000").SpecialCells(xlCellTypeConstants)
    cell.Value = .Replace(cell.Value, vbNullString)
  Next cell
End With

For Each cell In Worksheets("Raw").Range("A2:A1000").SpecialCells(xlCellTypeConstants)
cell.Value = LCase(cell.Value)
Next cell

End Sub

同一工作表上的其他代码 - 计算唯一单词

Sub ExtractWords()
Dim X As Variant, S As Variant, S2 As Variant, S3 As Variant, str As Variant, key As Variant
Dim oDict As Scripting.Dictionary
Dim i As Double, j As Double, k As Double
Dim Anchor As Range

Set oDict = New Scripting.Dictionary

With ThisWorkbook
   'Clear past output
   With .Sheets("Output")
       .Range("a2:" & .Cells(.Rows.Count, .Columns.Count).Address).ClearContents
   End With

'Fill array with text to search
   With .Sheets("Raw")
       X = .Range("a2:a" & .Range("a" & .Rows.Count).End(xlUp).Row).Value2
   End With
End With

For i = LBound(X) To UBound(X)
 S = Split(X(i, 1), " ")

  For j = LBound(S, 1) To UBound(S, 1)
      If oDict.Exists(LCase(S(j))) Then
          oDict.Item(LCase(S(j))) = oDict.Item(LCase(S(j))) + 1
      Else
          oDict.Add LCase(S(j)), 1
      End If
  Next j
Next i

'Output results to sheet "Output"
With ThisWorkbook.Sheets("Output")
For Each key In oDict.Keys
    Set Anchor = .Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
    Anchor = key
    Anchor.Offset(0, 1) = oDict.Item(key)
Next key

.Range("a1:" & .Range("a" & .Rows.Count).End(xlUp).Offset(0, 1).Address).Sort .Range("b:b"), xlDescending

End With

End Sub

添加一个列,用于计算使用该单词的句子数。 (为什么我需要在每个句子的开头和结尾添加一个空格。)

Option Explicit
Sub CountCards()

Worksheets("Output").Activate
'Range("C2:C5000").Formula = "=IF(COUNTIF(Raw!A:A,""* ""&A2&"" *"")=0,"""",COUNTIF(Raw!A:A,""* ""&A2&"" *""))"

Range("C2:C5000").Formula = "=IF(COUNTIF(Raw!A:A,""* ""&A2&"" *"")=0,"""",COUNTIF(Raw!A:A,""* ""&A2&"" *""))"

End Sub

1 个答案:

答案 0 :(得分:0)

我怀疑它与计算有关。您有一个过程在另一个工作表的C列中插入公式,该工作表引用了A列。如果C列已经填充了公式,那么更改A列中的值(从属)可能会触发所有这些公式的重新计算

{{1}}