首页 文章

Excel VBA根据单元格中的值填充空白单元格[关闭]

提问于
浏览
-2

我是VBA Excel的新手 .

我正在使用excel宏来根据另一个单元格中的值填充单元格中所有空白行的特定值 .

比方说,我们有一行:

blank1 blank2 blank3 AR blank4 blank5 DP blank6 blank 7 AR blank8 blank9 DP blank10 bl1nk11

我希望AR之前的所有空格都填充“OV”,并且“DP”之前的所有空白单元格都填充“OS”

请帮助我,我将能够将它集成到我的大代码中 .

谢谢 .

1 回答

  • 1

    通常我们希望你发布你到目前为止所做的事情,但我很好奇我是否能够编码或不编码 . 所以!试试这个 . 如果需要在多行上执行此操作,则可以添加另一个循环并将1更改为变量 . 希望这可以帮助!

    Option Explicit
    
    Sub FillEmpty()
    
        Dim ws As Worksheet
        Dim myRange As Range
        Dim lColumn As Long
        Dim x As Integer
    
        Set ws = Worksheets("Sheet1")
    
        'Find the last column that contains data
        lColumn = ws.Cells.Find(What:="*", _
            After:=ws.Cells(1, 1), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Column
    
        With ws
            For x = lColumn To 2 Step -1
                If .Cells(1, x) = "AR" Then
                    .Cells(1, x - 1).Value = "OV"
                ElseIf .Cells(1, x) = "DP" Then
                    .Cells(1, x - 1).Value = "OS"
                ElseIf .Cells(1, x).Value <> "" And .Cells(1, x - 1).Value = "" Then
                    .Cells(1, x - 1).Value = .Cells(1, x).Value
                End If
            Next x
        End With
    End Sub
    

相关问题