2017-07-11 30 views
1

有點背景,我有一個從多個excel文件中提取的vbs代碼,然後將它們粘貼到word文檔中以創建報告。一切工作就像它應該,但我需要能夠根據他們的組合行高來選擇一系列的行。原因是因爲我複製圖形並將其粘貼到單詞中作爲圖片,然後代碼打開另一個excel文件並選擇數據並將其粘貼到單詞中作爲該圖形下方的圖片,然後繼續循環直到它已經通過了所有的文件。如果使用的行的行距組合行高度大於此值,則可以放置8個正常大小的行(14.3)或圖形下方的總行高度114.4,那麼它需要轉到下一頁。選擇基於行總高度的行的範圍

現在我所看到的是代碼將查看使用的範圍,如果它大於8行,它將選擇使用的範圍並粘貼到下一頁,或者如果使用的範圍小於8,它將選擇即使它們是空的,也總共有8行,並粘貼它以保持整個報告中的統一。然而,有時候有少於8行數據的數據,但是必須增加一行或兩行以適應所有文本(列寬度必須被修正,因爲它必須適合於文檔文檔),因此使8行的總行高度大於114.4,並且它將不再適合在圖表下方。在這種情況下,我需要它根據114.4的組合行高而不僅僅是8行來進行選擇。

我希望我已經解釋了我需要做的更好的一些必要的幫助。以下是我目前試圖根據行高選擇數據的嘗試......它將在沒有錯誤的情況下通過代碼運行,但是它將剛剛脫離使用的範圍,我無法得到涉及行高的代碼任何事情,如果它只是忽略該代碼。

Sub Events(f) 
    Set objWkb3 = objExcel.Workbooks.Open(f) 
    LastRowEvents = objWkb3.Sheets("Events").usedRange.Rows.Count 
    LastRowEvents1 = objWkb3.Sheets("Events").usedRange.Rows 
    LastRowEvents2 = objWkb3.sheets("Events").usedRange.RowHeight 
    LastRowEvents3 = objWkb3.sheets("Events").Range("F1:F8").RowHeight 


    'Selection of Events 
    If LastRowEvents > 8 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture 
    objWord1.Selection.TypeParagraph 
    objWord2.Selection.TypeParagraph 
    Elseif LastRowEvents2 > 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture 
    objWord1.Selection.TypeParagraph 
    objWord2.Selection.TypeParagraph 
    Elseif LastRowEvents2 = 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture 
    Else objWkb3.Sheets("Events").Range("A1:F8").CopyPicture 
    End If 

    'Events to PJR 
    wsh.sleep 1500 
    objWord1.Selection.Paste 
    objWord1.Selection.MoveRight 
    objWord1.Selection.TypeParagraph 

    'Events to Gas 
    objWord2.Selection.Paste 
    objWord2.Selection.MoveRight 
    objWord2.Selection.TypeParagraph 

    'objWkb3.Save 
    objWkb3.Close(0) 
End Sub 
+0

它是如何工作的?你有錯誤嗎?有沒有意外的行爲?你也可以編輯你的第一段,所以它不僅僅是一段文字,它很難遵循。謝謝! – BruceWayne

+0

我編輯了原始文章更好地解釋了爲什麼它不工作。希望有所幫助。 –

回答

0

我一直在玩弄和嘗試不同的事物,通過改變.RowHeight到.Height我能得到的代碼工作,我是怎麼想。

Sub Events(f) 
    Set objWkb3 = objExcel.Workbooks.Open(f) 
    LastRowEvents = objWkb3.Sheets("Events").usedRange.Rows.Count 
    LastRowEvents1 = objWkb3.Sheets("Events").usedRange.Rows 
    LastRowEvents2 = objWkb3.sheets("Events").usedRange.Height 
    LastRowEvents3 = objWkb3.sheets("Events").Range("A1:F8").Height 
    MRow1 = Round(((114.4 - LastRowEvents2)/14.3),0) 
    MRowT = MRow1 + LastRowEvents 

    'Selection of Events 
    If LastRowEvents > 8 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture 
    objWord1.Selection.TypeParagraph 
    objWord2.Selection.TypeParagraph 
    Elseif LastRowEvents2 > 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture 
    objWord1.Selection.TypeParagraph 
    objWord2.Selection.TypeParagraph 
    Elseif LastRowEvents2 = 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture 
    Elseif LastRowEvents3 > 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & MRowT).CopyPicture 
    Elseif LastRowEvents3 = 114.4 then 
    objWkb3.Sheets("Events").Range("A1:F" & LastRowEvents).CopyPicture 
    Else objWkb3.Sheets("Events").Range("A1:F8").CopyPicture 
    End If 

    'Events to PJR 
    wsh.sleep 500 
    objWord1.Selection.Paste 
    objWord1.Selection.MoveRight 
    objWord1.Selection.TypeParagraph 

    'Events to Gas 
    objWord2.Selection.Paste 
    objWord2.Selection.MoveRight 
    objWord2.Selection.TypeParagraph 

    'objWkb3.Save 
    objWkb3.Close(0) 
End Sub