Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA脚本不影响表中的第一个单元格_Vba_Ms Word_Trim - Fatal编程技术网

VBA脚本不影响表中的第一个单元格

VBA脚本不影响表中的第一个单元格,vba,ms-word,trim,Vba,Ms Word,Trim,我有一个VBA脚本,通过搜索NBSP,然后用常规空格替换它们,然后使用trim清理文本前后的空格,可以在Microsoft Word中从表中去掉不间断的空格。目前,除了修剪表中的第一个单元格外,该代码将适用于所有操作(但是,它会替换第一个单元格中的空格)。我希望找出Word/VBA是否有任何独特的行为可能导致这种情况,因为我将在MS Word中为表格制作几个宏 Sub nbsbCleanupQuick() ' use find and replace to replace all nbsp in

我有一个VBA脚本,通过搜索NBSP,然后用常规空格替换它们,然后使用trim清理文本前后的空格,可以在Microsoft Word中从表中去掉不间断的空格。目前,除了修剪表中的第一个单元格外,该代码将适用于所有操作(但是,它会替换第一个单元格中的空格)。我希望找出Word/VBA是否有任何独特的行为可能导致这种情况,因为我将在MS Word中为表格制作几个宏

Sub nbsbCleanupQuick()
' use find and replace to replace all nbsp in the table with regular spaces
' use trim to clean up unwanted spaces
' For some reason this macro will replace nbsp with regular spaces but will not trim in the first cell of a table

' vars for counting
Dim numRows, numCells As Integer
Dim rindex, cindex As Integer

'container for cell contents
Dim container As String

Selection.Tables(1).Select
numRows = Selection.Rows.Count

For rindex = 1 To numRows
numCells = Selection.Tables(1).Rows(rindex).Cells.Count
For cindex = 1 To numCells
    container = ""
    Selection.Tables(1).Rows(rindex).Cells(cindex).Range.Select
    With Selection.Find
        .Text = Chr(160)
        .Replacement.Text = Chr(32)
        .Execute Replace:=wdReplaceAll, Wrap:=wdFindContinue
    End With
    If (Len(Selection.Text) - 2 > 0) Then
        container = Left(Selection.Text, Len(Selection.Text) - 2)
    End If
    Selection.Text = Trim(container)
Next
Next

End Sub

任何帮助都将不胜感激:)

因为您正处于更大项目的开始阶段,我将给您一些建议,对您的代码进行更多更改。请参阅下面建议的解决方案,并附上一些注释,解释代码内部更改的原因。显然,这解决了您在问题中描述的问题

Sub nbsbCleanupQuick()
' use find and replace to replace all nbsp in the table with regular spaces
' use trim to clean up unwanted spaces
' For some reason this macro will replace nbsp with regular spaces but will not trim in the first cell of a table

' vars for counting
Dim numRows, numCells As Integer
Dim rindex, cindex As Integer

'container for cell contents
Dim container As String

Selection.Tables(1).Select
numRows = Selection.Rows.Count

'do replacement once, at the beginning
'it will be more efficient option
    ActiveDocument.Tables(1).Select
    With Selection.Find
        .Text = Chr(160)
        .Replacement.Text = Chr(32)
        .Execute Replace:=wdReplaceAll ', Wrap:=wdFindContinue
        'keep wrap parameter switch off to do replacement only within table 1
    End With

'to iterate through all cells in table use this kind of loop
Dim tblCell As Cell
For Each tblCell In ActiveDocument.Tables(1).Range.Cells

        'it's not necessary to select but I didn't want to
        'change your solution completly
        tblCell.Select

    'this check is easier to understand
    If (Len(Selection.Text) > 2) Then
        container = Left(Selection.Text, Len(Selection.Text) - 2)
    End If
    Selection.Text = Trim(container)

Next

End Sub

因为您正处于更大项目的开始阶段,所以我将对您的代码进行更多的更改,从而为您提供一些建议。请参阅下面建议的解决方案,并附上一些注释,解释代码内部更改的原因。显然,这解决了您在问题中描述的问题

Sub nbsbCleanupQuick()
' use find and replace to replace all nbsp in the table with regular spaces
' use trim to clean up unwanted spaces
' For some reason this macro will replace nbsp with regular spaces but will not trim in the first cell of a table

' vars for counting
Dim numRows, numCells As Integer
Dim rindex, cindex As Integer

'container for cell contents
Dim container As String

Selection.Tables(1).Select
numRows = Selection.Rows.Count

'do replacement once, at the beginning
'it will be more efficient option
    ActiveDocument.Tables(1).Select
    With Selection.Find
        .Text = Chr(160)
        .Replacement.Text = Chr(32)
        .Execute Replace:=wdReplaceAll ', Wrap:=wdFindContinue
        'keep wrap parameter switch off to do replacement only within table 1
    End With

'to iterate through all cells in table use this kind of loop
Dim tblCell As Cell
For Each tblCell In ActiveDocument.Tables(1).Range.Cells

        'it's not necessary to select but I didn't want to
        'change your solution completly
        tblCell.Select

    'this check is easier to understand
    If (Len(Selection.Text) > 2) Then
        container = Left(Selection.Text, Len(Selection.Text) - 2)
    End If
    Selection.Text = Trim(container)

Next

End Sub

第一个单元格中有什么文本?按原样在此处键入。问题是,您的代码对我来说运行良好…不同的文本,事实上,第一个单元格通常是空的,只有一个nbsp,宏将nbsp替换为常规空格,但不会从第一个单元格中删除空白。老实说,对于我所需要的,这是非常好的。。。现在。我只想知道是什么原因导致它只能在第一个单元格上执行一半的操作。第一个单元格中有什么文本?按原样在此处键入。问题是,您的代码对我来说运行良好…不同的文本,事实上,第一个单元格通常是空的,只有一个nbsp,宏将nbsp替换为常规空格,但不会从第一个单元格中删除空白。老实说,对于我所需要的,这是非常好的。。。现在。我只想知道是什么导致它只在第一个单元上执行一半的操作。这确实很有趣。我没有意识到with块在没有wrap::continue的情况下会在整个表上执行。对我来说,“activedocument.tables(1)”行意味着它获取文档中的第一个表。我的文档有30多个表,这就是为什么我将其设置为强制用户选择表而不是引用特定表的原因。如果warp::continue行影响文档中的所有表,这就解释了为什么宏有时可能需要几分钟才能工作。这给了我很多考虑。谢谢。这确实很有趣。我没有意识到with块在没有wrap::continue的情况下会在整个表上执行。对我来说,“activedocument.tables(1)”行意味着它获取文档中的第一个表。我的文档有30多个表,这就是为什么我将其设置为强制用户选择表而不是引用特定表的原因。如果warp::continue行影响文档中的所有表,这就解释了为什么宏有时可能需要几分钟才能工作。这给了我很多考虑。非常感谢。