创建宏和重新排列Excel中的结果选项卡



我是在Excel中创建宏的新手。我正在从SQL Developer生成报告并创建Excel文件。

现在,我想创建宏,并在Excel中重新排列我的结果。为此,我想在Excel和单击按钮后创建按钮。

我不确定是否可以在Excel宏中做。我正在使用Office版本2016。

您可以将SQL与以下类型相结合:

SELECT 
i.name, i.id, id.date, id.import,
e.export,
v.volume
FROM IMPORT AS i
LEFT JOIN EXPORT AS e ON i.date = e.date
LEFT JOIN VOLUME AS v ON i.date = v.date

我是SQL的新手,但慢慢地习惯了编写查询,所以希望我的建议没有犯任何错误。

,或者如果您仍然希望在Excel中进行,以下将执行技巧

假设:从正确的工作簿中运行宏,并且使用正确的工作表活动

Sub consolidateOutput()
' note: this presumes your import data will always be on the first row
Dim exportStartRow, volumeStartRow, lastRow As Integer
lastRow = Range("D" & Rows.Count).End(xlUp).Row ' finds the last row with data in column D
For i = 1 To lastRow ' loop through column D from the 1st row until the last row with data
    If Range("D" & i).Value = "export" Then '
        exportStartRow = i ' set the row number where you found "export"
    ElseIf Range("D" & i).Value = "volume" Then
        volumeStartRow = i ' set the row number where you found "volume"
        GoTo exitLoop
    End If
Next i
exitLoop:
' using the row numbers you found above, select and then past the export data
Range("D" & exportStartRow & ":D" & volumeStartRow - 1).Select
Selection.Copy
Range("E1").Select
ActiveSheet.Paste
' using the row numbers you found above, select and then past the volume data
Range("D" & volumeStartRow & ":D" & lastRow).Select
Selection.Copy
Range("F1").Select
ActiveSheet.Paste
End Sub

最新更新