循环遍历VBA中的列

时间:2016-05-30 14:05:46

标签: excel vba excel-vba

我想循环遍历文件excel中的列,而不仅仅是列A,但我没有成功。你能帮帮我吗? 它是我阻止的7行(Filepath)。如果我为icol更改了A列,它就不起作用了。

Set workboo = Workbooks.OpeN("C:\Users\Desktop\list_files.xlsx")

Set worksh = workboo.Worksheets("List_File")

For j = 1 To 10
   icol = 2 * j - 1
   numRows = worksh.Cells(Application.Rows.Count, icol).End(xlUp).Row        
    For i = 2 To numRows
            Filepath = worksh.Range("A" & i).Value  'i changed column A for icol but it doesn t work 
            fso.CopyFile Filepath, Destination, True
        Next

1 个答案:

答案 0 :(得分:0)

要遍历列,它将更容易使用Cells

Filepath = worksh.Cells(i, icol).Value

而且:

numRows = worksh.Cells(Application.Rows.Count, icol).End(xlUp).Row  

应该是

numRows = worksh.Cells(worksh.Rows.Count, icol).End(xlUp).Row