我做错了什么? VBA如果这个和那么这个

时间:2014-05-12 13:26:01

标签: excel vba excel-vba

我真的需要帮助..

我试图让这段代码正确。 我需要在邮政编码的前面放一个0。只有非空细胞和短于5的细胞。

For i = 2 To ende2
    If (Not IsEmpty(LTrim(Cells(i, 9).Value))) And Len(LTrim(Cells(i, 9).Value)) < 5 Then
         Cells(i, 9).Value = "0" & Cells(i, 9).Value
    End If
next i

代码返回邮政编码前面的0 ..但是也将0放入空单元格中......为什么?

我是编程新手..所以请不要对我这么难:P

感谢您的帮助:)

LG Madosa

3 个答案:

答案 0 :(得分:0)

LTrim导致该值非空,即使它仍然是零长度字符串。试试这个:

If (Not IsEmpty(Cells(i, 9))) And Len(LTrim(Cells(i, 9).Value)) < 5 Then

顺便说一句,如果您尝试添加零的值是数字,则程序将没有结果。您需要将单元格格式更改为Text。把它放在If行之后:

Cells(i, 9).NumberFormat = "@"

答案 1 :(得分:0)

您不需要代码。

假设邮政编码在A栏中,请将此公式添加到B栏。

=IF(A1<>"","0"&A1,"")

以下细分说明。

' Checks the cell in A1 for something
    =IF(A1<>"",
' If there is, concatenate a "0" and whatever is in A1
        "0"&A1, 
' Otherwise put an empty string.
        "")

然后您可以复制B列 然后选择编辑&gt;选择性粘贴&gt;值 从公式转换为文本。

ALT + E,然后是S,然后是V,然后单击“确定”。

答案 2 :(得分:0)

虽然这段代码更长,但它会明显更快

  • 忽略要开始的空单元格(使用Set rng1 = Selection.SpecialCells(xlConstants, xlNumbers)
  • 它使用变量数组

Sub AddLeadingZeros()
    Dim rng1 As Range
    Dim rngArea As Range
    Dim strRep As String
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngCalc As Long
    Dim X()

    strRep = "'0"

    On Error Resume Next
    'Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8)
    Set rng1 = Selection.SpecialCells(xlConstants, xlNumbers)
    If rng1 Is Nothing Then Exit Sub
    On Error GoTo 0

   'Speed up the code by turning off screenupdating and setting calculation to manual
   'Disable any code events that may occur when writing to cells
    With Application
        lngCalc = .Calculation
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With

    'Test each area in the user selected range

    'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
    For Each rngArea In rng1.Areas
        'The most common outcome is used for the True outcome to optimise code speed
        If rngArea.Cells.Count > 1 Then
           'If there is more than once cell then set the variant array to the dimensions of the range area
           'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks
            X = rngArea.Value2
            For lngRow = 1 To rngArea.Rows.Count
                For lngCol = 1 To rngArea.Columns.Count
                    'add leading zeroes
                    If Len(X(lngRow, lngCol)) < 5 Then X(lngRow, lngCol) = strRep & X(lngRow, lngCol)
                Next lngCol
            Next lngRow
            'Dump the updated array swith a leading zeroes back over the initial range
            rngArea.Value2 = X
        Else
            'caters for a single cell range area. No variant array required
            If (Len(rngArea.Value) < 5) Then rngArea.Value = strRep & rngArea.Value2
        End If
    Next rngArea

    'cleanup the Application settings
    With Application
        .ScreenUpdating = True
        .Calculation = lngCalc
        .EnableEvents = True
    End With

  End Sub