2011-08-07 45 views
1

我试图将列表中的项目添加到Excel工作表中的某些行中。 我试图做这样说:如何将列表中的项目添加到工作表中的单元格

Dim Rand As Long 
Dim ws As Worksheet 
Set ws = Worksheets("Necmontage") 
Rand = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 

Range(ws.Cells(Rand, 1), ws.Cells(Rand + necesar.ListCount - 1, 1)).Merge 
ws.Cells(Rand, 1) = "K" 

Range(ws.Cells(Rand, 2), ws.Cells(Rand + necesar.ListCount - 1, 2)).Merge 
ws.Cells(Rand, 2) = "Montage" 

Range(ws.Cells(Rand, 3), ws.Cells(Rand + necesar.ListCount - 1, 3)).Merge 
ws.Cells(Rand, 3) = comanda.Caption 

Dim i As Integer 
i = 0 
Do While i = necesar.ListCount - 1 
    ws.Cells(Rand + i, 4) = necesar.List(i, 0) 
    i = i + 1 
Loop 
End Sub 

它增加了所有我想要的值,除了列表中的值(其中我这样做,虽然环)。我不知道为什么,但它并不考虑价值。有关这个问题的任何想法?

回答

2

您是不是要找你的代码:

Do While i <= necesar.ListCount - 1 'instead of = 
    ws.Cells(Rand + i, 4) = necesar.List(i, 0) 
    i = i + 1 
Loop 

顺便说一句,你可以通过戴上Do While行设置一个断点,如果程序去的地方,你希望它在调试模式下看到。

+0

是的,非常感谢......我改变了这个同时用一个For和它的工作......我不知道是什么问题......好吧......它没有给我任何错误,这就是为什么我没有使用调试模式。 –

+0

很高兴终于工作。即使你没有收到错误(只要你事实上没有得到预期的结果),调试模式也很有用(特别是在vba中,你可以一步一步地获取或设置变量值)。 – JMax

相关问题