Word VBA -makroer - Tabeller: Tilføj, Vælg, Loop, Indsæt fra Excel

Tilføj tabel til Word -dokument

Denne enkle makro tilføjer en tabel til dit Word -dokument:

Sub VerySimpleTableAdd () Dim oTable As Table Set oTable = ActiveDocument.Tables.Add (Range: = Selection.Range, NumRows: = 3, NumColumns: = 3) End Sub

Vælg Tabel i Word

Denne makro vælger den første tabel i det aktive Word -dokument:

Sub SelectTable () 'vælger første tabel i aktiv doc Hvis ActiveDocument.Tables.Count> 0 Then' for at undgå fejl kontrollerer vi, om der findes en tabel i aktiv doc ActiveDocument.Tables (1) .Vælg End If End Sub

Sløjfe igennem alle celler i en tabel

Denne VBA -makro går gennem alle celler i en tabel og skriver celletallet til cellen:

Sub TableCycling () 'loop gennem alle celler i tabel Dim nCounter As Long' vil dette blive skrevet i alle tabelceller Dim oTable As Table Dim oRow As Row Dim oCell As Cell ActiveDocument.Range.InsertParagraphAfter 'just makes new para athe end of doc , Tabellen vil blive oprettet her Sæt oTable = ActiveDocument.Tables.Add (Range: = ActiveDocument.Paragraphs.Last.Range, NumRows: = 3, NumColumns: = 3) 'opret tabel og tildel den til variabel For hver oRow In tabelle. Rækkes ydre sløjfe går gennem rækker For hver oCell I oRow.Cells 'indre sløjfe går nCounter = nCounter + 1' øger tælleren oCell.Range.Text = nCounter 'skriver tæller til cellen Næste oCell Næste oRow' displayresultat fra celle fra anden kolonne i anden række Dim strTemp As String strTemp = oTable.Cell (2, 2) .Range.Text MsgBox strTemp End Sub

Opret Word -tabel fra Excel -fil

Dette VBA -eksempel vil lave en tabel fra en Excel -fil:

Sub MakeTablefromExcelFile () 'advanced Dim oExcelApp, oExcelWorkbook, oExcelWorksheet, oExcelRange Dim nNumOfRows As Long Dim nNumOfCols As Long Dim strFile As String Dim oTable As Table' word table Dim oRow As Row 'word row Dim oCell As Cell' word As Long, y As Long 'tæller for sløjfer strFile = "c: \ Users \ Nenad \ Desktop \ BookSample.xlsx"' ændring til den faktiske sti Sæt oExcelApp = CreateObject ("Excel.Application") oExcelApp.Visible = True Set oExcelWorkbook = oExcelApp.Workbooks.Open (strFile) 'åben projektmappe og tildel den til variabel Indstil oExcelWorksheet = oExcelWorkbook.Worksheets (1)' tildel første regneark til variabel Indstil oExcelRange = oExcelWorksheet.Range ("A1: C8") nNumOfRows = oExcel. Tæl nNumOfCols = oExcelRange.Columns.Count ActiveDocument.Range.InsertParagraphAfter 'bare laver en ny del af enden af ​​doc, vil tabellen blive oprettet her Set oTable = ActiveDocument.Tables.Add (Range: = ActiveDocument.Paragraphs.Last.Range, NumRows: = nNumOfRows, NumColumns: = nNumOfCols) 'creat e tabellen og tildel den til variablen '*** real deal, tabellen bliver udfyldt her For x = 1 Til nNumOfRows For y = 1 Til nNumOfCols oTable.Cell (x, y) .Range.Text = oExcelRange.Cells (x, y ) .Værdi Næste y Næste x '*** oExcelWorkbook.Close False oExcelApp.Quit With oTable.Rows (1) .Range' we can now apply some beautiness to our table :) .Shading.Texture = wdTextureNone .Shading.ForegroundPatternColor = wdColorAutomatic .Shading.BackgroundPatternColor = wdColorYellow End With End Sub

Du vil bidrage til udviklingen af ​​hjemmesiden, at dele siden med dine venner

wave wave wave wave wave