Vba word удалить символ

MAnton

25 / 25 / 5

Регистрация: 21.04.2011

Сообщений: 141

1

Удалелние знака параграфа

09.05.2013, 15:00. Показов 3156. Ответов 2

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

Есть текстовый файл (см. приложение), при загрузке которого в Word после каждой строчки появляется знак конца параграфа, каждый абзац разделяется пустой строкой. Нужно сформировать параграфы, для этого нужно удалить знаки конца параграфа.
Ну для упрощения я сначала хочу удалить вообще все знаки параграфов как мне это сделать? вот такой код не работает

VB.NET
1
2
3
4
5
6
7
8
9
Dim myRange As Range
For Each myRange In ActiveDocument.Characters
myRange.Select
If Not myRange Is Nothing Then
    If myRange.Text = Chr(13) Then
        myRange.Delete
    End If
End If
Next myRange

Вложения

Тип файла: txt README.TXT (483 байт, 9 просмотров)



0



Скрипт

5468 / 1148 / 50

Регистрация: 15.09.2012

Сообщений: 3,514

09.05.2013, 15:47

2

Код

Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Sub Procedure_1()
 
    Dim myText As String
    
    '1. Удаляем часть ненужных данных VBA-средствами.
    '1.1. Берём вообще весь текст из документа в переменную "myText".
    myText = ActiveDocument.Range.Text
    
    '1.2. Удаляем из текста символ "Подача строки".
    'В программе "Word" символ "Подача строки" не ставится,
        'если нажимать клавишу "Enter", но если вставлять данные
        'из других документов и в этих документах есть такой символ,
        'то этот символ находится в Word-документе и пользователь ничего
        'не знает о его существовании.
    myText = Replace(myText, Chr(10), "")
    
    '1.3. Вставляем обработанный текст обратно в документ.
    ActiveDocument.Range.Text = myText
    
    '2. Остальное удаляем VBA-Word-средствами.
    '2.1. Удаляем знак абзаца, если перед знаком абзаца есть
        'любой символ, кроме знака абзаца и вставляем пробел.
    With ActiveDocument.Range.Find
        .Text = "(^13)([!^13])"
        .Replacement.Text = " 2"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
 
    '2.2. Удаляем пробел после символа "Конец абзаца".
    With ActiveDocument.Range.Find
        .Text = "(^13)( )"
        .Replacement.Text = "1"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
 
End Sub

Примечания

Т.к. в моём коде весь текст я помещал в VBA-переменную, то можно было бы всю обработку провести и в самой переменной без использования программы «Word».

Например, есть библиотека для поиска и замены «Microsoft VBScript Regular Expressions».



0



15136 / 6410 / 1730

Регистрация: 24.09.2011

Сообщений: 9,999

09.05.2013, 22:46

3

Задача сводится к замене одиночного символа абзаца на пробел, двойного символа абзаца на одиночный.
Без макросов задача решается так:
1. Ctrl+H, заменить ^p^p на ~ (или другой символ, который не встречается в тексте).
2. Заменить ^p на пробел.
3. Заменить ~ на ^p.



0



Sub Procedure_1()
 
    Dim myText As String
    
    '1. Удаляем часть ненужных данных VBA-средствами.
    '1.1. Берём вообще весь текст из документа в переменную "myText".
    myText = ActiveDocument.Range.Text
    
    '1.2. Удаляем из текста символ "Подача строки".
    'В программе "Word" символ "Подача строки" не ставится,
        'если нажимать клавишу "Enter", но если вставлять данные
        'из других документов и в этих документах есть такой символ,
        'то этот символ находится в Word-документе и пользователь ничего
        'не знает о его существовании.
    myText = Replace(myText, Chr(10), "")
    
    '1.3. Вставляем обработанный текст обратно в документ.
    ActiveDocument.Range.Text = myText
    
    '2. Остальное удаляем VBA-Word-средствами.
    '2.1. Удаляем знак абзаца, если перед знаком абзаца есть
        'любой символ, кроме знака абзаца и вставляем пробел.
    With ActiveDocument.Range.Find
        .Text = "(^13)([!^13])"
        .Replacement.Text = " 2"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
 
    '2.2. Удаляем пробел после символа "Конец абзаца".
    With ActiveDocument.Range.Find
        .Text = "(^13)( )"
        .Replacement.Text = "1"
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
 
End Sub

I’m trying to make a macro to delete all special characters in a Word document, My basic reference to all special characters I need to delete for work are:

SPECIAL CHARACTERS

• Time to produce

(8 CCR §9792.24. 3),

µ (the ‘u’ is not a letter, it is actually a symbol ‘µ’) If it documented as “µgm” in the file, and the reviewer is dictating it as “units per gram, ” then put units per gram (note in file states µgm [microgram/mcg]).

Kienböck’s

· Pain interferes…

®

©

· Pain interferes…

*Postsurgical physical

antagonists, α-adrenergic, cholinergic receptor agonists, γ agonists, Voltaren® Gel

•Genotype 1

treatment-naïve

≥ (add to autocorrect = greater than or equal to)

Any special characters found in above text are what I need removed from my Word document at any given time and or replaced with their respective meaning & = and, µ = micro and etc. I’ve tried researching to see if there are any out there but I can’t seem to find any. I don’t really have a set code as I’m a true novice. Any help on this would be greatly appreciated. This would be in Word 2013 or 2007, as I would send this macro for my coworkers as well.

String manipulation is a crucial skill in VBA programming. The skill level of a VBA developer is often determined by how well he/she can manipulate string data. Excel is very strong in mathematics operations and comes with loads of built-in calculation functions. But text manipulation is less straightforward and always requires some creativity and experience.

In this article, I am going to show you how to remove characters from strings. We’ll go through a series of common scenarios, and you’ll learn about how to tackle them with the Replace, Left, Right, Trim, and Instr VBA functions.

Remove All Occurrences of Specific Characters from Strings

The basic VBA skill to remove characters from a string is with the Replace method.

The basic  syntax of VBA Replace method you need to remember is:

Replace (inputString, findwhat, replace)

Example 1 is the most typical approach where the Replace method is used to remove all occurrences of specific character(s) from the input string.

Example 1 – The Most Common Approach, Case Sensitive

Here we want to remove all occurrence of “b” from variable input1,”aabbccAABBCC”, with the expected output of “aaccAABBCC”. In line 6 of the macro, the Replace method looks for “b” and replaces it with an empty string “”.

Sub removechar()
Dim input1 As String
Dim result As String
input1 = "aabbccAABBCC"
'to remove all occurrences of b from input string
result = Replace(input1, "b", "")
MsgBox result
End Sub

After running the macro, the answer is displayed in the message box:

aaccAABBCC

However, this approach with the Replace method is case sensitive. Only the lower case “b” characters were removed but not the “B”. We can modify our macro to automatically handle both lower and upper cases.

Example 2 – Simple Non-Case Sensitive Approach

We can enhance the macro to remove both the lower case “b” and upper case “B” from the input string. In macro removechar2 below, the character to be remove is defined in line 6. Then, in line 7, we first remove the lower case, and then in line 8 the upper case is also removed.

Sub removechar2()
Dim input1 As String
Dim remove1 As String
Dim result As String
input1 = "aabbccaabbcc"
remove1 = "b"	'this is the char to be removed
result = Replace(input1, LCase(remove1), "")
result = Replace(result, UCase(remove1), "")
MsgBox result
End Sub

aaccAACC

Remove the First n Occurrences of Specific Characters from Strings

Sometimes we want to remove only the first n occurrences of specific characters. This is easy to do by using the optional argument “count” of the VBA Replace method:

Syntax:

Replace (inputString, findwhat, replace, start_position, count)

The following macro “removechar3” removes the first 3 occurrences of “b” from input1 “aabbccaabbcc”. The expected result is “aaccaabcc”.

Sub removechar3()
Dim input1 As String
Dim remove1 As String
Dim result As String
input1 = "aabbccaabbcc"
remove1 = "b"
'remove the first 3 occurrences of "b" from input string
result = Replace(input1, remove1, "", , 3)
MsgBox result
End Sub

After running the macro, the answer is displayed in the message box:

aaccaabcc

Remove Characters from Left of Strings

Here we have a list of staff IDs in the format “X12345678” with 9 characters each. We want to remove the leading letter from every ID in the list.

List of staff IDs in Excel

We can use the Right function for this purpose. While each staff ID is 9 characters long, removing 1 character from the left is equivalent to outputting 8 (9 minus 1) characters from the right.

Sub removeLeft()
Dim cell As Range
Dim MyRange As Range
Dim tmp As String
Set MyRange = Selection  'this is your range of data
'loop through every cell in range and remove 1 char from left
For Each cell In MyRange.Cells
    tmp = cell.Value
    'output n - 1 characters from the right
    cell.Value = Right(tmp, Len(tmp) - 1)
Next
End Sub

However, after running the above macro, we realize that it did not fully meet our expectations (see the picture below). Because some of the original IDs have numeric parts with leading zeros. (Lines 1, 5 and 7), Excel is too clever and automatically trims those zeros. (e.g. 021 is treated by Excel as 21.) We want to make sure the zeros remain after the leading letters were removed.

Example with highlighted rows

We can achieve this by forcing Excel to treat numeric data as strings by adding a leading apostrophe to the result. For example, ‘021 will be treated by Excel as string. To achieve this, we can modify line 9 of the macro as shown below:

Sub removeLeft()
Dim cell As Range
Dim MyRange As Range
Dim tmp As String
Set MyRange = Selection  'this is your range of data
'loop through every cell in range and remove 1 char from left
For Each cell In MyRange.Cells
    tmp = cell.Value
    'output n-1 char from the right, and add apostrophe
    cell.Value = "'" & Right(tmp, Len(tmp) - 1)
Next
End Sub

IDs after processing in a list

Remove Characters from Right of Strings

Example 1 – Remove a Fixed Number of Characters from the Right

To remove a fixed number of characters x from the right of strings, you can use the LEFT function. The macro is almost the same as the macro above for removing characters from the left.

Sub removeRight()
Dim cell As Range
Dim MyRange As Range
Dim tmp As String
Set MyRange = Selection  'this is your range of data
'loop through every cell in range and remove 1 char from right
For Each cell In MyRange.Cells
    tmp = cell.Value
    'output n - 1 characters from the left
    cell.Value = Left(tmp, Len(tmp)-3)
Next
End Sub

Example 2 – Remove a Variable Number of Characters from the Right

If we want to remove characters from the right based on the variable position of a certain character(s), we can use the INSTR function in conjunction with the LEFT function.

Here we have a list of email addresses. We want to remove the domain names on the right of each address.

List of emails to process

We can find out the position of “@” with the following VBA statement, which in the case returns 9. Therefore, to remove the domain name, we have to extract the first 8 characters.

x = Instr("[email protected]","@")

The following macro loops through every cell in a selected range and removes the domain names.

Sub removeDomain()
Dim cell As Range
Dim MyRange As Range
Dim tmp As String

Set MyRange = Selection  'this is your range of data

'loop through every cell in range and remove characters after @
For Each cell In MyRange.Cells
    tmp = cell.Value
    '
    cell.Value = Left(tmp, InStr(tmp, "@") - 1)
Next
End Sub

Here’s a couple key takeaways to remember about removing characters from one side or the other of strings:

Scenario Function to be used
Remove characters from the Left RIGHT
Remove characters from the Right LEFT
Position specific remove INSTR (+ LEFT/RIGHT)

Removing Unwanted Spaces from Strings

There are two common scenarios where we want to remove unwanted spaces from strings:

  • Strings with leading and trailing spaces
  • Unwanted extra spaces within string (e.g. double spaces)

Remove Leading and Trailing Spaces from Strings

We can use the Trim function to remove leading and trailing spaces. In the example below we have an input string of  "   This is my data   " (line 5). The Trim function is used in line 6. The macro produces a message box to show a comparison of the input and output.

Sub removespace1()
Dim MyInput As String
Dim result As String
'here is the input string with leading and trailing spaces
MyInput = "   This is my data   "  
result = Trim(MyInput)  'remove leading and trailing spaces

'display result in msgbox
MsgBox "Original text: >" & MyInput & "<" & Chr(10) & _
    "Original length: " & Len(MyInput) & Chr(10) & _
    "Final text: >" & result & "<" & Chr(10) & _
    "Final length: " & Len(result)
End Sub

Messagebox showing before and after with spaces trimmed

Removing “extra spaces” is different from removing “all spaces.” You can visualize the difference here:

Example string with every single space removed

The macro removeAllUnwantedSpace below removes leading and trailing spaces as well as all unwanted repeated spaces. Pay special attention to line 10 to 12. The same statement must be run 3 times to ensure all repeated spaces are replaced with single spaces.

Sub removeAllUnwantedSpace()
Dim MyInput As String
Dim result As String
'here is the input string with unwanted spaces
MyInput = "   This     is    my   data   "
'first remove leading and trailing spaces
result = Trim(MyInput)  
'then replace double spaces with single space
'this step has to be repeated 3 times
result = Replace(result, "  ", " ")
result = Replace(result, "  ", " ")
result = Replace(result, "  ", " ")
'display result in msgbox
MsgBox "Original text: >" & MyInput & "<" & Chr(10) & _
    "Original length: " & Len(MyInput) & Chr(10) & _
    "Final text: >" & result & "<" & Chr(10) & _
    "Final length: " & Len(result)
End Sub

Messagebox showing spaces removed

Remove Numbers from String

Sometimes we want to remove all numeric characters from strings. We can achieve this with a For-Next loop to remove each of the digits from 0 to 9.

The custom VBA function below shows how to remove all numeric characters from an input string.

Function removenumbers(ByVal input1 As String) As String
Dim x
Dim tmp As String
tmp = input1
'remove numbers from 0 to 9 from input string
For x = 0 To 9
    tmp = Replace(tmp, x, "")
Next
'return the result string
removenumbers = tmp
End Function

You could also consider using a regular expression for a task like this.

Remove line breaks from string

Sometimes our data may contain line breaks with the strings and we want to remove them. A line break is actually an invisible character. We can simply use the VBA Replace function to remove them.

There are 2 types of line break characters. The most common one is Chr(10). But a less common one is Chr(13). Chr(13) is more common on the Mac. We can use a statement like this to remove such line break characters:

result = Replace(myString, Chr(10))
'or
result = Replace(Replace(myString, Chr(10)), Chr(13))

Remove Accented Characters from Names

There is a special situation that we want to remove the “accent” from accented characters in names. As an example, we have a list of French names below. In this case, we want to stick to “regular” English letters. Our expected result is shown on the right.

The macro “removeAccented” below loops through every cell in a selected range and replaces all accented characters with the corresponding regular English letters.

At the beginning of the macro, two constant strings were defined: “Accent” holds all accented characters; and “Normal” holds the corresponding regular English letters.

The For-Next loop (which begins in line 13 of code) loops through every accented character in the constant “Accent” and replaces with the alphabet in the same position in the constant “Normal”, e.g. “à” which is in position 1 of “Accent” will be replaced with “a” in position 1 of “Normal”.

Sub removeAccented()
Const Accent = _
"àáâãäåçèéêëìíîïðñòóôõöùúûüýÿŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝ"
Const Normal = _
"aaaaaaceeeeiiiidnooooouuuuyySZszYAAAAAACEEEEIIIIDNOOOOOUUUUY"
Dim cell As Range
Dim x As Integer
Dim tmp As String
'loop through each cell in selection
For Each cell In Selection.Cells
    tmp = cell.Value
    'loop through the constants, and replace 
    For x = 1 To Len(Accent)
        tmp = Replace(tmp, Mid(Accent, x, 1), Mid(Normal, x, 1))
    Next
    cell.Value = tmp
Next
End Sub

Conclusion

You can see in the above examples that text manipulation in Excel VBA indeed requires some creativity. There is no single universal approach to remove characters from strings which can serve all scenarios. The Replace function is the most important function to use. In some cases, you also have to apply the Left, Right, Trim, Instr functions.

The technique to loop through cells in a range has also been used in most of the examples above. You may refer to my article on this topic for more examples on the topic.

Tagged with: Characters, InStr, LEFT, Line Breaks, Replace, RIGHT, Spaces, String Processing, Strings, Substring, Trimming, VBA


You can use the following basic syntax to remove the last character from a string using VBA:

Sub RemoveLastChar()

    Dim i As Integer
    Dim myString As String

    For i = 2 To 11
    myString = Range("A" & i)
    Range("B" & i) = Left(myString, Len(myString) - 1)
    Next i
    
End Sub

This particular example removes the last character from each string in the range A2:A11 and outputs the results in the range B2:B11.

The following example shows how to use this syntax in practice.

Example: Using VBA to Remove Last Character from Strings

Suppose we have the following list of basketball team names in Excel:

Suppose we would like to remove the last character from each team name.

We can create the following macro to do so:

Sub RemoveLastChar()

    Dim i As Integer
    Dim myString As String

    For i = 2 To 11
    myString = Range("A" & i)
    Range("B" & i) = Left(myString, Len(myString) - 1)
    Next i
    
End Sub

When we run this macro, we receive the following output:

VBA remove last character from string

Column B displays each of the strings in column A with the last character removed.

If you would instead like to remove the last n characters from a string, simply change the 1 in the Left method to a different number.

For example, we can create the following macro to remove the last 2 characters from a string:

Sub RemoveLastTwoChar()

    Dim i As Integer
    Dim myString As String

    For i = 2 To 11
    myString = Range("A" & i)
    Range("B" & i) = Left(myString, Len(myString) - 2)
    Next i
    
End Sub

When we run this macro, we receive the following output:

Column B displays each of the strings in column A with the last two characters removed.

Note: You can find the complete documentation for the VBA Left method here.

Additional Resources

The following tutorials explain how to perform other common tasks using VBA:

VBA: How to Count Occurrences of Character in String
VBA: How to Check if String Contains Another String
VBA: How to Count Cells with Specific Text

Понравилась статья? Поделить с друзьями:
  • Vba word удалить разрыв раздела
  • Vba word удалить пробелы
  • Vba word удалить интервал после абзаца
  • Vba word удалить выделенное
  • Vba word удалить весь текст