提问人:mRminer 提问时间:10/25/2023 最后编辑:mRminer 更新时间:10/26/2023 访问量:80
VBA:无法获取单元格的值
VBA: can't get the value of a cell
问:
Sub ExtractFeedbackStats()
Const Path As String ="C:\feedbackFiles_4analysis\"
Const fileExtension As String = "*.xlsx"
'Variables
Dim feedbackFile As Variant
Dim analysisFile As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim Cell As Range
Dim errorRange As Range
Dim lastRow As Variant
Dim counter As Integer
Dim cellValue As Variant
'Target path with extension
feedbackFile = Dir(Path & fileExtension)
'Set constants
firstRow = "2"
errorColumn = "AG"
'Loop through file error detail report, look at HARD_ERROR_CD column
Do While feedbackFile <> ""
'Open each spreadsheet in folder
Set wb = Workbooks.Open(Filename:=Path & feedbackFile)
DoEvents
'Set parameters for scanning
lastRow = Range("E2").Value + 1
'Set data range to be scanned
Set errorRange = Range(errorColumn & firstRow, errorColumn & lastRow)
'Establish counter for error code instances
counter = 0
'Loop through each row in spreadsheet
For Each Cell In errorRange.Cells
cellValue = Cell.Value
'If: GR001 Error, Then: Return Customer Code + unique_counter
If cellValue = "GR001" Then counter = 100
Next Cell
'Ensure Workbook has closed before moving on to the next line of code
wb.Close SaveChanges:=False
DoEvents
'Get next file name
feedbackFile = Dir
'Close Loop
Loop
End Sub
我在另一个模块中有一段几乎相同的代码,该代码使用相同的 FOR EACH 循环,并且可以获取所指向的单元格的值。但这个不这样做。尝试使用所有变量类型来使某些东西正常工作,但无法让 vba 识别单元格中的数据。我在这里做错了什么?成功的代码如下所示:
Sub FeedbackFilesExtract()
'Update this folder to local
Const destPath As String = "C:\feedbackFiles_4analysis\"
Const srcPath As String = "M:\Feedback\"
'Variables
Dim ws As Worksheet
Dim srcFile As Variant
Dim destFile As Variant
Dim File As Variant
Dim fileArray() As Variant
Dim dataRange As Range
Dim Cell As Range
Dim arrayCounter As Integer
Dim counter As Integer
'Set data range
Set ws = Sheets("FEEDBACK_FILE_LIST")
secondColumn = "B"
firstRow = "2"
lastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row
Set dataRange = Range(secondColumn & firstRow, secondColumn & lastRow)
'Set size of array to accomodate list
ReDim fileArray(dataRange.Cells.Count)
'Populate array with list of files from xlsm
For Each Cell In dataRange.Cells
fileArray(arrayCounter) = Cell.Value
arrayCounter = arrayCounter + 1
Next Cell
答:
0赞
Dominique
10/25/2023
#1
需要添加以下两行:
Next
Loop
评论
0赞
mRminer
10/26/2023
这些已经在上面了。循环工作正常。它不是单元格引用。
0赞
Black cat
10/26/2023
#2
要定义范围,请尝试以下操作
Set errorRange = Range(errorColumn & firstRow & ":" & errorColumn & lastRow)
评论
0赞
VBasic2008
10/26/2023
仅供参考,..Range("A1:A2") = .Range("A1", "A2") = .Range("A1", lcell) = .Range(fcell, "A2") = .Range(fcell, lcell)
0赞
Black cat
10/26/2023
@vbas 感谢您提供此特殊信息。将来我会使用它
0赞
mRminer
10/26/2023
#3
在查看了 Tim 的评论后,我意识到这就像设置活动工作表一样简单。请注意,我声明了“ws”变量,但没有使用它;或多或少是菜鸟在工作日第十个小时犯的错误。我真的很感谢你们所有人的帮助。即使提出的解决方案没有解决我的问题,仍然激励着我继续努力,所以感谢您的投入和与我一起度过难关。
评论
0赞
VBasic2008
10/26/2023
不要将不合格的对象引用“放置”在周围:、、。用工作簿限定第一个,用工作表限定其他的(最后三个也与范围一起使用):、、、。此外,请在每个模块的顶部使用。它将检测任何未声明的变量 (, ) 并检测拼写错误。Sheets(...)
Range(...)
Cells(...)
Rows(...)
Columns(...)
Set ws = wb.Sheets("FEEDBACK_FILE_LIST")
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
Set dataRange = ws.Range(secondColumn & firstRow, secondColumn & lastRow)
lastRow = ws.Range("E2").Value + 1
Option Explicit
firstRow
errorColumn
上一个:基于列表隐藏工作表
下一个:VBA:嵌套循环仅运行一次
评论
Sub
feedbackfile
Path
fileextension
Dir
errorRange
lastRow = Range("E2").Value + 1
lastRow = Range("E2").Value + 1
这是在哪张纸上,在哪个工作簿上?此时是 ActiveWorkbook,因此您的代码默认为从那里读取。你真的需要确保对 等的每个调用都符合特定工作簿中的特定工作表,否则你将永远在代码中寻找错误。如果您需要帮助,可以查看此处的指南:stackoverflow.com/questions/10714251/......wb
lastRow
Range()
Cells()