简体   繁体   English

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

[英]What I'm doing wrong ? VBA IF this AND this then this

I really need help .. 我真的需要帮助..

Im trying to get this code right. 我试图使此代码正确。 I need to put a 0 infront of a postcode. 我需要在邮政编码前面加上0。 Only in not empty cells and cells shorter than 5. 仅在非空单元格和小于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

the code returns a 0 infront of the postcode .. but puts a 0 into empty cells too .. why ? 代码返回邮政编码的0开头..但是也将0放入空单元内..为什么?

Im new to programming .. so please dont be hard to me :P 我是编程的新手..所以请不要对我很难:P

Thanks for helping :) 感谢您的帮助:)

LG Madosa LG Madosa

The LTrim causes the value to be non-empty, even though it is still a zero-length string. LTrim导致该值是非空的,即使它仍然是零长度的字符串。 Try this: 尝试这个:

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

Incidentally, if the values you are trying to add a zero to are numeric, the program will have no result. 顺便说一句,如果您要向其添加零的值是数字,则该程序将没有结果。 You will need to change the cell format to Text. 您将需要将单元格格式更改为文本。 Put this after the If line: 将其放在If行之后:

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

You don't need code. 您不需要代码。

Assuming postcodes are in Column A, add this formula into column B. 假设邮政编码在A列中,则将此公式添加到B列中。

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

The breakdown below explains. 下面的细分说明。

' 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.
        "")

You can then copy Column B and choose Edit > Paste Special > Values to convert from the formula into text. 然后,您可以复制B列并选择“编辑”>“选择性粘贴”>“值”以将公式从文本转换为文本。

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

While this code is longer is will be significantly faster as it 虽然这段代码更长,但是它将大大加快速度

  • ignores the empty cells to start with (using Set rng1 = Selection.SpecialCells(xlConstants, xlNumbers) ) 忽略开头的空单元格(使用Set rng1 = Selection.SpecialCells(xlConstants, xlNumbers)
  • it uses variant arrays 它使用变量数组

code

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM