vba - 无法使用excel vba在foreach循环中获取单元格值

标签 vba excel excel-2010

enter image description here

你好,
我已附上工作表图像。
我的要求是:
我想获取与特定组织名称匹配的组织的所有“G”列值(例如:360 评估)。

在 G 列的第一次循环后我得到空值

Sub UsageWeekTrend()

    Dim customerName As String
    Dim sheetName As String
    Dim dataFound As Boolean

    Dim selectedCell As Range
    Dim rowNumber As Integer
    Dim weekMinutes As Double

    Dim trendsFile As Workbook
    Dim trendsSheet As Worksheet

    On Error GoTo errorHandling

    sheetName = ActiveSheet.Name
    customerName = ActiveSheet.Range("A" & (ActiveCell.row)).Value
    dataFound = False

    For Each selectedCell In ActiveSheet.Range("A1:A1000")
     If UCase(selectedCell.Value) = UCase(customerName) Then
        weekMinutes = ActiveSheet.Range("G" & selectedCell.row).Value
        Debug.Print weekMinutes
        Debug.Print "G" & selectedCell.row

           If dataFound = False Then

                  If trendsFile Is Nothing Then
                     Set trendsFile = Workbooks.Add()
                     trendsFile.Activate
                     Set trendsSheet = trendsFile.ActiveSheet

                    Else
                       ' add a new sheet to the trends workbook
                        trendsFile.Activate
                        Set trendsSheet = Sheets.Add

                    End If

                dataFound = True
                rowNumber = 1
                trendsSheet.Name = Left(customerName, 10) + " " + Format(Date, "MMDD")
                trendsSheet.Cells(rowNumber, 1) = "Users"
                trendsSheet.Cells(rowNumber, 2) = "Minutes"
                rowNumber = rowNumber + 1

            End If

             ' if a sheet has been created, then we have at least one non-zero value so add data

             If dataFound = True Then
                trendsSheet.Cells(rowNumber, 1) = customerName
                trendsSheet.Cells(rowNumber, 2) = weekMinutes
                rowNumber = rowNumber + 1
             End If
       End If

  Next selectedCell

    ' if we have data, create the chart

    If dataFound = True Then

        ' make sure the trends sheet is active for chart insertion

        trendsSheet.Activate

        Dim chtChart As ChartObject
        Dim chartName As String
        Dim endRange As String

        ' define the end of the range for the chart

        endRange = "C" & CStr(rowNumber - 1)

        ' add chart to current sheet

        Set chtChart = ActiveSheet.ChartObjects.Add(Left:=200, Top:=200, Width:=900, Height:=400)
        chtChart.Activate
        ActiveChart.ChartType = xlLineStacked
        ActiveChart.SetSourceData Source:=trendsSheet.Range("A2", endRange)
        ActiveChart.HasTitle = True
        ActiveChart.ChartTitle.Text = customerName
        ActiveChart.ApplyLayout (5)


    Else
        MsgBox ("No usage data found for customer " + customerName)
    End If

    Exit Sub

errorHandling:
    MsgBox (Err.Description)


End Sub

最佳答案

当您运行此行时:

trendsFile.Activate

您更改 Activesheet ,所以第二次在循环中你再次查看事件表
weekMinutes = ActiveSheet.Range("G" & selectedCell.row).Value

但事件表已更改。我会改变那些 Activesheet调用您在顶部分配的工作表对象。

对于那些刚接触 VBA 编程的人来说,这总是一本好书:How to avoid using Select in Excel VBA macros

关于vba - 无法使用excel vba在foreach循环中获取单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076268/

相关文章:

vba - 如何使用单元格内的十六进制颜色值突出显示单元格?

excel - 如何使用 VBA 将 ShapeStyle 应用于 Excel 中图表的特定系列?

Excel - 合并具有共同值的行并将差异连接在一列中

excel - 自动保存附件 Outlook 转换为 csv

excel - 如何将渐变颜色分配给 Excel 列?

excel - 如何选择除标题以外的整个列

hyperlink - 如何在没有宏/vba 的情况下创建从 Word 到 Excel 特定单元格的超链接?

vba - Excel VBA - 比较同一列中的行

ms-access - 在报告中显示 PDF

excel - 将不同工作簿中的范围复制到一张最终目标工作表中