简体   繁体   中英

paste to active cell VBA excel

I need to paste text from a Word table to an Excel worksheet, but because some of the cells in Word have line breaks that I need to preserve in Excel, a straight copy/paste is not an option--Excel interprets any line breaks as cell breaks. To work around this, I'm replacing the line breaks in Word with dummy text (eg, "@@@"), then pasting into Excel, and finally running find/replace to replace the dummy text with an Excel-compatible line break (alt+0010).

I recorded a macro to do this:

Sub IDA()
'
' IDA Macro
'
' Keyboard Shortcut: Ctrl+q
'
Sheets("Deployment").Select
ActiveWindow.SmallScroll Down:=-12
Range("C2").Select
ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
    False, NoHTMLFormatting:=True
Selection.Replace What:="@@@", Replacement:="" & Chr(10) & "", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
End Sub

But when I move the the next block of text to paste, it pastes back to the original destination. I can see the sheet ("Deployment") and range ("C2") are both hard-coded. I tried removing those lines, but that broke the script--nothing happens when I run it.

I could simplify the script to just do the search and replace, but I don't trust the other users to use the paste special option when they paste into Excel, which means the spreadsheet may end up with other formatting from Word that I don't want.

How do I modify my script so it simply pastes into the active worksheet and cell?

try to remove the 2 hardcoded lines, and use:

Selection.PasteSpecial ....

so the end code:

Sub IDA()
'
' IDA Macro
'
' Keyboard Shortcut: Ctrl+q
'
ActiveWindow.SmallScroll Down:=-12
Selection.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
    False, NoHTMLFormatting:=True
Selection.Replace What:="@@@", Replacement:="" & Chr(10) & "", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
End Sub

This turned out to be an easy fix. Simply clicking the "Use Relative References" button (directly under the record button) prior to recording the macro did the trick. It yielded the following script:

Sub IDA()
'
' IDA Macro
'
' Keyboard Shortcut: Ctrl+q
'
    ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:= _
        False
    Selection.Replace What:="@@@", Replacement:="" & Chr(10) & "", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub

Not sure if this is the most efficient code, but it will do the job for myself and the three other people doing this a few times per quarter. There's never enough data being copied to notice a difference. A lot of effort to satisfy the bellyaching of one coworker who couldn't be bothered with a whopping two keystrokes and ensure the paste special option is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM