2014-01-28 101 views
0

它应该是非常容易的,但我不能找到只选择一个整列只选择一列

的我需要执行一个循环,并粘贴复制列中的每个n列的方式。 的问题是,当我写

Dim a As Integer 
Dim b As Integer 


b = 1 
'a = InputBox("Insert number", "Insert number") 
a = 6 

While Application.CountA(Columns(b)) > 0 
    b = b + a 
    Columns(b).Select 
    b = b + 1 
Wend 

不仅列(b)中被选择,但几个是。数字有所不同,但从1到10左右。我不知道这里有什么问题!我已经尝试了很多具有相同结果的小东西。

+3

我不知道我明白你在做什么。命令'Columns(b).Select'将只选择一列,这一列对应于变量'b'中存储的编号。你必须更详细地解释你想要做什么,例如'While'循环的目的。 –

+0

我在工作表中有很多数据,我需要通过图像导出其中的一部分。开头的标题必须包含在每张图片中,所以我需要一个可以复制列并将其粘贴到每列的宏。对不起,如果我已经严重解释自己。 – Sturm

+0

如果你想复制标题,也许你需要复制行,而不是列。你能提供一个截图吗? – Makah

回答

0

从内存中,如果你改变:

Columns(b).Select 

到:

Columns(b).EntireColumn.Select 

然后它可能工作。

+0

对不起,这不起作用 – Sturm

+0

@Sturm,你有任何合并单元格与你试图选择的列相交吗? – MarkHone

+0

是的!就是这样,我刚刚意识到这一点,然后回到这里来评论它。谢谢 – Sturm

0

你可以试试:

Dim a As Integer 
Dim b As Integer 

b = 1 : a = 6 

While Application.CountA(Columns(b)) > 0 
    b = b + a 
    Columns(b).Copy(Sheets("MySecondSheet").Columns(b).Cells(1,1)) 
    b = b + 1 

    Debug.Print "a = " & a & "; b = " & b ' just for debug your values 
Wend 

编辑:

我的代码复制整列并粘贴 “MySecondSheet”。如果你想复制标题,也许你需要复制行,而不是列。

你能提供截图吗?

1

从我收集的内容来看,您希望每隔6列选择一个列,并在每个列中插入预定列值。

Sub selectCols() 
    'b=1 is your starting column, 20 is the last column, step 6 makes it go up in 6's, if needs be, you can set this to a 
    For b = 1 To 20 Step 6 
     'I like to do this to ensure 100% there is no carry-over of selected cells from the previous run 
     Range("A1").Select 

     Columns(b).EntireColumn.Select 
     'do your pasting here 
    Next b 
End Sub