excel的VBA问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 02:36:42
我想要在excel里实现:
处理第一列中的值:如果是纯数字(如: 0140384)则在前加单引号;如果别的(如: 134hkh、空值)就保持不变。
如何写宏?请大虾赐教!
另外想在关闭excel的时候自动运行这个宏,能否实现呢?

直接变格式没用,我要的就是上面这个要求。请vba高手来赐教,谢谢

按Alt+F11,菜单:插入>模块
写入下面的

Private Sub Auto_Close()
Dim i, r As Integer
i = 1
r = [A65536].End(xlUp).Row
For i = 1 To r
If IsNumeric(Cells(i, 1).Value) = True Then
Cells(i, 1).Value = "'" & Cells(i, 1)
End If
Next i
End Sub

按楼主的要求
Sub bb()
For Each cell In [a:a].SpecialCells(2, 23)
If IsNumeric(cell.Value) Then
cell.FormulaR1C1 = "'" & cell.Value
Else
cell.Value = cell.Value
End If
Next
End Sub
‘在关闭时执行这个宏
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call bb
End Sub

还有一个不是楼主要求的,不过也一并列出,是工作表的改变事件
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 And Target.Column = 1 Then
If IsNumeric(Target.Value) Then
Target.FormulaR1C1 = "'" & Target.Value
Else
Target.Value = Target.Value
End If
End If
End Sub
楼主自己选一个吧

<