【问题标题】:VBA export only cells with dataVBA 仅导出包含数据的单元格
【发布时间】:2017-06-07 07:52:38
【问题描述】:

我正在尝试浏览工作簿中的许多工作表,并且仅从 B 列中包含数据的单元格中导出数据。

现在导出速度非常慢,因为我选择了 B 列中的所有内容并将其写入文本文件。

我是 VBA 新手,这个宏是从在线搜索中组合起来的。

Sub Export()
Application.ScreenUpdating = False
Application.EnableEvents = False
'Remember original sheet
Set mySheet = ActiveSheet

For Each sht In ActiveWorkbook.Worksheets
    sht.Activate
    Columns("B").Select
Next sht

Dim myFile As String, cellValue As Variant, rng As Range, i As Long, j As Integer
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
myFile = fso.GetBaseName(ActiveWorkbook.Name) & ".txt"
Set rng = Selection
Open myFile For Output As #1
       For i = 1 To rng.Rows.Count
            For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
    Write #1, cellValue
Else
    Write #1, cellValue,
End If
    Next j
Next i
Close #1
'Remove extra quotes
Dim r As Range, c As Range
Dim sTemp As String
Open myFile For Output As #1
For Each r In Selection.Rows
    sTemp = ""
    For Each c In r.Cells
        sTemp = sTemp & c.Text & Chr(9)
    Next c
    'Get rid of trailing tabs
    While Right(sTemp, 1) = Chr(9)
        sTemp = Left(sTemp, Len(sTemp) - 1)
    Wend
    Print #1, sTemp
Next r
Close #1
'Return to original sheet
mySheet.Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox "Done"
End Sub

编辑:

我可以快速导出当前工作表上具有值的单元格。它不会循环遍历所有工作表。

For Each ws In ThisWorkbook.Worksheets
    Range("B12:B1746").SpecialCells(xlCellTypeConstants, xlTextValues).Select
Next ws

编辑 2:

这行得通,但我会继续努力。随意添加建议。

Sub CopyRangeFromMultiWorksheets()
'Remember original sheet
Set mySheet = ThisWorkbook.ActiveSheet
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

'Delete the sheet "RDBMergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True

'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ThisWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"

'loop through all worksheets and copy the data to the DestSh
For Each sh In ThisWorkbook.Worksheets
    'Error if not unprotected first
    'ActiveSheet.Unprotect Password:=""
    If sh.Name <> DestSh.Name Then

        'Find the last row with data on the DestSh
        Last = LastRow(DestSh)

        'Fill in the range that you want to copy
        Set CopyRng = sh.Range("B12:B1746").SpecialCells(xlCellTypeConstants, xlTextValues)

        'Test if there enough rows in the DestSh to copy all the data
        If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
            MsgBox "There are not enough rows in the Destsh"
            GoTo ExitTheSub
        End If

        'This example copies values/formats, if you only want to copy the
        'values or want to copy everything look at the example below this macro
        CopyRng.Copy
        With DestSh.Cells(Last + 1, "A")
            .PasteSpecial xlPasteValues
            .PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
        End With

        'Optional: This will copy the sheet name in the H column
        DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name

    End If
Next

ExitTheSub:

Application.Goto DestSh.Cells(1)

'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit

'Copy to txt
Dim iCntr
Dim myFile As String
Dim strFile_Path As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
myFile = fso.GetBaseName(ActiveWorkbook.Name) & ".txt"
Open myFile For Output As #1
For iCntr = 1 To LastRow(DestSh)
Print #1, Range("A" & iCntr)
Next iCntr
Close #1
'Remove helper sheet without alert
Application.DisplayAlerts = False
ThisWorkbook.Worksheets("RDBMergeSheet").Delete
Application.DisplayAlerts = True
'Return to original sheet
mySheet.Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox "Done"
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
                        After:=sh.Range("A1"), _
                        Lookat:=xlPart, _
                        LookIn:=xlFormulas, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlPrevious, _
                        MatchCase:=False).Row
On Error GoTo 0
End Function

【问题讨论】:

  • 这会变慢的主要原因是因为您正在迭代一系列单元格和整个 B 列。将范围限制为使用范围会快得多,然后将数据复制到Variant Array 并对其进行迭代。在SO上搜索那个词,这里有很多例子
  • .AdvancedFilter 怎么样?它也会加快速度。
  • 另外,你的第一个For Each sht 循环实际上并没有做任何事情。您只需遍历每张工作表,然后选择 B 列。此外,您可以使用 Range([range]).SpecialCells(xlCellTypeConstants) 仅抓取包含数据的单元格。
  • 我可以快速导出当前工作表上带有值的单元格,但它不会选择下一个工作表。
  • 已解决,但我会继续努力。随时提出建议。

标签: vba excel


【解决方案1】:

您在这里遇到了一些多步骤问题。我将尝试从高层次上介绍最大的项目,以使您更容易依次解决(或提出后续问题)每个单独的问题。

对于循环工作表,您可能需要这样的东西:

For Each ws In ThisWorkbook.Worksheets

    ' Insert your main actions within here, instead of after here

Next ws

现在,您的第一个循环实际上并没有做任何事情。它只是不必要地“触摸”每张纸,然后继续执行其余代码。

您很可能会想要执行您想要执行的每个操作并将它们放入循环中。

此外,当您打开多本书时,请使用 ThisWorkbook 而不是 ActiveWorkbook 以避免出现极端情况。

因为您遇到了速度问题,所以最好在复制列时尽量避免使用SelectActivate。试试这样的:

...
Const RANGE_BASE As String = "B1:B"
Dim rangeToImport As String
Dim Items() As Variant

rangeToImport = RANGE_BASE & CStr(ReturnLastUsedRow(ws:=ws))
Items = ws.Range(rangeToImport)
...

Private Function ReturnLastUsedRow(ByVal ws As Worksheet) As Long

    Const CUTOFF_ROW As Long = 1000000
    Const SELECTED_COLUMN As String = "B"

    ReturnLastUsedRow = ws.Cells(CUTOFF_ROW, SELECTED_COLUMN).End(xlUp).Row

End Function

以上对列进行了硬编码(而不是仅仅依赖于活动的内容)。然后,它将给定列的内容保存到一个数组中,供您以后使用。

提供了一个单独的辅助函数来帮助确定范围的最大长度。这是为了确保您不会遍历每一行,而只是遍历其中包含内容的行。

我不确定您是需要单独导出列,还是需要将它们作为一个整体导出?如果是前者,那么您应该能够在 For 循环的每次迭代中导出。如果是后者,您可能希望将数组转换为多维数组,并在循环的每次迭代中增加其大小。

你已经清理了这部分,你应该对出口很好。这将是遍历数组而不是遍历行的问题,这应该会加快速度。

【讨论】:

  • “另外,当您打开多本书籍时,请使用 ThisWorkbook 而不是 ActiveWorkbook 以避免出现极端情况。” - 使用ActiveWorkbook 允许用户在一个工作簿中拥有宏,并对另一个(希望没有宏)工作簿采取行动。这通常是一个很好的做法 - 我目前工作的领域经常出现问题,因为人们在他们的工作簿的每个版本中都保存了宏,现在一半的工作簿的宏已经过时了,没有人知道哪个版本是正确版本的代码 - 如果只保留一个启用宏的工作簿,所有这些问题都会消失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
  • 2019-03-08
  • 2014-03-29
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多