Page 1 of 1

รวมไฟล์ แค่บางชีท

Posted: Mon May 13, 2019 5:39 pm
by choinat
ทำไฟล์ขึ้นมารวมไฟล์ หลายไฟล์มารวมกันครับแต่ติดตรง ไฟล์มีหลายชีท มีชีท BB กับ AGENT01 แต่ต้องการแค่ชีท AGENT01 ไม่ต้องการ BB ครับ

Re: รวมไฟล์ แค่บางชีท

Posted: Mon May 13, 2019 8:53 pm
by puriwutpokin
ลองปรับเป็นตามนี้ครับ คราวหน้าให้แนบโค้ดมาแบบที่ผมแนบนี้ด้วยครับ และแจ้งว่าติดต้องไหนด้วยครับ :D

Code: Select all

Dim Lrow As Integer
Private Sub CommandButton1_Click()

    Dim sh As Worksheet, thisBook As Workbook, strThisbook As Variant
    Dim ob As Workbook, i As Integer, rRow As Long, tg As Range
    
    'Set ob = ThisWorkbook
    'ob.sheets(2).UsedRange.Clear
    strThisbook = Application.GetOpenFilename(Filefilter:= _
            "All File (*.*), *.*", Title:="Please select source file(s).", MultiSelect:=True)

    Set ob = ThisWorkbook
    If TypeName(strThisbook) = "Boolean" Then
        'MsgBox "Please select file(s)."
        Exit Sub
    End If
    'rRow = 1
    rRow = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
    
    For i = 1 To UBound(strThisbook)
        Set thisBook = Workbooks.Open(strThisbook(i))
        Application.ScreenUpdating = False
'        For Each sh In thisBook.Worksheets
   Set sh = Sheets("BB")
          sh.UsedRange.Copy
           ob.Sheets(2).Range("A" & rRow).PasteSpecial xlPasteValues
            rRow = ob.Sheets(2).UsedRange.Rows.Count + 1 'ถ้า +2 จะได้ยอด Total ยอดสุดท้ายมาด้วย
           Application.CutCopyMode = False
'        Next sh
        thisBook.Saved = True
        thisBook.Close
    Next i
    
      Application.ScreenUpdating = True
    
    MsgBox "รวมข้อมูลทุกไฟล์ให้เรียบร้อยแล้ว กรุณาตรวจสอบข้อมูลอีกครั้ง"

End Sub