求教excel VBA编程

发布网友

我来回答

4个回答

热心网友

用循环处理,如从1到1000行

Sub demo3()

for i= 1 to 1000
If sheet1.Cells (i,9) >= 0 Then 'I为第9列
sheet1.Cells (i,10) = ""
sheet1.Cells (i,10).Interior.ColorIndex = 0
Else
sheet1.Cells (i,10) =sheet1.Cells (i,9)
sheet1.Cells (i,10).Interior.ColorIndex = 3

End If
next i
End Sub

书写代码的时候不出现代码联想是因为你前面的对象名没有用全称,如 sheet1.追问如果有2000行数据,用手动编码太繁琐了吧,如何实现“Ctrl+shift+→ ”即最后一行非空单元格的效果呢?

热心网友

用Range("A65536").End(xlUp).Row 取得数据末行行号
再用FOR循环一下
MsgBox "更新成功" 放到FOR语句外面

热心网友

下午再来看看……

热心网友

Sub demo3()
Dim i As Integer
For i = 3 To 11
    If ActiveSheet.Cells(i, 9) > 0 Then
        ActiveSheet.Cells(i, 10) = ""
        ActiveSheet.Cells(i, 10).Interior.ColorIndex = 0
    Else
        ActiveSheet.Cells(i, 10) = ActiveSheet.Cells(i, 9)
        ActiveSheet.Cells(i, 10).Interior.ColorIndex = 3
    End If
Next
    MsgBox "更新成功"
End Sub

追问这个表格我只截屏到11行,如何实现“Ctrl+shift+→ ”即最后一行非空单元格的效果呢?

追答

那就要加个判断条件

Sub demo3()
Dim i As Integer
i = 3
Do While ActiveSheet.Cells(i, 9) <> ""
    If ActiveSheet.Cells(i, 9) > 0 Then
        ActiveSheet.Cells(i, 10) = ""
        ActiveSheet.Cells(i, 10).Interior.ColorIndex = 0
    Else
        ActiveSheet.Cells(i, 10) = ActiveSheet.Cells(i, 9)
        ActiveSheet.Cells(i, 10).Interior.ColorIndex = 3
    End If
    i = i + 1
Loop
    MsgBox "更新成功"
End Sub

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com