iT邦幫忙

0

excel跨工作表自動搜索函數

  • 分享至 

  • xImage

請問高手大大
【彙整】sheet C3至E3數值可自動搜索對應工作表進行加總嗎?我只會=VLOOKUP(A3,B站!A:D,2,0),因實際作業會有多個工作表,請高手們指導,謝謝。
https://ithelp.ithome.com.tw/upload/images/20230113/201367048YQqNQhfLk.jpg

blanksoul12 iT邦研究生 5 級 ‧ 2023-01-13 15:48:07 檢舉
你是一個檔案很多工作表要做滙總還是有很多檔案要滙總?
很多個工作表
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
ccenjor
iT邦大師 9 級 ‧ 2023-01-13 19:20:33
最佳解答

彙整工作表C3:
=VLOOKUP($A3,INDIRECT($B3&"!$A$3:$D$5"),COLUMN()-1,0)
向右複製公式到D3:E3。
https://ithelp.ithome.com.tw/upload/images/20230113/20109881OCXUlcSFUL.jpg

0
blanksoul12
iT邦研究生 5 級 ‧ 2023-01-14 12:28:07

把彙整變成 result, 另外表頭(料號,站別....)那些要在第一行開始
以下是 vba sql (我是初學), 送兩個給你,自己試試

Sub sample_1()
    
    Dim Conn As Object, Rst As Object
    Dim strConn As String, strSQL As String
    Dim i As Integer, PathStr As String
    Set Conn = CreateObject("ADODB.Connection")
    Set Rst = CreateObject("ADODB.Recordset")
    PathStr = ThisWorkbook.FullName
    Select Case Application.Version * 1
    Case Is <= 11
        strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
    Case Is >= 12
        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
    End Select
    Conn.Open strConn
    
    If Worksheets("result").[a1048576].End(xlUp).Row > 1 Then
        Worksheets("result").Range("a2:m" & Worksheets("result").[a1048576].End(xlUp).Row).Clear
    End If

    Set dc = CreateObject("Scripting.Dictionary")
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "Result" Then
            For j = 2 To ws.[a1048576].End(xlUp).Row
                With dc
                    If dc.exists(ws.Cells(j, "a").Value) = False Then
                        dc.Add ws.Cells(j, "a").Value & "," & ws.Name, 1
                    End If
                End With
            Next
        End If
    Next
    Worksheets("result").[a2].Resize(UBound(dc.keys) + 1, 1) = WorksheetFunction.Transpose(dc.keys)
    
    Worksheets("Result").Sort.SortFields.Clear
    Worksheets("Result").Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With Worksheets("Result").Sort
        .SetRange Worksheets("Result").Range("a2:a7")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    For i = 2 To Worksheets("result").[a1048576].End(xlUp).Row
        Worksheets("result").Cells(i, "a").Resize(1, 2) = Split(Worksheets("result").Cells(i, "a"), ",")
        For j = 3 To Worksheets("result").[zz1].End(xlToLeft).Column
            strSQL = "Select `" & Worksheets("result").Cells(1, j) & "` FROM [" & Worksheets("result").Cells(i, "b") & "$] where `料號`='" & Worksheets("result").Cells(i, "a") & "'" & ""
            Set Rst = Conn.Execute(strSQL)
            If Rst.EOF = False And Rst.BOF = False Then
                If IsNull(Rst.Fields(0).Value) = False Then
                    Worksheets("result").Cells(i, j) = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Conn.Execute(strSQL).getrows))(1)
                End If
            End If
        Next
    Next
    
    Conn.Close
    
End Sub
Sub sample_2()
    
    Dim Conn As Object, Rst As Object
    Dim strConn As String, strSQL As String
    Dim i As Integer, PathStr As String
    Set Conn = CreateObject("ADODB.Connection")
    Set Rst = CreateObject("ADODB.Recordset")
    PathStr = ThisWorkbook.FullName
    Select Case Application.Version * 1
    Case Is <= 11
        strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
    Case Is >= 12
        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
    End Select
    Conn.Open strConn
    
    If Worksheets("result").[a1048576].End(xlUp).Row > 1 Then
        Worksheets("result").Range("a2:m" & Worksheets("result").[a1048576].End(xlUp).Row).Clear
    End If
    
    Set dc = CreateObject("Scripting.Dictionary")
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "Result" Then
            For j = 2 To ws.[a1048576].End(xlUp).Row
                With dc
                    If dc.exists(ws.Cells(j, "a").Value) = False Then
                        dc.Add ws.Cells(j, "a").Value, 1
                    End If
                End With
            Next
        End If
    Next
    Worksheets("result").[a2].Resize(UBound(dc.keys) + 1, 1) = WorksheetFunction.Transpose(dc.keys)
    
    For Each ws In ThisWorkbook.Sheets
        If ws.Name <> "Result" Then
            For i = 2 To Worksheets("result").[a1048576].End(xlUp).Row
                For j = 3 To ws.[zz1].End(xlToLeft).Column
                    strSQL = "Select `" & Worksheets("result").Cells(1, j) & "` FROM [" & ws.Name & "$] where `料號`='" & Worksheets("result").Cells(i, "a") & "'" & ""
                    Set Rst = Conn.Execute(strSQL)
                    If Rst.EOF = False And Rst.BOF = False Then
                        If IsNull(Rst.Fields(0).Value) = False Then
                            Worksheets("result").Cells(i, j) = Worksheets("result").Cells(i, j) + WorksheetFunction.Transpose(WorksheetFunction.Transpose(Conn.Execute(strSQL).getrows))(1)
                        End If
                    End If
                Next
            Next
        End If
    Next
    
    Conn.Close
    
End Sub
blanksoul12 iT邦研究生 5 級 ‧ 2023-01-14 12:36:04 檢舉

純粹滙總
https://ithelp.ithome.com.tw/upload/images/20230114/20135171pKUsR0G4hL.jpg

blanksoul12 iT邦研究生 5 級 ‧ 2023-01-14 12:36:21 檢舉

加總
https://ithelp.ithome.com.tw/upload/images/20230114/20135171UeoJmqBp5u.jpg

非常感謝回覆,但VBA複雜目前不太適合,謝謝。

我要發表回答

立即登入回答