Vairāku Excel tabulu apvienošana vienā Word dokumentā ar VBA

Vairāku Excel tabulu apvienošana vienā Word dokumentā ar VBA
Vairāku Excel tabulu apvienošana vienā Word dokumentā ar VBA

Efektīva Excel datu apvienošana programmā Word

Datu pārvaldība dažādās platformās var būt apgrūtinošs uzdevums, it īpaši, ja Word dokumentā ir jāapkopo vairākas tabulas no Excel. Izmantojot VBA, jūs varat automatizēt šo procesu, nodrošinot netraucētu datu pārsūtīšanu, vienlaikus saglabājot vēlamo formātu un struktūru.

Šajā rakstā ir apskatīts VBA makro, kas pašlaik izveido trīs atsevišķus Word dokumentus no Excel tabulām. Mēs parādīsim, kā modificēt kodu, lai izveidotu visas tabulas vienā Word dokumentā ar lappušu pārtraukumiem aiz katras tabulas skaidrības un sakārtotības labad.

Pavēli Apraksts
Set wdApp = New Word.Application Inicializē jaunu Word lietojumprogrammas gadījumu.
wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak Dokumenta beigās ievieto lappuses pārtraukumu.
.Rows(1).HeadingFormat = True Norāda, ka tabulas pirmā rinda ir galvenes rinda.
.Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol) Word dokumentam pievieno jaunu tabulu ar norādītajām rindām un kolonnām.
With wdTbl.Borders Iestata tabulas apmales stilu iekšējās un ārējās līnijas.
wdApp.Visible = True Padara Word lietojumprogrammu redzamu lietotājam.
If (r - startRow + 2) >If (r - startRow + 2) > .Rows.Count Then .Rows.Add Pievieno tabulai jaunu rindu, ja pašreizējā rinda pārsniedz esošo rindu skaitu.
Set wdDoc = .Documents.Add Programmā Word izveido jaunu dokumentu.

Izpratne par VBA makro tabulu apvienošanai

Piedāvātie skripti parāda, kā automatizēt datu pārsūtīšanas procesu no vairākām Excel tabulām vienā Word dokumentā, izmantojot VBA. Galvenais skripts, Sub ConsolidateTablesInOneDocument(), inicializē jaunu Word lietojumprogrammas gadījumu ar Set wdApp = New Word.Application un izveido jaunu dokumentu, izmantojot Set wdDoc = .Documents.Add. Tas identificē Excel rindas, kurās tabulas beidzas, pārbaudot tukšas šūnas, un saglabā šīs pozīcijas mainīgajos. First un Second. Tas ļauj skriptam zināt, kur katra tabula beidzas un sākas. Pēc tam makro izveido tabulas programmā Word, izmantojot .Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol) un aizpilda šīs tabulas ar datiem no Excel.

Lai nodrošinātu, ka katra tabula ir skaidri nodalīta, skripts ievieto lappuses pārtraukumu pēc katras tabulas izmantošanas wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak. The CreateTable apakšprogramma tiek izsaukta trīs reizes, lai ģenerētu un formatētu katru tabulu. Šī apakšprogramma definē galvenes, aizpilda rindas un kolonnas un piemēro apmales stilus tabulām ar .Rows(1).Range.Font.Bold = True un With wdTbl.Borders. Visbeidzot, makro iestata Word lietojumprogrammas redzamību uz True with wdApp.Visible = True, nodrošinot, ka lietotājs var redzēt ģenerēto dokumentu. Šī pieeja efektīvi apvieno vairākas tabulas no Excel vienā Word dokumentā, saglabājot skaidrību un formātu.

Vairāku Excel tabulu apvienošana vienā Word dokumentā

Šis skripts parāda, kā programmā Excel izmantot VBA, lai apvienotu vairākas tabulas vienā Word dokumentā ar lappušu pārtraukumiem pēc katras tabulas.

Sub ConsolidateTablesInOneDocument()
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    Dim wdTbl As Word.Table
    Dim xlSht As Worksheet
    Dim lRow As Integer, lCol As Integer
    Dim r As Integer, c As Integer
    Dim Blanks As Integer, First As Integer, Second As Integer
    lRow = Sheets("Feedback Sheets").Range("A1000").End(xlUp).Row - 2
    Blanks = 0
    i = 1
    Do While i <= lRow
        Set rRng = Worksheets("Feedback Sheets").Range("A" & i)
        If IsEmpty(rRng.Value) Then
            Blanks = Blanks + 1
            If Blanks = 1 Then First = i
            If Blanks = 2 Then Second = i
        End If
        i = i + 1
    Loop
    Set xlSht = ActiveSheet: lCol = 5
    With wdApp
        .Visible = True
        Set wdDoc = .Documents.Add
        Call CreateTable(wdDoc, xlSht, 1, First, lCol)
        wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
        Call CreateTable(wdDoc, xlSht, First + 1, Second, lCol)
        wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
        Call CreateTable(wdDoc, xlSht, Second + 1, lRow, lCol)
    End With
End Sub
Sub CreateTable(wdDoc As Word.Document, xlSht As Worksheet, startRow As Integer, endRow As Integer, lCol As Integer)
    Dim wdTbl As Word.Table
    Dim r As Integer, c As Integer
    Set wdTbl = wdDoc.Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol)
    With wdTbl
        .Rows(1).Range.Font.Bold = True
        .Rows(1).HeadingFormat = True
        .Cell(1, 1).Range.Text = "Header 1"
        If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
        If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
        For r = startRow To endRow
            If (r - startRow + 2) > .Rows.Count Then .Rows.Add
            For c = 1 To lCol
                .Cell(r - startRow + 2, c).Range.Text = xlSht.Cells(r, c).Text
            Next c
        Next r
    End With
    With wdTbl.Borders
        .InsideLineStyle = wdLineStyleSingle
        .OutsideLineStyle = wdLineStyleDouble
    End With
End Sub

Excel datu sapludināšana programmā Word, izmantojot VBA

Šis skripts izmanto VBA, lai sapludinātu tabulas no Excel lapas vienā Word dokumentā, nodrošinot pareizu formatējumu un lappušu pārtraukumus.

Sub MergeTablesIntoWord()
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    Dim wdTbl As Word.Table
    Dim xlSht As Worksheet
    Dim lRow As Integer, lCol As Integer
    Dim r As Integer, c As Integer
    Dim Blanks As Integer, First As Integer, Second As Integer
    lRow = Sheets("Feedback Sheets").Range("A1000").End(xlUp).Row - 2
    Blanks = 0
    i = 1
    Do While i <= lRow
        Set rRng = Worksheets("Feedback Sheets").Range("A" & i)
        If IsEmpty(rRng.Value) Then
            Blanks = Blanks + 1
            If Blanks = 1 Then First = i
            If Blanks = 2 Then Second = i
        End If
        i = i + 1
    Loop
    Set xlSht = ActiveSheet: lCol = 5
    With wdApp
        .Visible = True
        Set wdDoc = .Documents.Add
        Set wdTbl = wdDoc.Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol)
        PopulateTable wdTbl, xlSht, 1, First, lCol
        wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
        Set wdTbl = wdDoc.Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol)
        PopulateTable wdTbl, xlSht, First + 1, Second, lCol
        wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
        Set wdTbl = wdDoc.Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol)
        PopulateTable wdTbl, xlSht, Second + 1, lRow, lCol
    End With
End Sub
Sub PopulateTable(wdTbl As Word.Table, xlSht As Worksheet, startRow As Integer, endRow As Integer, lCol As Integer)
    Dim r As Integer, c As Integer
    With wdTbl
        .Rows(1).Range.Font.Bold = True
        .Rows(1).HeadingFormat = True
        .Cell(1, 1).Range.Text = "Header 1"
        If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
        If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
        For r = startRow To endRow
            If (r - startRow + 2) > .Rows.Count Then .Rows.Add
            For c = 1 To lCol
                .Cell(r - startRow + 2, c).Range.Text = xlSht.Cells(r, c).Text
            Next c
        Next r
    End With
    With wdTbl.Borders
        .InsideLineStyle = wdLineStyleSingle
        .OutsideLineStyle = wdLineStyleDouble
    End With
End Sub

Tabulu izveide un formatēšana programmā Word, izmantojot VBA

Automatizējot datu pārsūtīšanu no Excel uz Word, izmantojot VBA, ir ļoti svarīgi saprast, kā efektīvi pārvaldīt un formatēt tabulas. Viens no galvenajiem aspektiem ir nodrošināt pareizu datu pārsūtīšanu, saglabājot gan struktūru, gan lasāmību. Tam ir jāsaprot VBA komandas, kas kontrolē tabulas izveidi, formatēšanu un lappušu pārtraukumu ievietošanu. Piemēram, komanda Set wdTbl = wdDoc.Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol) tiek izmantots, lai Word dokumentam pievienotu jaunu tabulu, norādot rindu un kolonnu skaitu, pamatojoties uz Excel datiem.

Vēl viens svarīgs elements ir tabulas formatēšana. Komandas, piemēram, .Rows(1).Range.Font.Bold = True padariet pirmo rindu treknrakstā, norādot galvenes, kamēr wdTbl.Borders tiek izmantots, lai iestatītu apmales stilus gan tabulas iekšējām, gan ārējām rindām. Turklāt lappušu pārtraukumu ievietošana ir būtiska, lai nodrošinātu, ka katra tabula tiek parādīta atsevišķā lapā, kas tiek darīts, izmantojot wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak. Šīs komandas kopā nodrošina, ka gala dokuments ir labi sakārtots un profesionāli formatēts.

Bieži uzdotie jautājumi par VBA makro programmām Word un Excel

  1. Kā palaist jaunu Word lietojumprogrammu, izmantojot VBA?
  2. Izmantot Set wdApp = New Word.Application lai inicializētu jaunu Word lietojumprogrammas gadījumu.
  3. Kā es varu ievietot lappuses pārtraukumu Word dokumentā, izmantojot VBA?
  4. Ievietojiet lappuses pārtraukumu ar wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak.
  5. Kā es varu pievienot tabulu Word dokumentam, izmantojot VBA?
  6. Pievienojiet tabulu, izmantojot wdDoc.Tables.Add(Range:=wdDoc.Range, NumRows:=2, NumColumns:=lCol).
  7. Kā es varu formatēt tabulas pirmo rindu kā galveni?
  8. Iestatiet pirmo rindu kā galveni ar .Rows(1).HeadingFormat = True un padariet to drosmīgi, izmantojot .Rows(1).Range.Font.Bold = True.
  9. Kā es varu iestatīt apmales tabulai programmā Word, izmantojot VBA?
  10. Iestatiet apmales ar wdTbl.Borders, norādot stilus iekšējām un ārējām līnijām.
  11. Kā es varu padarīt Word programmu redzamu lietotājam VBA?
  12. Iestatiet redzamību ar wdApp.Visible = True.
  13. Kāda komanda tiek izmantota, lai pievienotu tabulai jaunu rindu, ja pašreizējā rinda pārsniedz esošo rindu skaitu?
  14. Pievienojiet jaunu rindu ar If (r - startRow + 2) > .Rows.Count Then .Rows.Add.
  15. Kā programmā Word izveidot jaunu dokumentu, izmantojot VBA?
  16. Izveidojiet jaunu dokumentu ar Set wdDoc = .Documents.Add.

Pēdējās domas

Vairāku Excel tabulu apvienošana vienā Word dokumentā, izmantojot VBA, racionalizē datu pārsūtīšanas un formatēšanas procesu. Automatizējot tabulas izveidi, formatēšanu un lappušu pārtraukumus, makro nodrošina, ka gala dokuments ir labi sakārtots un profesionāli noformēts. Šī pieeja ietaupa laiku un samazina kļūdu risku, padarot to par efektīvu risinājumu datu pārvaldībai un prezentēšanai dažādās platformās.