【发布时间】:2014-08-20 13:57:42
【问题描述】:
我无法解决以下情况:我有一个循环复制名为“the_collector”的工作表中的值。同时,所有复制的值都是已经存在的工作表名称。所以第一列中所有复制的值都是工作表名。让我们转向问题:在第二列中,我想知道工作表内容的长度。我试过这个循环:
1. Sub Knop1_Klikken()
2. Sheets.Add.Name = "The_collector"
3. Sheets("The_collector").Move after:=Sheets("Blad2")
4. For Count = 1 To 25
5. snames = Sheets("Blad2").Cells(Count, 1).Value
6. Sheets("The_collector").Cells(1, 1).Value = "snames"
7. Sheets("The_collector").Cells(Count, 1).Value = snames
8. countlastuntillrows = Sheets(Sheets(snames)).Cells.SpecialCells(xlCellTypeLastCell).Row
9. Sheets("The_collector").Cells(Count, 2).Value = countlastuntillrows
10. Next Count
11.End Sub
当然,当我离开第 8 行和第 9 行时,我会在工作表中获得工作表名称列表。到目前为止,一切都很好。当我添加这两行时,我什么也没得到。我认为第八行有问题:countlastuntillrows 变量。执行步骤检查时出现错误 13。它指向第八行,我认为它指的是Sheets(snames)。我查看了帮助,我得到了这个:http://msdn.microsoft.com/en-us/library/office/jj535012(v=office.15).aspx
IamDranger:此代码有效(countlastuntillrows = Sheets(snames).Cells.SpecialCells(xlCellTypeLastCell).Row)但我仍然收到错误 9:http://msdn.microsoft.com/en-us/library/office/jj543427(v=office.15).aspx
有关错误的更多详细信息:
http://msdn.microsoft.com/en-us/library/office/gg264179(v=office.15).aspx
我通过运行以下代码得到此代码:
Sub Knop1_Klikken()
Sheets.Add.Name = "The_collector"
Sheets("The_collector").Move after:=Sheets("Blad2")
For Count = 1 To 25
snames = Sheets("Blad2").Cells(Count, 1).Value
Sheets("The_collector").Cells(1, 1).Value = "snames"
Sheets("The_collector").Cells(Count, 1).Value = snames
validName = False
For Each ws In Worksheets
If ws.Name = snames Then
validName = True
Exit For
End If
Next ws
If validName = False Then
Debug.Print "Invalid worksheet Name " & snames
End If
countlastuntillrows = Sheets(snames).Cells.SpecialCells(xlCellTypeLastCell).Row
'countlastuntillrows = Sheets(Sheets(snames)).Cells.SpecialCells(xlCellTypeLastCell).Row
Sheets("The_collector").Cells(Count, 2).Value = countlastuntillrows
Next Count
End Sub
【问题讨论】:
-
您忘记了这一行中 snames 变量前面的 s countlastuntillrows = Sheets(Sheets(names)).Cells.SpecialCells(xlCellTypeLastCell).row
-
哦,我明白了,谢谢。仍然没有解决我的问题。
-
您能否详细说明问题的实质是什么?
标签: excel for-loop office-2013 vba