简体   繁体   English

在Excel VBA中的多个列上设置列宽

[英]Setting a column width on multiple columns in Excel VBA

Jeeped and CLR kindly provided the code that added a variable number of columns to a worksheet named sht02AnalysisSummary starting at Column D whilst copying the borders and formulae of Column C. Jeeped和CLR提供了代码,该代码从D列开始向名为sht02AnalysisSummary的工作表中添加了可变数目的列,同时复制了C列的边界和公式。

AddCol = txtNrEvaluated

With sht02AnalysisSummary
    Set rangeCopy = .Range(.Cells(3, "C"), .Cells(.Rows.Count, "C").End(xlUp))
    rangeCopy.Copy Destination:=.Cells(3, .Columns.Count).End(xlToLeft).Offset(0, 1).Resize(rangeCopy.Rows.Count, AddCol)
End With

This code, however, does not copy the format of Column C in terms of width and although I have experimented with EntireColumn.ColumnWidth = 15 with the With End With and even its own With End With , I have been unsuccessful. 但是,此代码不会复制宽度的Column C格式,尽管我尝试了EntireColumn.ColumnWidth = 15With End With甚至是自己的With End With ,但我一直没有成功。

Any assistance would be much appreciated. 任何帮助将不胜感激。

Expanding on my comment below your question. 在您的问题下方扩展我的评论。

Column Widths are only copied across when you are copying across entire columns and nor ranges. 仅当跨整个列和整个范围进行复制时,才复制列宽。 Also rng.ColumnWidth = 15 should work 同样rng.ColumnWidth = 15应该工作

Is this what you are trying? 这是您要尝试的吗?

Dim rangeCopy As Range, destRng As Range

AddCol = 2

With sht02AnalysisSummary
    Set rangeCopy = .Range(.Cells(3, "C"), .Cells(.Rows.Count, "C").End(xlUp))
    Set destRng = .Cells(3, .Columns.Count).End(xlToLeft).Offset(0, 1).Resize(rangeCopy.Rows.Count, AddCol)
    rangeCopy.Copy destRng
    destRng.ColumnWidth = 15
End With

Screenshot 截图

在此处输入图片说明

If you want to copy the source tab's column C width across to the new columns, you could use: 如果要将源选项卡的列C宽度复制到新列中,可以使用:

Dim rangeCopy As Range, rangePaste As Range
Dim Addcol As Integer

Addcol = NrEvaluated

With sht02AnalysisSummary

    Set rangeCopy = .Range(.Cells(3, "C"), .Cells(.Rows.Count, "C").End(xlUp))
    Set rangePaste = .Cells(3, .Columns.Count).End(xlToLeft).Offset(0, 1).Resize(rangeCopy.Rows.Count, Addcol)

    rangeCopy.Copy Destination:=rangePaste
    rangePaste.EntireColumn.ColumnWidth = rangeCopy.EntireColumn.ColumnWidth

End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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