条件复制范围上的两个表SQL Server 2008

时间:2017-03-15 08:49:48

标签: sql-server-2008 datatable conditional-compilation

我有两个表,需要将第一个表更新为第三个屏幕截图。 This is the first table. The VON is the first value of the range. This value is picked up from the second table till the BIS value is reached. While the BIS value is reached in the second table, the RANGE column is updated with the values between VON and BIS values. 这是第一张表。 VON是该范围的第一个值。从第二个表中获取该值,直到达到BIS值。在第二个表中达到BIS值时,RANGE列将使用VON和BIS值之间的值进行更新。

第二个表包含从01到99的顺序列出的值和字母数字值,如A1,A2等。

This is the required output.

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

Option Explicit
Sub ExpandRows()
Dim rgLastCell As Range
Dim rgThisRow As Range
Dim wsMaster As Worksheet
Dim wsNewSheet As Worksheet
Dim rgThisRecord As Range
Dim intVon As Integer
Dim intRes As Integer
Dim dblCopyRowNumber As Double

Set wsMaster = ActiveSheet
Set wsNewSheet = Worksheets.Add(after:=wsMaster)
wsNewSheet.Name = "Output"
Set rgLastCell = GetLastCell(wsMaster)
dblCopyRowNumber = 2

wsMaster.Rows(1).Copy wsNewSheet.Range("A1")
wsmaster.columns.autofit

For Each rgThisRow In Range("A2:A" & rgLastCell.Row)
    Set rgThisRecord = wsMaster.Range(Cells(rgThisRow.Row, 1).Address, Cells(rgThisRow.Row, rgLastCell.Column).Address)
    Debug.Print rgThisRecord.Address
    If IsNumeric(rgThisRecord(8)) Then

        intVon = rgThisRecord(8).Value
        intRes = rgThisRecord(9).Value

        While intVon <= intRes
            rgThisRecord.Copy wsNewSheet.Range("A" & dblCopyRowNumber)
            wsNewSheet.Cells(dblCopyRowNumber, 11).Value = intVon
            dblCopyRowNumber = dblCopyRowNumber + 1
            intVon = intVon + 1
        Wend
    Else
        rgThisRecord.Copy wsNewSheet.Range("A" & dblCopyRowNumber)
        dblCopyRowNumber = dblCopyRowNumber + 1
    End If
Next

Set rgLastCell = Nothing
Set rgThisRow = Nothing
Set wsMaster = Nothing
Set wsNewSheet = Nothing
Set rgThisRecord = Nothing

End Sub

这是查找最后一个单元格函数:

Function GetLastCell(ByVal wsCurrentSheet As Worksheet) As Range
Dim rgLastRow As Range
Dim rglastColumn As Range
Dim rgLastCell As Range

Set rgLastRow = wsCurrentSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious)
Set rglastColumn = wsCurrentSheet.Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious)

Set GetLastCell = wsCurrentSheet.Cells(rgLastRow.Row, rglastColumn.Column)

Set rgLastCell = Nothing
Set rgLastRow = Nothing
Set rglastColumn = Nothing

End Function

希望有所帮助。