excel 复制包含合并单元格的区域时保留行高

0wi1tuuw  于 2023-03-04  发布在  其他
关注(0)|答案(2)|浏览(292)

Inspection templates
根据要进行的检查,我从检查模板加载检查表(名称定义的选择),并将其添加到包含要检查的选定标签的所有标签信息的工作表中

Sub copycells()

' copycells Macro
'

'
    Application.Goto Reference:="Ex_d_Visual"
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A9").Select    
    ActiveSheet.Paste

End Sub

问题是合并的单元格高度不会复制到新工作表中。“EX_d_Visual”= A1:K41
我尝试了许多不同的复制粘贴选项和特殊粘贴选项,但似乎不能让它工作,我想我可能需要使用一个“for cell next”循环,获取每个原始单元格的高度,然后将新的工作表等效单元格设置为相同的高度。使用范围“Ex_d_Visual”从原始单元格获取单元格高度是可行的但只是不知道如何设置新的工作表,因为我只知道单个单元格,我已经复制到。

efzxgjgh

efzxgjgh1#

调整复制区域中的行高

Sub CopyCells()

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim srg As Range: Set srg = wb.Names("Ex_d_Visual").RefersToRange
    
    Dim dws As Worksheet: Set dws = wb.Sheets("Sheet1")
    Dim dCell As Range: Set dCell = dws.Range("A9")
    
    srg.Copy dCell
        
    Dim sCell As Range
        
    For Each sCell In srg.Cells
        dCell.RowHeight = sCell.RowHeight
        Set dCell = dCell.Offset(1)
    Next sCell
        
End Sub
qxsslcnc

qxsslcnc2#

在您的示例中,由于您知道目标合并区域中的行数相同,因此可以使用.Resize将其定义为与源区域大小相同。
然后,在行上循环以应用原始行高,如下所示:

Const RangeName = "Ex_d_Visual"
Const SheetName = "Sheet1"
Const RangeAddress = "A9"

Dim SourceRange As Range
Set SourceRange = ThisWorkbook.Names(RangeName).RefersToRange

Dim DestinationRange As Range
Set DestinationRange = ThisWorkbook.Sheets(SheetName).Range(RangeAddress).Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)

Dim Row As Range, Offset As Long
For Each Row In SourceRange.Rows
    DestinationRange.Rows(1 + Offset).RowHeight = Row.Height
    Offset = Offset + 1
Next Row

相关问题