将每个工作表的最后一列导出到文本文件中

时间:2011-08-23 09:19:11

标签: excel vba text

我有一个包含许多标签的Excel电子表格。我想将每张纸的最后一列导出到一个文本文件中(所有文件都在同一个文件中,第二张纸的第一行必须在第一张纸的最后一行之后)。

事情是从一张纸到另一张纸的列数变化。最后一列的编号可以由第一行上的最后一个非空单元格给出。

我已经看过如何在一个文件中写,但我对如何迭代表和行无能为力......

欢迎任何帮助。感谢。

2 个答案:

答案 0 :(得分:5)

您可以使用以下代码连续找到最后一个单元格:

Sub LastCellInRow()
   Range("IV1").End(xlToLeft).Select
End Sub

您可以使用Worksheets集合迭代每个Worsheet:

Sub LoopThroughSheets() 
    Dim ws As Worksheet 
    For Each ws In ActiveWorkbook.Worksheets 

         '** Perform code here **

    Next ws 
End Sub 

答案 1 :(得分:3)

这有效:

Dim r As Range
Dim s As Worksheet
Dim wbSource As Workbook
Dim wbDestination As Workbook
Dim lastcol As Long
Dim lastrow As Long
Dim cumrow As Long
Dim i As Long

Set wbSource = ActiveWorkbook
Set wbDestination = Workbooks.Open("C:\destination.xls")

cumrow = 0
For Each s In wbSource.Worksheets
    lastcol = s.Cells(1, s.Columns.Count).End(xlToLeft).Column
    lastrow = s.Cells(s.Rows.Count, lastcol).End(xlUp).Row
    Set r = s.Cells(1, lastcol).Resize(lastrow, 1) ' This is your column

    ' Copy it to appropriate location on destination sheet
    wbDestination.Sheets(1).Cells(cumrow + 1, 1).Resize(lastrow, 1) = r
    cumrow = cumrow + lastrow
Next s

上述内容是在视频电话会议上编写和测试的!