繁体   English   中英

Debug.Print 不会在 VBA 即时窗口中打印任何内容

[英]Debug.Print won't print anything in VBA Immediate Window

此代码创建一个范围之外的数组。 但是,当我尝试使用 Debug.Print 查看数组是否实际保存了元素时,立即窗口中没有显示任何内容,但它只显示空格。 没有错误,只是不显示任何内容。

然而,这只发生在代码的那一部分,第一行 Debug.Print 工作正常,只有第二个 Debug.Print 行不工作。 我确实在列中有数据。

Dim myArray() As Variant 
Dim iCountLI As Long 
Dim iElementLI As Long 

If IsEmpty(Range("B3").Value) = True Then
      ReDim myArray(0, 0)
Else
      iCountLI = Sheets("Sheet1").Range("B3").End(xlDown).Row
      iCountLI = (Range("B3").End(xlDown).Row) - 2 
      Debug.Print iCountLI
      ReDim myArray(iCountLI) 

      For iElementLI = 1 To iCountLI 
      myArray(iElementLI - 1) = Cells(iElementLI + 2, 2).Value 
      Debug.Print myArray(iElementLI)
      Next iElementLI
      
End If

任何帮助将不胜感激,在此先感谢您!

您正在评估myArray(iElementLI -1)并打印myArray(iElementLI ),它仍然是空的。

直接问题: ReDim myArray(iCountLI)创建一个值数组。 在 For 循环中, myArray(iElementLI - 1) = Cells(iElementLI + 2, 2).Value会覆盖第一个、第二个等值,但您的Debug.Print myArray(iElementLI)正在打印第二个、第三个等。价值观。 当然,这些还没有被覆盖,所以它们仍然是空的。 那么,最简​​单的解决方法是使用Debug.Print myArray(iElementLI - 1)

更一般地说,我认为您可能误解了ReDim myArray(iCountLI)的含义。 假设我们在B3:B7中有值。 这将导致代码中出现ReDim myArray(5) ,但这是一个包含6 个空值的数组,位于位置0 ,1,2,3,4,5。 这意味着您将在循环之后在数组中保留一个空值,这可能不是您想要的。

这是一个带有一些评论的建议重写:

Sub FillArray()

Dim myArray() As Variant
Dim iCountLI As Long
Dim iElementLI As Long

'let's assume: B3:B7 with values: 1,2,3,4,5

If IsEmpty(Range("B3").Value) = True Then
    ReDim myArray(0, 0)
Else
    'iCountLI = Sheets("Sheet1").Range("B3").End(xlDown).Row
    'this line serves no purpose: you are immediately reassigning the value in the next line
    
    'iCountLI = (Range("B3").End(xlDown).Row) - 2   'This would be 5,
                                                    'but myArray(5) would have SIX elements
    iCountLI = (Range("B3").End(xlDown).Row) - 3
    
    Debug.Print iCountLI '4
    
    ReDim myArray(iCountLI)
    
    'For iElementLI = 1 To iCountLI
    For iElementLI = 0 To iCountLI '0 to 4
    
    myArray(iElementLI) = Cells(iElementLI + 3, 2).Value    'starting at 0, so "+3", not "+2"
    'myArray(iElementLI - 1) = Cells(iElementLI + 2, 2).Value
    
    Debug.Print myArray(iElementLI) 'in succession: 1,2,3,4,5
    Next iElementLI
      
End If

End Sub

最后,值得指出的是,您实际上不需要 For 循环来使用范围内的值填充数组。 您可以使用如下内容:

Sub FillArrayAlt()

Dim myArray() As Variant
Dim iCountLI As Long
Dim iElementLI As Long

Dim myRange As Range

'let's assume: B3:B7 with values: 1,2,3,4,5

If IsEmpty(Range("B3").Value) = True Then
    ReDim myArray(0, 0)
Else
    
    Set myRange = Range("B3:" & Range("B3").End(xlDown).Address)
    myArray = Application.Transpose(myRange.Value)
    
    'N.B. Confusingly, used in this way, your array WILL start at 1!
    
    For i = LBound(myArray) To UBound(myArray)
        Debug.Print i; ":"; myArray(i)
        
'        1 : 1
'        2 : 2
'        3 : 3
'        4 : 4
'        5 : 5
        
    Next i
      
End If

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM