尝试根据上面的数字填充包含字母字符的空白

时间:2017-09-13 14:03:15

标签: excel-vba vba excel

我有这个。

3
3.1
3.2
3.3
3.4
4
4.1
NULL
NULL
NULL
NULL
5

我想要这个。

3
3.1
3.2
3.3
3.4
4
4.1
4.A
4.B
4.C
4.D
5

我有大约22k行要填写。我怎么能这样做?我把下面的代码放在一起,bu tit不能做我想要的。每当找到一个新的空白单元格时,我需要从之前的数字+'A'重新开始。

这是我到目前为止所做的,但它不起作用。

Sub AlphaFill()
    Dim Cell, CellChars
    Dim Default, Prompt, Title
    Dim rangeSelected As Range
    Dim UpperCase As Boolean
    On Error Resume Next
    Set rangeSelected = Range("F1:F21400")

    For Each Cell In rangeSelected

        If Cell.Value <> "" Then
            i = 1
        End If

        If Cell.Value = "" Then
            CellChars = Chr(64 + i)
            If Not UpperCase Then CellChars = UCase(CellChars)
            Cell.Value = Cell.Value & CellChars
            i = i + 1
        End If

        Debug.Print Cell.Value

    Next

End Sub

问题是,我似乎无法保留先前的单元格,例如4.A,4.B,4.C和4.D

3 个答案:

答案 0 :(得分:1)

哦,我明白了。这有效!!

Sub AlphaFill()
    Dim Cell, CellChars
    Dim Default, Prompt, Title
    Dim rangeSelected As Range
    Dim UpperCase As Boolean
    On Error Resume Next
    Set rangeSelected = Range("F1:F21400")

    For Each Cell In rangeSelected

        If Cell.Value <> "" Then
            KeepValue = Cell.Value
            i = 1
        End If

        If Cell.Value = "" Then
            CellChars = Chr(64 + i)
            If Not UpperCase Then CellChars = UCase(CellChars)
            Cell.Value = KeepValue & CellChars
            i = i + 1
        End If

    Next

End Sub

答案 1 :(得分:1)

如果我理解正确,那么您需要存储您看到的最后一个整数,然后在下一个空白单元格中使用它,重置间隙之间的计数器:

Sub AlphaFill()
    Dim Cell            As Range
    Dim UpperCase       As Boolean
    Dim LastWholeNumber As String
    Dim LastLetter      As Long
    Dim CurrentNumber   As String

    UpperCase = True

    For Each Cell In Range("F1:F21400")
        CurrentNumber = Cell.Value

        If CurrentNumber = "" Then
            LastLetter = LastLetter + 1
            Cell.Value = LastWholeNumber & "." & ChrW$(LastLetter)
        ElseIf InStr(CurrentNumber, ".") = 0 Then
            '// whole number - store it & reset to A/a 
            LastLetter = IIf(UpperCase, 64, 97)
            LastWholeNumber = CurrentNumber
        End If
    Next
End Sub

答案 2 :(得分:0)

假设您的数据为

enter image description here

试试这个

Sub AlphaFill()
    Dim Cell, CellChars
    Dim Default, Prompt, Title
    Dim rangeSelected As Range
    Dim UpperCase As Boolean
    Dim charIndex As Long
    On Error Resume Next
    Set rangeSelected = Range("F1:F21400")

    UpperCase = True
    charIndex = 65
    For Each Cell In rangeSelected
        If Cell.Value = "" Then
            If InStr(Cell.Offset(-1, 0), ".") > 0 Then  'check if value contains "."
                Cell.Value = Left(Cell.Offset(-1, 0), WorksheetFunction.Find(".", Cell.Offset(-1, 0))) & Chr(charIndex) 'extract the string till "."
            Else
                Cell.Value = Cell.Offset(-1, 0) & "." & Chr(charIndex)  'get the number and add "."
            End If
            charIndex = charIndex + 1
        Else
            If charIndex <> 65 Then charIndex = 65  'if cell is not blank set charIndex to 65
        End If
    Next
End Sub

这将得到结果

enter image description here