热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

IhaveanExcelspreadsheetwithmanytabs.Idliketoexportthelastcolumnofeachsheetinate

I have an Excel spreadsheet with many tabs. I'd like to export the last column of each sheet in a text file (all in the same file, the first line of the second sheet must go just after the last line of the fist sheet).

我有一个包含许多选项卡的Excel电子表格。我想在文本文件中导出每个工作表的最后一列(所有在同一个文件中,第二个工作表的第一行必须在第一个工作表的最后一行之后)。

The thing is the number of columns changes from one sheet to another. The number of the last column can be given by the last non empty cell on the first row.

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

I've seen how to write in a file, but I'm clueless on how to iterate over sheets and rows...

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

Any help is welcome. Thanks.

欢迎任何帮助。谢谢。

2 个解决方案

#1


5  

You can find the last cell in a row using this code:

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

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

You can iterate through every Worsheet using the Worksheets collection:

您可以使用Worksheets集合遍历每个Worsheet:

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

         '** Perform code here **

    Next ws 
End Sub 

#2


3  

This works:

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

The above was written and tested while on a video conference call!

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


推荐阅读
author-avatar
点燃半吱煙
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有