运行我的VBA代码时遇到了一个小问题。我试图检索并检查excel中指定列中的信息。它将运行16次检索信息,直到17次它甚至没有给我工作簿的列中的什么。能帮助或指导我解决这个问题吗?
Dim CheckingWhatsInCell As String
Dim i As Integer
Dim j As Integer
Dim ToWorkbook As Workbook
ThisWorkbook.Activate
For i = 1 To 20
CheckingWhatsInCell = Trim(Range("K" & i).Value)
If CheckingWhatsInCell = "Albuquerque NM" Then
Set ToWorkbook = Workbooks.Open("C:\Users\mgonza-c\Documents\TerritoryAlbuquerqueNM.xlsx")
For j = 1 To 139
ToWorkbook.Worksheets("Sheet1").Cells(i, j) = ThisWorkbook.Sheets("Sheet2").Cells(i, j).Value
Next j
End If
Next i
ToWorkbook.Save
ToWorkbook.Close
End Sub
谢谢!
答案 0 :(得分:0)
CheckingWhatsInCell = Trim(Range("K" & i).Value)
此代码将查看ThisWorkbook,它是Active,以及最后一个活动的Sheet。也许它正在寻找错误的表格。当其他工作簿打开时,情况也会发生变化,使其处于活动状态。
然后,正如Julien所说,K17中可能存在奇数值。
另请注意,您尝试保存并关闭ToWorkbook,即使它可能永远不会分配给工作簿。