在Excel中自动填充数据

时间:2019-05-18 15:56:30

标签: excel vba excel-formula

enter image description here

我有一个8列和1500行的Excel工作表。

有两个标签数据列,其中提到(限制)From(开始)和To(结束)。我的要求是根据限制填充行。

示例:在第一行中,从1(到)到100(到)。然后,我想在rows(100)中填充1到100,并将其中的其余列数据拖动到所有100行。每行中只有标签数据列在更改,其余数据是通用的。我想做100行。

我尝试通过手动拖动来填充每一行。有什么解决办法吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

Data and Output

您可以尝试以下代码:

Sub autoFillDataInExcel()

Dim dSh As Worksheet
Set dSh = ThisWorkbook.Sheets("DATA") 'Worksheet that contains data to process
Dim oSH As Worksheet
Set oSH = ThisWorkbook.Sheets("OUTPUT") 'Ouput data

Dim data1 As String, data2 As String
Dim rFrom As Long, rTo As Long
Dim ouputRow As Long
outputRow = 2 'Default row for output worksheet

Application.ScreenUpdating = False

For a = 2 To dSh.Range("A" & Rows.Count).End(xlUp).Row 'Loop until the last row of Column A
    'Transfer data to variable
    data1 = dSh.Range("A" & a).Value
    data2 = dSh.Range("B" & a).Value
    rFrom = dSh.Range("C" & a).Value
    rTo = dSh.Range("D" & a).Value

    For b = rFrom To rTo 'Loop based on the range number
        oSH.Range("A" & outputRow).Value = data1
        oSH.Range("B" & outputRow).Value = data2
        oSH.Range("C" & outputRow).Value = b
        outputRow = outputRow + 1 'Increment Output row for the next data
    Next b
Next a

Application.ScreenUpdating = True
End Sub