【问题标题】:VBA excel macro to parse blocks of data in excelVBA excel宏来解析excel中的数据块
【发布时间】:2016-06-16 10:23:58
【问题描述】:

我是 VBA for excel 的新手(就像几个小时前的新手),并不是真正的程序员,所以请耐心等待。

我有一个 Excel 数据集,全部在一个列(A 列)中,其结构如下:

Data
Data
Data

Data
Data
Data

Data
Data
Data
Data
Data

Data
Data

也就是说,数据块是由空白行分隔的,但不是固定间隔。我正在尝试编写一个宏,它将通过文件和组(特定的 excel 命令)这些数据块一起。到目前为止,我有这个:

Set firstCell = Worksheets("627").Range("A1")
Set currentCell = Worksheets("627").Range("A1")

Do While Not IsEmpty(firstCell)
    Set firstCell = currentCell

    Do While Not IsEmpty(currentCell)

        Set nextCell = currentCell.Offset(1, 0)

        If IsEmpty(nextCell) Then
        Range("firstCell:currentCell").Select
        Selection.Rows.Group
        Set firstCell = nextCell.Offset(1, 0)

        Else
            Set currentCell = nextCell

        End If


    Loop

Loop

我有点卡住了,移动到下一个数据块并启动的逻辑特别麻烦。

任何帮助将不胜感激!

【问题讨论】:

  • 组只影响数据透视表数据。这是数据透视表吗?
  • 我不确定?我确实知道我可以手动浏览和分组数据块......
  • 是的,我认为我使用的是不同的版本或其他东西。组帮助指定数据透视表,但其他页面不限制它。
  • 他说的是大纲分组,不是pt分组
  • @Davigor 已经发布了四个答案。如果这些答案中的任何一个解决了您的问题,那么如果您能mark this answer as the "accepted" answer 将不胜感激。如果它们中的任何一个帮助您解决了问题,请随意对其他答案进行投票。通过接受答案,您表明您的问题已解决,并且不需要其他用户的更多答案/关注。此外,它还表示您对所提供的帮助表示感谢。

标签: vba excel


【解决方案1】:

这样的事情怎么样:

Option Explicit

Public Sub tmpTest()

Dim i As Long
Dim lngLastRow As Long

With ThisWorkbook.Worksheets(1)
    lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = lngLastRow To 1 Step -1
        If .Cells(i, 1).Value2 = vbNullString Then
            .Range(.Cells(i + 1, 1), .Cells(lngLastRow, 1)).EntireRow.Group
            lngLastRow = i - 1
        End If
    Next i
    .Range(.Cells(1, 1), .Cells(lngLastRow, 1)).EntireRow.Group
End With

End Sub

【讨论】:

  • 这很快就奏效了...非常感谢!现在我只需要弄清楚它是如何工作的......
【解决方案2】:

给你。您只需要在您的范围内提取地址,而不是尝试引用该对象。您还需要重置 if 语句中的当前单元格和第一个单元格。

Sub test()
Set firstCell = Worksheets("test2").Range("A1")
Set currentcell = Worksheets("test2").Range("A1")

Do While Not IsEmpty(firstCell)
    Set firstCell = currentcell

    Do While Not IsEmpty(currentcell)

        Set nextcell = currentcell.Offset(1, 0)
        If IsEmpty(nextcell) Then
          Range(firstCell.Address, currentcell.Address).Select
          Selection.Rows.group
          Set currentcell = nextcell.Offset(1, 0)
          Set firstCell = nextcell.Offset(1, 0)

        Else
            Set currentcell = nextcell

        End If


    Loop

Loop

End Sub

【讨论】:

    【解决方案3】:

    首先,你的代码出错了

    Range("firstCell:currentCell").Select
    

    您正在尝试选择名为“firstCell:currentCell”的范围,而不是

    选择从第一个单元格到当前单元格的范围

    你应该把它改成

    .Range(firstCell,currentCell).select
    

    尝试使用下面的代码,看看它是否符合您的要求

    Dim GROUP_LAST_CELL As Range
    
    With Worksheets("627")
    
        LAST_ROW = .Range("A" & Rows.Count).End(xlUp).Row
    
        I = 1
    
        While I <= LAST_ROW
            Set GROUP_LAST_CELL = .Cells(I, 1).End(xlDown)
            .Range(.Cells(I, 1), GROUP_LAST_CELL).Rows.Group
            I = GROUP_LAST_CELL.Row + 2
        Wend
    
    End With
    

    【讨论】:

      【解决方案4】:

      根据我对问题的理解,我认为您想要做的是遍历特定列中的所有元素,跳过所有空白。

      您可以这样做

      • 计算列的最后一行
      • 从第一行计数循环到计算的 lastRow 计数
      • 在循环中应用条件以仅打印非空单元格

      代码块

      Sub test()
      
      Dim j As Long, lastRow As Long
      
      lastRow = Cells(Rows.Count, "A").End(xlUp).Row
      
      For j = 1 To lastRow
      
          If Cells(j, "A").Value <> "" Then
              MsgBox (Cells(j, "A").Value)
      
           End If
      Next j
      
      End Sub
      

      我希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-14
        • 1970-01-01
        • 2018-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-01
        相关资源
        最近更新 更多