写一个控制Excle自动化的VBA
时间: 2024-05-25 17:19:22 浏览: 174
Sub AutoAutomation()
'定义变量
Dim wb As Workbook
Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
'打开工作簿
Set wb = Workbooks.Open("C:\Users\Administrator\Desktop\Test.xlsx")
Set ws = wb.Sheets("Sheet1")
'查找最后一行
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
'在最后一行插入新数据
ws.Cells(lastrow + 1, 1).Value = "New Data"
ws.Cells(lastrow + 1, 2).Value = "123"
ws.Cells(lastrow + 1, 3).Value = Date
'循环遍历表格
For i = 2 To lastrow
If ws.Cells(i, 2).Value > 100 Then
ws.Cells(i, 4).Value = "High"
Else
ws.Cells(i, 4).Value = "Low"
End If
Next i
'保存并关闭工作簿
wb.Save
wb.Close
End Sub
阅读全文