Copying number to clipboard, subtracting 398 then pasting the valueto overwrite the original

J

John

Hi.

I am very new to this.

I'd be really grateful if someone could help/guide me. I want to
create a macro in Microsoft Word but I don't know visual basic.


I want to be able to highlight a number then:

- copy it to the clipboard
- subtract 298
- paste the value to the Word document, overwriting the original text
 
P

Pesach Shelniitz

Hi John,

There is no need to involve the clipboard if you are only changing the
selected number. The following macro subtracts 298 from the selected number.

Sub Subtract298()
If IsNumeric(Selection.Text) Then
Selection.Text = Val(Selection.Text) - 298
Else
MsgBox "The selected text is not a number."
End If
End Sub

Hope this helps,
Pesach Shelnitz
My Web site: http://makeofficework.com
 
G

Greg Maxey

John,

If the number is already highlighted in the document, then you don't need to
copy it to the clipboard:

Sub ScratchMaco()
If IsNumeric(Selection.Text) Then
Selection.Text = Selection.Text - 298
End If
End Sub

If there is some reason you have to put it in the clipboard, you could try:

Sub ScratchMacoII()
Dim myCopy As DataObject
Dim myPaste As DataObject
'Must have a reference to Microsoft Forms 2.0 Object Library enabled
Dim myRng As Range
If IsNumeric(Selection.Text) Then
Set myCopy = New DataObject
myCopy.SetText Selection.Text - 298
myCopy.PutInClipboard
Set myPaste = New DataObject
myPaste.GetFromClipboard
Selection.Text = myPaste.GetText(1)
End If
End Sub
 
J

John

Hi John,

There is no need to involve the clipboard if you are only changing the
selected number. The following macro subtracts 298 from the selected number.

Sub Subtract298()
    If IsNumeric(Selection.Text) Then
        Selection.Text = Val(Selection.Text) - 298
    Else
        MsgBox "The selected text is not a number."
    End If
End Sub

Hope this helps,
Pesach Shelnitz
My Web site:http://makeofficework.com

Wow, thank you so much. It worked! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top