我目前正在尝试制作一个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
有人可以帮助代码,这将是非常令人遗憾的
答案 0 :(得分: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