2013-05-29 88 views
2

對VBA來說是非常新穎的,但我們的客戶希望1,850頁的Word表格中的所有數據都對齊。我認爲在VBA中這很簡單。我試圖弄清楚,我確信我可以自己動手,但最後期限迫使我尋求幫助。所以如果我錯過了已發佈的解決方案,我會提前道歉。將Word表格中的特定單元格與VBA /宏對齊,VBA /宏

舉個例子,他們希望這樣的:

enter image description here

是這樣的:

enter image description here

所以我有:

Dim oTable As Table 
    Dim oRow As Row 

    For Each oTable In ActiveDocument.Tables 
    For Each oRow In oTable.Rows 

但我不不知道如何循環表格的正文。前4行(表格標題)也被合併到一個單元格中,並且第一列仍然左對齊。幫助和下一輪對我:)

+0

是你的表** **所有格式這樣嗎?或者他們會有各種格式,列,行等? – enderland

+0

行數和列數會有所不同,但通用格式是相同的。正確的方向任何一點將不勝感激。謝謝! –

回答

3

通常我不是一個「請爲我寫代碼」的巨大粉絲,但我沒有做足夠的Word中的VBA,並想學習一些自己。

這會讓你獲得大部分途徑。

您目前沒有提供足夠的信息來保證if聲明對於整個文件可行,但您應該可以從此處下載。


Sub alignTableElementsRight() 
    Dim oTable As Table 
    Dim oRow As Row 

    Dim i As Integer 
    Dim dataTable As Boolean 

    For Each oTable In ActiveDocument.Tables 
    'this will be set once you are in the "table" part and 
    'not headings 
    dataTable = False 

    For Each oRow In oTable.Rows 

     'you will need custom logic here to determine what your if statement 
     'is to properly execute on the right row, this is going to depend based on your table 
     'format, etc. This checks if a leftmost column heading is "65 to 66" 
     If (InStr(oRow.Cells(1).Range.Text, "65 to 66") > 0) Then 
     dataTable = True 
     End If 

     'if you are in the datatable, move all values to align right in each row following 
     If (dataTable = True) Then 
      For i = 2 To oRow.Cells.Count 
       oRow.Cells(i).Range.ParagraphFormat.Alignment = wdAlignParagraphRight 
      Next i 
     End If 


    Next oRow 

    Next oTable 
End Sub 
+0

是的,你在「請寫代碼」方面完全正確,我很抱歉地問。所以非常感謝,我希望你能從中搞清楚。再次感謝您讓我走上正軌! –