【发布时间】:2017-05-12 17:58:03
【问题描述】:
我有一个包含 3000 列的 Excel 工作表,我需要转换此工作表,使一个选项卡仅包含 254 列,其余的将转到下一个选项卡。所以我需要一个可以执行相同功能的 VBA 代码(宏)。
到目前为止,我只编写了以下代码,它创建了 3000 个选项卡,每个选项卡有一列,而且由于我没有为空白列设置任何条件,所以它也将进入无限循环。
Sub SpliteIntoMultipleTab()
'
' createtemplates Macro
Dim WS As Worksheet
Dim SS As Worksheet
Dim TemplateName As String
Dim tempstr As String
'
Dim CurCol As String
Dim Template As String
Dim xColIndex As Integer
Dim xRowIndex As Integer
Dim WSCount As Integer
'==========================================================================
'Declarations
CurCol = 1
Template = "Sheet1"
'==========================================================================
Set SS = Worksheets(Template)
If WS Is Nothing Then
Start:
With ActiveWorkbook
Set WS = .Sheets.Add(After:=ActiveSheet)
WSCount = Sheets.Add(After:=Sheets(Worksheets.Count))
On Error Resume Next
Set WS = Worksheets("temp")
WS.Name = SS.Range("A1").Value
End With
Else
End If
SS.Activate
xIndex = Application.ActiveCell.Column
xRowIndex = Application.ActiveSheet.Cells(Rows.Count, xIndex).End(xlUp).Row
Range(Cells(1, xIndex), Cells(xRowIndex, xIndex)).Select
Selection.Copy
WS.Select
WS.Range("A1").Select
ActiveSheet.Paste
SS.Columns(1).EntireColumn.Delete
CurCol = CurCol + 1
GoTo Start
End Sub
【问题讨论】:
-
你拥有的和你想要的有什么区别?
标签: sql-server vba excel macros