根据值插入行该示例可在某一列中搜索某个值,当找到该值时,就插入一个空行。此程序可在 B 列中搜索值“1”,当找到该值时,就插入一个空行。 Sub InsertRow() Dim Rng As Range Dim findstring As String 'Change the search string to suit your needs. findstring = "1" 'Change the range to suit your needs. Set Rng = Range("B:B").Find(What:=findstring, LookAt:=xlWhole) While Not (Rng Is Nothing) Rng.EntireRow.Insert Set Rng = Range("B" & Rng.Row + 1 & ":B" & Rows.Count) _ .Find(What:=findstring, LookAt:=xlWhole) Wend End Sub 将文本转换为电子邮件地址以下代码可循环访问一列范围数据,并将每个条目转换为一个电子邮件地址。 Sub convertToEmail() Dim convertRng As Range 'Change the range to suit your need. Set convertRng = Range("B13:B16") Dim rng As Range For Each rng In convertRng If rng.Value <> "" Then ActiveSheet.Hyperlinks.Add rng, "mailto:" & rng.Value End If Next rng End Sub 根据单元格值处理字体颜色下面的示例可根据单元格中显示的值将单元格的字体设置为某种颜色。具体来说,如果单元格包含公式(例如“=today()”),则设置为黑色,如果单元格包含数据(例如“30 Oct 2004”),则设置为蓝色。 Sub ColorCells() On Error Resume Next With Sheet1.UsedRange .SpecialCells(xlCellTypeFormulas).Font.Color = vbBlack .SpecialCells(xlCellTypeConstants).Font.Color = vbBlue End With On Error GoTo 0 End Sub 前面的示例可更改工作表的整个使用范围的字体颜色。以下代码片段使用 Range 对象的 HasFormula 属性来确定一个单元格是否包含公式: Sub ColorCells2() With Sheet1.Range("A3") If .HasFormula Then .Font.Color = vbBlack Else .Font.Color = vbBlue End If End With End Sub 或 Sub ColorCells3() With Cells(3, 3) .Interior.Color = IIf(.HasFormula, vbBlue, vbBlack) End With End Sub (责任编辑:admin) |