重复代码,循环列直到空白单元格

时间:2016-01-11 13:24:51

标签: excel vba loops

我有一个包含130列和65行的表格,我选择了一列,过滤,打印,然后隐藏该列,然后再进行下一次迭代。

代码必须在完成最后一列后停止。

我的想法是,我应该以更合理的方式规划这个,并且有一些代码参考过滤器和打印要求。

然而,这是一部分/摘录。

' Select 5th customer, 9th column (field), filter to exclude blanks,
'select a print range with column offset from A1 and dynamic row range

ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=9, Criteria1:= _
    "<>" ' selects "blank"
Range(Range("A1"), Range("A1").Offset(0, 8)).Select ' selects A1 to 9th column.
Range(Selection, Selection.End(xlDown)).Select 'selects dynamic rectangle A1 to G1 down, defined print selection

Selection.PrintOut Copies:=1 'print above defined print selection


ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=9 ' filters on "select all"
Columns("I:I").Select
Selection.EntireColumn.Hidden = True
Range("Table1[[#Headers],[Material / SITE]]").Select


 ' Select 6th customer, 10th column (field), filter to exclude blanks,
'select a print range with column offset from A1 and dynamic row range

ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=10, Criteria1:= _
    "<>" ' selects "blank"
Range(Range("A1"), Range("A1").Offset(0, 9)).Select ' selects A1 to 10th column.
Range(Selection, Selection.End(xlDown)).Select 'selects dynamic rectangle A1 to G1 down, defined print selection

Selection.PrintOut Copies:=1 'print above defined print selection


ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=10 ' filters on "select all"
Columns("j:j").Select
Selection.EntireColumn.Hidden = True
Range("Table1[[#Headers],[Material / SITE]]").Select


' Select 7th customer, 11th column (field), filter to exclude blanks,
'select a print range with column offset from A1 and dynamic row range

ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11, Criteria1:= _
    "<>" ' selects "blank"
Range(Range("A1"), Range("A1").Offset(0, 10)).Select ' selects A1 to 11th column.
Range(Selection, Selection.End(xlDown)).Select 'selects dynamic rectangle A1 to G1 down, defined print selection

Selection.PrintOut Copies:=1 'print above defined print selection


ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11 ' filters on "select all"
Columns("k:k").Select
Selection.EntireColumn.Hidden = True
Range("Table1[[#Headers],[Material / SITE]]").Select


' Select 8th customer, 12th column (field), filter to exclude blanks,
'select a print range with column offset from A1 and dynamic row range

ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=12, Criteria1:= _
    "<>" ' selects "blank"
Range(Range("A1"), Range("A1").Offset(0, 11)).Select ' selects A1 to 12th column.
Range(Selection, Selection.End(xlDown)).Select 'selects dynamic rectangle A1 to G1 down, defined print selection

Selection.PrintOut Copies:=1 'print above defined print selection


ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=12 ' filters on "select all"
Columns("l:l").Select
Selection.EntireColumn.Hidden = True
Range("Table1[[#Headers],[Material / SITE]]").Select

1 个答案:

答案 0 :(得分:0)

您需要使用计数器循环遍历列,然后您只需要一段代码:

注意:您需要计数器从第一列到130 +该列(不是1到130)运行

伪码:

For counter = 1 to 130 (number of customers)
  ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=counter,    Criteria1:= _
"<>" ' selects "blank"
 Range(Range("A1"), Range("A1").Offset(0, counter-1)).Select ' selects A1 to    11th column.
Range(Selection, Selection.End(xlDown)).Select 'selects dynamic rectangle A1 to G1 down, defined print selection

Selection.PrintOut Copies:=1 'print above defined print selection


ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=counter ' filters on "select all"
Columns("counter:counter").Select
Selection.EntireColumn.Hidden = True
Range("Table1[[#Headers],[Material / SITE]]").Select

Next counter