【问题标题】:Clearing contents of a cell based on the value of another cell within the same row, based on header references根据标题引用,根据同一行中另一个单元格的值清除单元格的内容
【发布时间】:2019-04-29 23:05:47
【问题描述】:

我是 VBA 领域的新手,无法完全理解我想要创建的这个宏。

基本上,我有一个月度数据集,但数据不完善。我经常需要根据另一个单元格的值清除一个单元格的多余数据值。

复杂的部分是数据将每周更换一次,唯一静态的就是包含的列标题。

例如,A 到 E 列有标题 Company1、Company2、Company3 等。 Q 到 U 列有标题 Product1、Product2、Product3 等。

产品列将包含公司名称作为值(通常不止一个,以逗号分隔),如果任何产品列中均未出现公司名称,则在同一行的单元格中显示该名称公司的栏目应该被清除。

因此,如果 Q4:U4 不包含“Product1”作为值,则应清除 A4(产品 1 列,第 4 行)处的值。

任何有关如何解决此问题的见解将不胜感激!

编辑

示例数据的屏幕截图:

Example 2

【问题讨论】:

  • 我会循环遍历数据行,并为每一行循环遍历要检查的列,并使用If 语句来检查这些单元格的内容。根据您的If 检查,清除该行中的其他单元格(或不清除),然后移至下一行。应该不会太难。
  • 你想对新旧数据做什么?你不关心删除/改变结构吗?您可以将 A 到 E 列中的数据读入名为 company 的数组,将 Q 到 U 列中的数据读入名为 products 的数组,然后将它们进行比较以创建一个(或两个干净的)数组以粘贴回原始工作表或放入另一张纸中。
  • 你能添加一些你的数据集的截图吗?只是为了让我们看清楚?
  • 为了更清晰,我添加了一个屏幕截图。以屏幕截图为例,如果列 Q 到 T 中的行在任何列中包含其各自的公司作为值,则列 A 到 H 应该只有值。如果它们不存在于任何类别列中,则该值将从该公司的行中删除。例如:Company8(H 列)在第 7 行中没有任何值,但仍有值。在这种情况下,应清除第 7 行的 Company8 的值。 A 到 H 列的数据被复制粘贴到单独的数据透视表中。

标签: excel vba


【解决方案1】:

试试这个。在 VBA 编辑器中创建一个新模块并复制下面的代码...

Public Sub ProcessData()
    Dim objCompanyRange As Range, objProductRange As Range, objCompanyCell As Range
    Dim strCompany As String, objThisProductRange As Range, rngFrom As Range
    Dim rngTo As Range, objFindResult As Range, lngLastRow As Long

    On Error Resume Next

    ' Get the range for the company data.
    Set objCompanyRange = Application.InputBox("Please select the COMPANY data range, including headers ...", "Company Data", , , , , , 8)
    If Err.Description <> "" Then Exit Sub

    ' Get the range for the product data.
    Set objProductRange = Application.InputBox("Please select the PRODUCT data range, including headers ...", "Product Data", , , , , , 8)
    If Err.Description <> "" Then Exit Sub

    On Error GoTo 0

    For Each objCompanyCell In objCompanyRange
        ' We want the headers in the range but want to skip processing the first row.
        If objCompanyCell.Row > objCompanyRange.Cells(1, 1).Row Then
            ' This is the only contentious line for me.  If your headers are specified as you had in your
            ' example, i.e. "Group: Company1" then the below will work.  If that was a mocked example that
            ' was not 100% accurate, the below line will need to change.  It is currently splitting the header
            ' by a colon and only storing the right hand side as the company.
            strCompany = Trim(Split(objCompanyRange.Cells(1, objCompanyCell.Column).Text, ":")(1))

            ' Only reset objThisProductRange if the row has changed, otherwise we use the same set of
            ' products we used last time.
            If objCompanyCell.Row <> lngLastRow Then
                ' Determine the range for the product data given the current row being processed
                With objProductRange.Worksheet
                    Set rngFrom = .Range(.Cells(objCompanyCell.Row, objProductRange.Cells(1, 1).Column).Address)
                    Set rngTo = rngFrom.Offset(0, objProductRange.Columns.Count - 1)
                End With

                Set objThisProductRange = Range(rngFrom.Address & ":" & rngTo.Address)
            End If

            ' Find the company name within the current row of Product data.
            Set objFindResult = objThisProductRange.Find(strCompany, MatchCase:=False)

            ' Clear the cell if nothing was found.
            If objFindResult Is Nothing Then
                objCompanyCell.ClearContents
            End If
        End If

        lngLastRow = objCompanyCell.Row
    Next
End Sub

...现在观看下面的动画 GIF 以了解您如何启动它以及生成的输出。

如果每次选择数据集都会给您带来麻烦,那么您可以随意对其进行硬编码或使用您自己的确定方法。这是最简单的方法,因为我不知道你可能想怎么做。

希望这是您所追求的。如果您有任何问题,请务必阅读代码中的 cmets。

【讨论】:

  • 考虑一下,如果您的公司名称交叉,它可能需要调整,例如如果您有 Company1 和 Company10,它将在 Company10 名称中找到 Company1。我有它可能没问题,但需要考虑一下。
  • 嘿皮肤,感谢您对此进行调查。我在假数据上运行宏,它运行良好,但是当我在实时数据上运行它时,它删除了所有“组:公司”数据,而不仅仅是那些不匹配的数据。我试图解释为什么会发生这种情况。实时数据与我发布的假数据并没有太大区别,公司标题和类别标题都包含冒号,类别列中的公司名称完全用逗号分隔同样的方式……唯一的区别是公司名称(和公司数量)。
  • @dsaurr 你知道如何在 VBA 中调试吗?我会首先考虑这样做。除非我可以使用您的确切数据集,否则我很难解决。无论如何共享实际的工作簿?
  • 很遗憾,我无法发布实时数据。我尝试了一些方法来解决问题,并发现如果我将数据复制粘贴到新工作表中,然后运行宏,它可以完美运行。所以有一些关于所有列如何协同工作的东西打破了宏。我添加了一个消息框来显示 strCompany 并注意到它包含了一堆不应该包含的标题...为了进一步清楚,我将在今天晚些时候尝试发布另一个示例。
  • 添加了示例 2 以供进一步参考。使用该示例,我认为宏正在获取“不相关”类别数据,而不是我指定的“组:”数据,除非我将数据复制粘贴到单独的工作表中,并排除所有不相关的列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多