使用VBA按列拆分工作表到多个工作表 [英] Split worksheet into multiple worksheets by column using VBA

查看:159
本文介绍了使用VBA按列拆分工作表到多个工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用特定列将工作表拆分为多个工作表,根据受影响的单元格中的数据命名每个工作表。 我找到了对类似问题的回复,但是当我按照步骤操作时,它只拆分了前几行
行,并且在第一行之后错误地命名了新工作表。 这是我复制使用的VBA代码:

  Sub   SplitData  ()  
Const lngNameCol = 2 '第二栏中的名字(B)
Const lngFirstRow = 2 '数据从第2行开始
Dim wshSource As 工作表
Dim wshTarget 作为 工作表
Dim lngRow As
昏暗 lngLastRow 作为
Dim lngTargetRow 作为
申请
< span class ="pun">。 ScreenUpdating = False
设置 wshSource = ActiveSheet
lngLastRow
= < span class ="pln"> wshSource 单元格 wshSource < span class ="pln">行 计数 lngNameCol )。 结束 xlUp )。 Row
对于 lngRow = lngFirstRow lngLastRow
如果 wshSource 单元格 lngRow lngNameCol )。 <> wshSource 单元格 lngRow - 1 lngNameCol )。 然后
设置 wshTarget = 工作表 添加 := 工作表 工作表 计数 ))
wshTarget
名称 = wshSource 单元格 lngRow lngNameCol )。 价值
wshSource
lngFirstRow - 1 )。 复制目的地 := wshTarget 单元格 1 1
lngTargetRow
= 2
结束 如果
wshSource
< span class ="pln">行 lngRow )。 复制目的地 := wshTarget 单元格 lngTargetRow 1
lngTargetRow
= lngTargetRow + 1
下一步 lngRow
应用程序
ScreenUpdating = Tru e
结束 Sub

在我的工作表中,我需要按第6列(F)中的数据排序,这些是日期。 我尝试将第一行更改为:

    Const   lngNameCol   =     6    '第六列(F)中的名称 

但这不起作用。 还有更多需要改变的地方吗?


解决方案





我想你想根据日期(F列)将数据(行)分成其他工作表。

如果这是正确的,请通过云存储分享你的excel文件,如Onedrive,DropBox等。我将制作VBA代码。

(请记住在分享前隐藏或更改您的重要数据)



问候,

I am trying to split a worksheet into multiple sheets using a specific column, naming each worksheet according to the data in the cells affected.  I found a response to a similar question, but when I follow the steps it only splits out the first few rows, and it misnames the new worksheets after the first one.  Here is the VBA code I copied to use:

Sub SplitData()
    Const lngNameCol = 2 ' names in second column (B)
    Const lngFirstRow = 2 ' data start in row 2
    Dim wshSource As Worksheet
    Dim wshTarget As Worksheet
    Dim lngRow As Long
    Dim lngLastRow As Long
    Dim lngTargetRow As Long
    Application.ScreenUpdating = False
    Set wshSource = ActiveSheet
    lngLastRow = wshSource.Cells(wshSource.Rows.Count, lngNameCol).End(xlUp).Row
    For lngRow = lngFirstRow To lngLastRow
        If wshSource.Cells(lngRow, lngNameCol).Value <> wshSource.Cells(lngRow - 1, lngNameCol).Value Then
            Set wshTarget = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            wshTarget.Name = wshSource.Cells(lngRow, lngNameCol).Value
            wshSource.Rows(lngFirstRow - 1).Copy Destination:=wshTarget.Cells(1, 1)
            lngTargetRow = 2
        End If
        wshSource.Rows(lngRow).Copy Destination:=wshTarget.Cells(lngTargetRow, 1)
        lngTargetRow = lngTargetRow + 1
    Next lngRow
    Application.ScreenUpdating = True
End Sub

In my worksheet, I need to sort by the data in column six, (F), which are dates.  I have tried changing the first line to read:

 Const lngNameCol = 6 ' names in sixth column (F)

But this is not working.  Is there more that needs to be changed?

解决方案

Hi,

I guess you want to split data (rows) according to date (column F) into other worksheets.
If this is correct, please share your excel file via cloud storage such as Onedrive, DropBox, etc. and I'll make VBA code.
(Please remember to hide or change your vital data before sharing)

Regards,


这篇关于使用VBA按列拆分工作表到多个工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆