如何使用具有数据操作的宏

时间:2016-02-26 15:31:38

标签: excel vba macros

对于宏而言我非常新手,需要你的帮助。我需要帮助创建一个宏来将相同值的多行合并到一个单元格中。这就是我要找的东西

Project #  Impacted LOB  Hrs

1111       Operation     500
1111       Operation-IT  1000
1111       Marketing     5000
1111       Marketing-IT  1000  
2222       Operation- IT 100
2222       Operation     200
2222       Marketing     200
2222       Marketing-IT  5000  

我期待的是以下内容:

PROJECT   IT HOURS NON IT HOURS
1111       2000    5500
2222       5100    400

在我的代码下面,但问题是我没有为每个项目获得一行总计

Sub First_Macro()
'Dont display delete button:
Application.DisplayAlerts = False
'************ VARIABLES ****************
    'Starting row to analyze:
    StartRow = 5

    'Total rows to analyze:
    NumCols = 80000

    'New worksheet name:
    Dim New_Worksheet_Name As String
    New_Worksheet_Name = "CCB PPL EXTRACT"

    'CCB PPL Extract worksheet name:
    Dim Plan_Worksheet_Name As String
    Plan_Worksheet_Name = "PPL Project Estimates"

     'New worksheet for summary:Total Summary sheet with graph
    Dim New_Worksheet_Summary_Name As String
    New_Worksheet_Summary_Name = "Analyze HRSs Summary"

'*********end variables********
Dim sh As Worksheet, flg As Boolean
'Add Concept Vs. Requirements worksheet:

flg = False
For Each sh In Worksheets
    If sh.Name Like New_Worksheet_Name Then flg = True: Exit For
    'If sh.Name Like BE_Worksheet_Name Then flg = True: Exit For
Next
If flg = True Then
    Sheets(New_Worksheet_Name).Delete
End If
Dim NEW_WS As Worksheet
Set NEW_WS = ThisWorkbook.Sheets.Add(After:= _
     ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
NEW_WS.Name = New_Worksheet_Name

Dim NEW_SUM_WS As Worksheet
Set NEW_SUM_WS = ThisWorkbook.Sheets.Add(After:= _
     ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
NEW_SUM_WS.Name = New_Worksheet_Summary_Name

'GET DATA:
Dim Plan_WS As Worksheet
Set Plan_WS = Worksheets(Plan_Worksheet_Name)
'Set headers in new worksheet:
NEW_WS.Cells(1, 1).Value = "DM ID                                                                                           "
NEW_WS.Cells(1, 2).Value = "PROJECT TITLE"
NEW_WS.Cells(1, 3).Value = "STATUS"
NEW_WS.Cells(1, 4).Value = "IMPACTED LOB"
NEW_WS.Cells(1, 5).Value = "CONCEPT HRS"
NEW_WS.Cells(1, 6).Value = "REQUIREMENT HRS"
NEW_WS.Cells(1, 7).Value = "RELEASE DATE"

Dim DM_ID_Array(1 To 20000) As String
Dim DM_PROJECT_TITLE_Array(1 To 20000) As String
Dim DM_PROJECT_STATUS_Array(1 To 20000) As String
Dim DM_CONCEPT_CARD_IT_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_REQ_CARD_IT_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_CONCEPT_CARD_BUS_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_REQ_CARD_BUS_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(1 To 20000) As String
Dim DM_ID_Index As Long
DM_ID_Index = 0

'Loop through rows in colum:
Dim j As Long
j = 1
h = 2
prev_dm_id = Plan_WS.Cells(StartRow, 1).Value
For i = StartRow To NumCols
    DM_ID = Plan_WS.Cells(i, 1).Value
    PROJECT_TITLE = Plan_WS.Cells(i, 6).Value
    Status = Plan_WS.Cells(i, 7).Value
    lob = Plan_WS.Cells(i, 10).Value
    concept_hrs = Plan_WS.Cells(i, 17).Value
    req_hrs = Plan_WS.Cells(i, 19).Value
    release_date = Plan_WS.Cells(i, 39).Value
    If Status = "15. Commitment Complete" Then
        NEW_WS.Cells(h, 1).Value = DM_ID
        NEW_WS.Cells(h, 2).Value = PROJECT_TITLE
        NEW_WS.Cells(h, 3).Value = Status
        NEW_WS.Cells(h, 4).Value = lob
        NEW_WS.Cells(h, 5).Value = concept_hrs
        NEW_WS.Cells(h, 6).Value = req_hrs
        NEW_WS.Cells(h, 7).Value = release_date

        If DM_ID = prev_dm_id Then
            prev_dm_id = DM_ID
            DM_ID_Index = DM_ID_Index + 1
            DM_ID_Array(DM_ID_Index) = DM_ID
            DM_PROJECT_TITLE_Array(DM_ID_Index) = PROJECT_TITLE
            DM_PROJECT_STATUS_Array(DM_ID_Index) = Status
            DM_CONCEPT_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = 0
            DM_REQ_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = 0
            DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = 0
            DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = 0
            DM_CONCEPT_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = 0
            DM_REQ_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = 0
            DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = 0
            DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = 0
            '*****************************************************
            If lob = "Auto Finance" Then
             DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
             DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Auto Finance IT" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CARD Business" Then
              DM_CONCEPT_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CARD Canada Business" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CARD Canada IT" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CARD Commercial Card IT" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CARD Technology" Then
              DM_CONCEPT_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CBB TECHNOLOGY" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CCB Architecture, Data, Quality & Chief Development Office" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs

            End If
            If lob = "CCB Operations" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CCB Risk Management" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CHASE WEALTH MGMT" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Commerce Solutions" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Commerce Solutions Technology" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CONSUMER AND COMMUNITY BANKING" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CONSUMER BANKING" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "CORPORATE SECTOR" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Digital" Then
               DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
               DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Digital Technology" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Infrastructure Risk and Controls" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Marketing Services" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Mortgage Banking" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Mortgage Banking Technology" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "OPERATIONS TECHNOLOGY" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Student Loans" Then
              DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
            If lob = "Student Loans IT" Then
              DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + concept_hrs
              DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(DM_ID_Index) + req_hrs
            End If
         End If
End If


    h = h + 1
'End If
Next
'summarize columns
NEW_SUM_WS.Cells(1, 1).Value = "Committed Totals by DM ID"
NEW_SUM_WS.Cells(2, 1).Value = "DM ID "
NEW_SUM_WS.Cells(2, 2).Value = "PROJECT TITLE"
NEW_SUM_WS.Cells(2, 3).Value = "STATUS"
NEW_SUM_WS.Cells(2, 4).Value = "CONCEPT-BUS HRS"
NEW_SUM_WS.Cells(2, 5).Value = "REQ-BUS HRS"
NEW_SUM_WS.Cells(2, 6).Value = "CONCEPT-CARD IT HRS"
NEW_SUM_WS.Cells(2, 7).Value = "REQ-CARD IT HRS"
NEW_SUM_WS.Cells(2, 8).Value = "CONCEPT-NON CARD IT HRS"
NEW_SUM_WS.Cells(2, 9).Value = "REQ-NON CARD IT HRS"
NEW_SUM_WS.Cells(2, 10).Value = "CONCEPT-NON CARD BUS HRS"
NEW_SUM_WS.Cells(2, 11).Value = "REQ-NON CARD BUS HRS"
NEW_SUM_WS.Cells(2, 12).Value = "COMMITMENT DATE"

 Dim jj As Long
jj = 1
For i = 1 To UBound(DM_ID_Array)
    If Len(DM_ID_Array(i)) Then
        NEW_SUM_WS.Cells((2 + i), 1).Value = DM_ID_Array(i)
        NEW_SUM_WS.Cells((2 + i), 2).Value = DM_PROJECT_TITLE_Array(i)
        NEW_SUM_WS.Cells((2 + i), 3).Value = DM_PROJECT_STATUS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 4).Value = DM_CONCEPT_CARD_IT_TOTAL_HRS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 5).Value = DM_REQ_CARD_IT_TOTAL_HRS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 6).Value = DM_CONCEPT_NON_CARD_IT_TOTAL_HRS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 7).Value = DM_REQ_NON_CARD_IT_TOTAL_HRS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 8).Value = DM_CONCEPT_CARD_BUS_TOTAL_HRS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 9).Value = DM_REQ_CARD_BUS_TOTAL_HRS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 10).Value = DM_CONCEPT_NON_CARD_BUS_TOTAL_HRS_Array(i)
        NEW_SUM_WS.Cells((2 + i), 11).Value = DM_REQ_NON_CARD_BUS_TOTAL_HRS_Array(i)
        jj = jj + 1
    End If
 Next i
 End Sub

非常感谢您的帮助

2 个答案:

答案 0 :(得分:2)

在D栏中使用此公式:ko.bindingHandlers.ddl1 = { init: function (element, valueAccessor, allBindings, viewModel, bindingContext) { $(element).append('<option value=1>My tax 1</option>'); $(element).append('<option value=2>My tax 2</option>'); }, update: function(element, valueAccessor, allBindings, viewModel, bindingContext) { alert(valueAccessor()); } } function Entry(child) { var self = this; self.Amount = ko.observableArray(child.Amount); self.Taxes = ko.observableArray(child.Taxes); } var VM = { Entries: ko.observableArray([]), Total: function(){ var tot = 0; for (var i = 0; i < VM.Entries().length; i++) { var amt = parseFloat(VM.Entries()[i].Amount()); var taxtot = 0; for(var j=0;j<VM.Entries()[i].Taxes.length;j++){ taxtot += amt*parseFloat(VM.Entries()[i].Taxes[j]); } tot +=taxtot; } return tot.toFixed(2); } } $(function () { VM.Entries().push(new Entry({})); VM.Entries().push(new Entry({})); ko.applyBindings(VM); });

enter image description here

突出显示整个表格,转到INSERT&gt;数据透视表 它将为工作簿添加一个带有数据透视表的新工作表。 使用数据透视表选项,将项目添加到行标签,将类型添加到列标签,将小时添加到值(进行总和)。

enter image description here

这应该根据您定义的类型细分自动执行总和。

答案 1 :(得分:0)

考虑一个SQL解决方案,因为这是一个经典的条件聚合查询。 MS Excel可以使用适用于所有MS Office产品的Windows内置Jet / ACE SQL引擎(.dll文件)在自己的工作簿上运行SQL,而不仅仅是Excel数据库同级MS Access的受限技术。下面是在VBA宏中嵌入为字符串的SQL查询。

SQL 查询(使用“IT”上的通配符匹配;合并其他搜索)

SELECT [Project #]
       SUM(IIF([Impacted LOB] LIKE '%Operation%', 
               [Hrs], NULL)) As [IT HOURS]
       SUM(IIF([Impacted LOB] NOT LIKE '%-IT%', 
               [Hrs], NULL)) As [NON IT HOURS]
FROM DataTable
GROUP BY [Project #];

工作表设置 (结构数据非常类似于数据库表,第一行中的列和相同的数据类型;创建DATA和RESULTS选项卡,并留下后面的宏填充)

Data Worksheet Tab Results

VBA (另一个连接字符串已注释掉)

Sub RunSQL()
On Error GoTo ErrHandle
    Dim conn As Object, rst As Object
    Dim strConnection As String, strSQL As String
    Dim i As Integer, fld As Object

    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")

    ' Hard code database location and name
'    strConnection = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" _
'                      & "DBQ=C:\Path\To\Workbook.xlsm;"
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
                       & "Data Source='C:\Path\To\Workbook.xlsm;" _
                       & "Extended Properties=""Excel 8.0;HDR=YES;"";"

    strSQL = "SELECT [DATA$].[Project #], " _
              & "     SUM(IIF([DATA$].[Impacted LOB] Like '%Operation%', [DATA$].[Hrs], NULL)) As [IT HOURS]," _
              & "     SUM(IIF([DATA$].[Impacted LOB] NOT LIKE '%-IT%', [DATA$].[Hrs], NULL)) As [NON IT HOURS]" _
              & " FROM [DATA$] " _
              & " GROUP BY [DATA$].[Project #];"

    ' Open the db connection
    conn.Open strConnection
    rst.Open strSQL, conn

    ' column headers
    i = 0
    Worksheets("RESULTS").Activate
    Worksheets("RESULTS").Range("A1").Activate
    For Each fld In rst.Fields
        ActiveCell.Offset(0, i) = fld.Name
        i = i + 1
    Next fld

    ' data rows
    Worksheets("RESULTS").Range("A2").CopyFromRecordset rst

    rst.Close
    conn.Close

    MsgBox "Successfully ran SQL query!", vbInformation
    Exit Sub

ErrHandle:
    MsgBox Err.Number & " = " & Err.Description, vbCritical
    Exit Sub
End Sub