Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用for循环在VBA中设置列宽范围_Vba_Excel_For Loop_Range - Fatal编程技术网

使用for循环在VBA中设置列宽范围

使用for循环在VBA中设置列宽范围,vba,excel,for-loop,range,Vba,Excel,For Loop,Range,我希望能够使用某种for循环来设置excel工作表中的列宽,这样我就不必键入要更改列宽的每一列。在这张图片中,我希望所有黑色列的宽度为0.92,绿色为0.83,橙色为7.29,我希望这些列在设定范围内继续超过我在这里显示的宽度。下面是我使用的通用代码,但正如我所说,我不想为了更改宽度而必须键入每一列 sub Set_Column_Width Range("E:E, I:I, M:M, Q:Q, U:U, Y:Y, AC:AC, AG:AG, AK:AK, AO:AO, AS:AS, AT

我希望能够使用某种for循环来设置excel工作表中的列宽,这样我就不必键入要更改列宽的每一列。在这张图片中,我希望所有黑色列的宽度为0.92,绿色为0.83,橙色为7.29,我希望这些列在设定范围内继续超过我在这里显示的宽度。下面是我使用的通用代码,但正如我所说,我不想为了更改宽度而必须键入每一列

sub Set_Column_Width

   Range("E:E, I:I, M:M, Q:Q, U:U, Y:Y, AC:AC, AG:AG, AK:AK, AO:AO, AS:AS, AT:AT, AX:AX").ColumnWidth = 0.92
  Range("G:G,K:K,O:O,S:S").ColumnWidth = 0.83
   Range("F:F, H:H, J:J, L:L").ColumnWidth = 7.29

End sub
使用
列(索引)

另一种方法是根据索引确定宽度

For i = 5 To 100
    'find you what width this column should have
    Columns(i).Columnwidth = x
next i
这是假设宽度不是由颜色定义的,而是由它们的位置定义的

使用
列(索引)

另一种方法是根据索引确定宽度

For i = 5 To 100
    'find you what width this column should have
    Columns(i).Columnwidth = x
next i

这是假设宽度不是由颜色定义的,而是由它们的位置定义的。虽然我不确定这些颜色是什么,但很容易找到。如果整个列都是相同的颜色,您可以在列中循环并查看第1行。差不多

for i = 1 to 1000 'or whatever your last column is
    if cells(1, i).interior.color = rgb(255, 0, 0) then 'red, for example
        cells(1, i).columnwidth = 0.83 'or whatever the column width needs to be here
    else if cells(1, i).interior.color = rgb(...) then 'next color
        cells(1, i).columnwidth = ... 'etc.
    end if
next i

虽然我不确定这些颜色是什么,但很容易找到。如果整个列都是相同的颜色,您可以在列中循环并查看第1行。差不多

for i = 1 to 1000 'or whatever your last column is
    if cells(1, i).interior.color = rgb(255, 0, 0) then 'red, for example
        cells(1, i).columnwidth = 0.83 'or whatever the column width needs to be here
    else if cells(1, i).interior.color = rgb(...) then 'next color
        cells(1, i).columnwidth = ... 'etc.
    end if
next i

如果你正在做一个模式,黑橙绿橙你可以这样循环

for i = [first column] to [last column] step 4
     Columns(i).ColumnWidth = 0.92
     Columns(i+1).ColumnWidth = 7.29
     Columns(i+2).ColumnWidth = 0.83
     Columns(i+3).ColumnWidth = 7.29
next i

如果你正在做一个模式,黑橙绿橙你可以这样循环

for i = [first column] to [last column] step 4
     Columns(i).ColumnWidth = 0.92
     Columns(i+1).ColumnWidth = 7.29
     Columns(i+2).ColumnWidth = 0.83
     Columns(i+3).ColumnWidth = 7.29
next i

你有什么规则可以用吗?i、 e.什么决定了哪列的宽度?是颜色还是图案?对不起,颜色只是为了更好地突出我想做的…是图案我想循环而不是颜色你有什么规则可以使用吗?i、 e.什么决定了哪列的宽度?这是颜色还是有图案?抱歉,颜色只是为了更好地突出我想做的事情…这是我试图循环的图案而不是颜色如果你将
[]
更改为
),并使用
I
I+1
I+2
I+3
,这比我的颜色要好。)如果您将
[]
更改为
()
,并使用
i
i+1
i+2
i+3
;),这比我的更好我喜欢这个答案,因为它考虑了颜色。看起来OP的专栏遵循一种模式,但没有保证,所以我会选择这一个。@BruceWayne。大概OP不希望手动进行着色,然后使用宏进行宽度。哦,解释模棱两可的客户简介的乐趣:)正是。不清楚电子表格是否真的是彩色的,或者是否添加了颜色来说明图案。我喜欢这个答案,因为它考虑了颜色。看起来OP的专栏遵循一种模式,但没有保证,所以我会选择这一个。@BruceWayne。大概OP不希望手动进行着色,然后使用宏进行宽度。哦,解释模棱两可的客户简介的乐趣:)正是。不清楚电子表格是否真的是彩色的,或者是否添加了颜色来说明图案。