在excel中,用vba编程,写了代码没有运行按钮点不了
private sub commandbutton1_click()dim i as integerdim j as integeri = 6with sheet1do while cells(i, 1) > 135 or cells(i, 1) < 120for j = i + 1 to i + 7 step 1if cells(j, 1) > cells(i, 1) thentextbox1 = i - 5textbox2 = cells(i - 5, 1)end ifnext j??i = i + 1 if i > 1000 then exit doloopend withend sub请见上面三处修改:i = 6 ,否则cells(i-5,1)中会出现第0行,报错;两个do 合并;do 中增加 i = i + 1否则相当于没循环;并增加一判断语句,否则不退出循环。其实代码应该写为:private sub commandbutton1_click()dim i as integerdim j as integerwith sheet1for i = 6 to range("a65000").end(xlup).rowdo while cells(i, 1) > 135 or cells(i, 1) < 120for j = i + 1 to i + 7 step 1if cells(j, 1) > cells(i, 1) thenTeXtbox1 = i - 5textbox2 = cells(i - 5, 1)end iFnext jnext iend withend sub