从Excel复制非连续区域到Word

huangapple go评论59阅读模式
英文:

Copy a Non-contiguous Range From Excel to Word

问题

以下是翻译好的部分:

我有以下的代码,我只想要给定的范围,但每次我使用我的宏时,范围C28到H28也会被复制并粘贴到Word文件中。

这是选择范围的代码:

Dim rangeSP As Range 
Set rangeSP = Sheets("Details").Range("C27:H27") 

Dim rangeSP2 As Range 
Set rangeSP2 = Sheets("Details").Range("C29:H30") 

Dim rangeSpecification As Range 

Set rangeSpecification = Union(rangeSP, rangeSP2)

这是被复制和粘贴到Word文件的部分:

wordDoc.Application.Selection.Find.Execute "Where it will be put"

wordApp.Selection.MoveRight Unit:=wdCharacter, Count:=1

wordApp.Selection.TypeParagraph

rangeSpecification.Copy 

wordApp.Selection.PasteExcelTable False, True, False

我想要从Excel中复制表格到Word文件中。因此,我尝试使用联合范围,因为在数据集之间有不需要的行。但是当我使用我的宏时,不需要的行也被复制并粘贴到Word文件中。

英文:

I have the following Code and I only want the Ranges that are given, but every time I use my Macro, the range C28 to H28 is also copied and pasted into the word-file.

This the Code that selects the ranges:

Dim rangeSP As Range 
Set rangeSP = Sheets("Details").Range("C27:H27") 

Dim rangeSP2 As Range 
Set rangeSP2 = Sheets("Details").Range("C29:H30") 

Dim rangeSpecification As Range 

Set rangeSpecification = Union(rangeSP, rangeSP2) 

This where it gets copied and pasted into the word file:

wordDoc.Application.Selection.Find.Execute "Where it will be put"

wordApp.Selection.MoveRight Unit:=wdCharacter, Count:=1

wordApp.Selection.TypeParagraph

rangeSpecification.Copy 

wordApp.Selection.PasteExcelTable False, True, False

I want to copy Tables from an Excel into a Word-File. So, I tried it with Union Ranges, because there are Rows in-between the Data Sets that I don't need. But when I use my Macro, the unwanted rows are also copied and pasted into the word file.

答案1

得分: 1

复制和粘贴多个范围(非连续的)可能会有些棘手。将这些范围粘贴到Excel工作表中会如预期一样。但是,跨应用程序(例如Word或PowerPoint)粘贴将包括被跳过的单元格。

对于您的情况,您有两个选项:

  • 将多个Excel范围粘贴到未使用的工作表或新的Excel工作表中。然后复制合并的范围并粘贴到Word中。
rangeSpecification.Copy ActiveSheet.Range("AA1")
Application.CutCopyMode = False
Set rngData = ActiveSheet.Range("AA1").CurrentRegion
rngData.Copy
wordApp.Selection.PasteExcelTable False, True, False
rngData.Clear
  • 在从Excel粘贴后,使用Word中的VBA代码删除不需要的中间行。(假设Word文档中只有一个表格。)
ThisDocument.Tables(1).Rows(2).Delete
英文:

Copying and pasting multiple ranges (non-contiguous) can be tricky. Pasting such ranges within Excel sheets will be as expected. However, pasting across applications (Word or PowerPoint) will include the skipped cells.

For your situation, you have two options:

  • Paste the multiple Excel ranges onto unused range on ative sheet or a new Excel sheet. Then copy the consolidated range and paste into Word.
    rangeSpecification.Copy ActiveSheet.Range("AA1")
    Application.CutCopyMode = False
    Set rngData = ActiveSheet.Range("AA1").CurrentRegion
    rngData.Copy
    wordApp.Selection.PasteExcelTable False, True, False
    rngData.Clear
  • Using VBA code in Word to delete the unwanted in-between rows after pasting from Excel. (Assuming there is only one table in Word document.)
ThisDocument.Tables(1).Rows(2).Delete

huangapple
  • 本文由 发表于 2023年7月17日 16:32:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定