2017-06-01 61 views
0

我目前正在試圖製作一個Excel表格,以便從我們的實驗室獲取信息到一個文件並製作一張表格以適應我們需要表示的數據。問題在於實驗室向我們報告的劑量符合報告。我爲此需要合併A1:A2 .... Z1:Z2近50頁,所以即時嘗試使一個VBA循環該動作。問題是,我不能讓合併工作。我很新的VBA。 我的工作看起來像現在這樣:合併單元格頁面循環

 Sub WorksheetLoop() 
       Dim WS_Count As Integer 
       Dim I As Integer 
    Dim x as integer 
    Dim n as integer 

       WS_Count = ActiveWorkbook.Worksheets.Count 

       For I = 1 To WS_Count 
x=0 
     For n = 1 to 26 


         Range(Cells(1,x), Cells(1,1+x) Merge x=x+2 
Next N     
Next I 
       End Sub 

有人能與代碼的幫助,這將大大apreciated

回答

0

下面的代碼應該爲你工作。你只需要2個變量來執行任務,一個循環遍歷工作表,另一個循環遍歷每個表單中的列。

Sub WorksheetLoop() 

Application.ScreenUpdating = False 'Stops the screen from flickering when the code changes worksheets 

Dim ws As Worksheet 
Dim intColumn As Integer 

For Each ws In ThisWorkbook.Sheets 'This tells the code to loop through all of the worksheets in the current workbook without the need to count them 
    ws.Activate 'The worksheet needs to be activated for the Range function to run correctly. 
    For intColumn = 1 To 26 'For columns A to Z 
     ws.Range(Cells(1, intColumn), Cells(2, intColumn)).merge 'Select Row 1 and Row 2 of the column, then merge) 
    Next intColumn 'Moves to the next column 
Next ws 'Moves to the next worksheet 

Application.ScreenUpdating = True 'Always set ScreenUpdating back to True at the end of your code 

End Sub