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

在这里输入图像描述

嗨,我已经附上了表格图像。 我的要求是:我想获得与特定组织名称匹配的组织的所有“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 ,所以第二次在循环你再次看activesheet

 weekMinutes = ActiveSheet.Range("G" & selectedCell.row).Value 

但activesheet已经改变。 我会将这些Activesheet调用更改为您在顶部分配的工作表对象。

对于那些新的VBA编程,这总是一个很好的解读: 如何避免在Excel VBAmacros中使用Select

问题是你正在使用ActiveSheet ,并且活动工作表正在你的代码中被改变。

一旦执行了trendsFile.Activate ,这两个引用将具有新的含义ActiveSheet.Range("A1:A1000")ActiveSheet.Range("G" & selectedCell.row).Value

您已经为Trends文件创build了工作簿和工作表variables,并使用这些variables,还需要为“源”工作表创build一个工作表variables(不确定如何引用它)。

另外,我会关心这段代码:

 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 

我相信你会每次都通过循环添加一个新表。

尝试这样的事情:

 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 Dim SourceSheet as worksheet 'this is the place where you start, call it what you will On Error GoTo errorHandling set SourceSheet = activesheet 'this will now always be THIS sheet, and won't change sheetName = SourceSheet.Name customerName = SourceSheet.Range("A" & (ActiveCell.row)).Value dataFound = False For Each selectedCell In SourceSheet.Range("A1:A1000") If UCase(selectedCell.Value) = UCase(customerName) Then weekMinutes = SourceSheet.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 - never needed Set trendsSheet = trendsFile.Sheets("Sheet1") 'use the first sheet, since you just created a brand new workbook Else ' add a new sheet to the trends workbook 'trendsFile.Activate -- you never need this when you're working with an object instead of "Active" 'you'll find that this line will add a new sheet every time you execute the loop 'once you've created your "trendsFile" workbook. you'll need to do some tweaking here 'to prevent you getting one loop worth of data on each sheet 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 'The rest of your routine here... End Sub