如何使用Excel宏用多个值填充行

时间:2019-05-05 01:30:55

标签: excel vba

我有一个作为.txt文件接收的报告。我记录了一个宏以空格分隔打开文件。该报告有多个页面,并在每个页面的顶部包含员工名称。我想在我的初始宏中添加一个步骤,该步骤将在销售每一天复制其名称。每个员工的天数每周可能有所不同,因此我需要能够确定他们在报告中所占天数的范围,并为每行复制其名称。

在该示例中,我需要在日期4/1至4/5的每一行上复制约翰的名字,黛比的名字来自她的销售天数,玛丽的名字来自她的销售天数。

我该怎么做?

Sales Report

3 个答案:

答案 0 :(得分:0)

选择要在表中转换的文本 然后去表格工具,配置,clic转换为文本 并在“转换为文本”区域中,分隔文本,在定义冒号参数限制的字符上按

答案 1 :(得分:0)

修改并尝试:

Option Explicit

Sub test()

    Dim Lastrow As Long, i As Long, y As Long
    Dim strName As String

        With ThisWorkbook.Worksheets("Sheet1") 'Change if needed

            Lastrow = .Cells(.Rows.Count, "E").End(xlUp).Row

            For i = 3 To Lastrow

                If .Range("E" & i).Value <> "" Then

                    strName = .Range("E" & i).Value

                    y = i + 1

                    Do Until IsEmpty(.Cells(y, "A").Value)

                        If IsDate(.Cells(y, "A").Value) Then
                            .Cells(y, "C").Value = strName
                        End If

                        y = y + 1

                    Loop

                End If

            Next i

        End With

    End Sub

答案 2 :(得分:0)

以下内容将为您提供帮助。下面的代码假定您始终在e列(日期的开始-1)中填写此人的姓名。您需要更改工作表索引或将其命名为“ Your_sheet_Name”,如果需要,将i = 2的值更改为...“ 1000”->有时会失败。

Sub Nieuws()

    With ThisWorkbook.Sheets(1)
        For i = 2 To .Range("A1000").End(xlUp).Row
        startpoint = 0
        endpoint = 0

            If .Range("A" & i).Value = "" Then
                counter = counter + 1
                If counter > 20 Then Exit Sub
            End If

            If .Range("A" & i).Value <> "" Then
                startpoint = i
                'assuming that you only have 14 max rows
                For j = i To i + 14
                    If .Range("A" & j).Value = "" Then
                        endpoint = j - 1
                        GoTo filldata
                    End If
                Next j
            End If
filldata:
            If startpoint > 0 And endpoint > 0 Then
                For k = startpoint + 1 To endpoint
                    .Range("D" & k).Value = .Range("E" & startpoint - 1)
                Next k

                i = endpoint + 1
                counter = 0
            End If

         Next i

    End With

End Sub