查找范围内的值并打印到列

时间:2016-01-07 15:15:04

标签: excel vba excel-vba

如何通过宏生成如下图所示的Excel? 简单地说,我想:

  • a1和b1之间的数字打印到d列;
  • a2和b2之间的数字打印到e列;
  • a3和b3之间的数字打印到f列。

A列和B列有数千个值。

excel rows mahmut

5 个答案:

答案 0 :(得分:3)

作为替代方案,这是一个公式解决方案:

=IF(ROW(D1)>INDEX($A:$B,COLUMN(D1)-COLUMN($C1),2)-INDEX($A:$B,COLUMN(D1)-COLUMN($C1),1)+1,"",INDEX($A:$B,COLUMN(D1)-COLUMN($C1),1)+ROW(D1)-1)

虽然我意识到基于这个陈述,公式解决方案可能不可行:

  

A列和B列有数千个值。

编辑:纯阵列VBA解决方案:

Sub tgr()

    Dim ws As Worksheet
    Dim rData As Range
    Dim aData As Variant
    Dim aResults() As Variant
    Dim lMaxDiff As Long
    Dim i As Long, j As Long
    Dim rIndex As Long, cIndex As Long

    Set ws = ActiveWorkbook.ActiveSheet
    Set rData = ws.Range("A1", ws.Cells(Rows.Count, "B").End(xlUp))

    lMaxDiff = Evaluate("MAX(" & rData.Columns(2).Address(external:=True) & "-" & rData.Columns(1).Address(external:=True) & ")") + 1
    aData = rData.Value2
    ReDim aResults(1 To lMaxDiff, 1 To rData.Rows.Count)

    For i = LBound(aData, 1) To UBound(aData, 1)
        If IsNumeric(aData(i, 1)) And IsNumeric(aData(i, 2)) Then
            rIndex = 0
            cIndex = cIndex + 1
            For j = Int(aData(i, 1)) To Int(aData(i, 2))
                rIndex = rIndex + 1
                aResults(rIndex, cIndex) = j
            Next j
        End If
    Next i

    ws.Range("D1").Resize(UBound(aResults, 1), UBound(aResults, 2)).Value = aResults

End Sub

答案 1 :(得分:2)

只因为我喜欢谜题:

Sub u5758()
Dim x As Long
Dim i As Long
Dim oArr() As Variant
Dim arr() As Long
Dim rng As Range
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = ActiveSheet

x = 4
With ws
oArr = .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp)).value
    For j = LBound(oArr, 1) To UBound(oArr, 1)
        ReDim arr(oArr(j, 1) To oArr(j, 2))
        For i = LBound(arr) To UBound(arr)
            arr(i) = i
        Next i
        .Cells(1, x).Resize(UBound(arr) - LBound(arr) + 1).value = Application.Transpose(arr)
        x = x + 1
    Next j
End With
Application.ScreenUpdating = True

End Sub

enter image description here

答案 2 :(得分:2)

你可以用这个:

 Sub test()

Dim Lastrow As Long
Dim j As Double, i As Double, r As Double
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1") ' Change the name of your sheet

Lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row

j = 4 ' Column D

With ws

For i = 1 To Lastrow ' Start the loop at A1 until the last row in column A

    .Cells(1, j) = .Cells(i, 1).Value

r = 1

    Do
        .Cells(r + 1, j) = .Cells(r, j) + 1
        r = r + 1

    Loop Until .Cells(r, j) = .Cells(i, 2).Value

    j = j + 1

Next i

End With

End Sub

答案 3 :(得分:2)

我也喜欢谜题。

Sub from_here_to_there()
    Dim rw As Long
    With Worksheets("Sheet5")  '<~~ set this worksheet properly!
        For rw = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
            If IsNumeric(.Cells(rw, 1)) And IsNumeric(.Cells(rw, 2)) Then
                With .Columns(Application.Max(4, .Cells(1, Columns.Count).End(xlToLeft).Column + 1))
                    .Cells(1, 1) = .Parent.Cells(rw, 1).Value2
                    .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
                        Step:=1, Stop:=.Parent.Cells(rw, 2).Value2
                End With
            End If
        Next rw
    End With
End Sub

number_series

答案 4 :(得分:1)

在这里,另一个快速的只是为了好玩:

Sub transposeNfill()
Dim lastRow&, i&, xStart$, xEnd$, xMid$

lastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To lastRow
    xStart = Cells(i, 1)
    xEnd = Cells(i, 2)
    xMid = xEnd - xStart
    Cells(1, i + 3).Value = xStart
    Cells(1 + xMid, i + 3) = xEnd
    Range(Cells(2, i + 3), Cells(xMid, i + 3)).FormulaR1C1 = "=r[-1]c+1"
    Cells.Copy
    Cells.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False

Next i

End Sub