Excel VBA代码如何将文本添加到特定单元格

时间:2016-07-15 15:03:30

标签: vba excel-vba excel

特定列中所有单元格的长度必须为6个字符。如果没有,我必须在每个单元格的开头添加0,直到单元格长度= 6。最好的方法是什么?

2 个答案:

答案 0 :(得分:0)

以下是 C 列的示例:

Sub Make6()
    Dim r As Range, r1 As Range, r2 As Range, r3 As Range
    bry = Array("000000", "00000", "0000", "000", "00", "0", "")
    Dim N As Long, v As String, L As Long
    Set r1 = Range("C:C")
    N = Cells(Rows.Count, "C").End(xlUp).Row
    Set r2 = Range("C1:C" & N)
    Set r3 = Range("C" & N + 1 & ":C" & Rows.Count)

    r1.NumberFormat = "@"
    For Each r In r2
        v = r.Value
        L = Len(v)
        If L < 6 Then
            r.Value = bry(L) & v
        End If
    Next r
    r3.Value = "000000"

End Sub

它将修复列 C 中的所有单元格。

答案 1 :(得分:0)

Insert into sgn values (( select isnull(max(idsgn),0)+1 from sgn  ) , N'بهنام' , N'بهنام'  )

这对你有用吗?您只需要编辑要检查的coloumn,就可以使用TextBox。