Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1616|回复: 14

[已解决]vba中合并的单元格如何选中

[复制链接]
发表于 2022-8-9 09:38 | 显示全部楼层 |阅读模式
3学分
本帖最后由 Quincy_xiao 于 2022-8-9 10:32 编辑

麻烦各位老师看看,附件中的三个地方需要怎么样实现,自己写过,选中合并的单元格时会出现问题。
最佳答案
2022-8-9 09:38
本帖最后由 我行我速2008 于 2022-8-9 11:47 编辑

认为行就选为最佳答案
Dim R%, I%, Str$, K%
Sub 依次获取D列合并单元格的值()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 4).End(xlUp).Row
            If .Cells(R, 4).MergeCells Then
                I = .Cells(R, 4).MergeArea.Count
                MsgBox .Cells(R, 4).Text
                R = R + I - 1
            End If
        Next R
    End With
End Sub
Sub 根据F列合并单元格获取I列对应数据()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 6).End(xlUp).Row
            If .Cells(R, 6).MergeCells Then
                I = .Cells(R, 6).MergeArea.Count
                For K = R To R + I - 1
                    If .Cells(K, 9) <> "" Then Str = Str & .Cells(K, 9) & ","
                Next K
                If Len(Str) > 0 Then
                    MsgBox .Cells(R, 6).Resize(I, 6).Address & "的I列对应数据是:" & Str
                Else
                    MsgBox .Cells(R, 6).Resize(I, 6).Address & "的I列对应数据为空"
                End If
                Str = ""
                R = R + I - 1
            End If
        Next R
    End With
End Sub

Sub 选中D列所有合并单元格()
    Dim R%, I%, Str$
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 4).End(xlUp).Row
            If .Cells(R, 4).MergeCells Then
                I = .Cells(R, 4).MergeArea.Count
                Str = Str & .Cells(R, 4).Address(0, 0) & ","
                R = R + I - 1
            End If
        Next R
        .Range(Left(Str, Len(Str) - 1)).Select
    End With
    MsgBox Selection.Address
End Sub


Book1.zip

13 KB, 下载次数: 5

最佳答案

查看完整内容

认为行就选为最佳答案 Dim R%, I%, Str$, K% Sub 依次获取D列合并单元格的值() With Sheets("project") For R = 4 To .Cells(Rows.Count, 4).End(xlUp).Row If .Cells(R, 4).MergeCells Then I = .Cells(R, 4).MergeArea.Count MsgBox .Cells(R, 4).Text R = R + I - 1 End If Next R End With End Sub Sub 根据F ...
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2022-8-9 09:38 | 显示全部楼层    本楼为最佳答案   
本帖最后由 我行我速2008 于 2022-8-9 11:47 编辑

认为行就选为最佳答案
Dim R%, I%, Str$, K%
Sub 依次获取D列合并单元格的值()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 4).End(xlUp).Row
            If .Cells(R, 4).MergeCells Then
                I = .Cells(R, 4).MergeArea.Count
                MsgBox .Cells(R, 4).Text
                R = R + I - 1
            End If
        Next R
    End With
End Sub
Sub 根据F列合并单元格获取I列对应数据()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 6).End(xlUp).Row
            If .Cells(R, 6).MergeCells Then
                I = .Cells(R, 6).MergeArea.Count
                For K = R To R + I - 1
                    If .Cells(K, 9) <> "" Then Str = Str & .Cells(K, 9) & ","
                Next K
                If Len(Str) > 0 Then
                    MsgBox .Cells(R, 6).Resize(I, 6).Address & "的I列对应数据是:" & Str
                Else
                    MsgBox .Cells(R, 6).Resize(I, 6).Address & "的I列对应数据为空"
                End If
                Str = ""
                R = R + I - 1
            End If
        Next R
    End With
End Sub

Sub 选中D列所有合并单元格()
    Dim R%, I%, Str$
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 4).End(xlUp).Row
            If .Cells(R, 4).MergeCells Then
                I = .Cells(R, 4).MergeArea.Count
                Str = Str & .Cells(R, 4).Address(0, 0) & ","
                R = R + I - 1
            End If
        Next R
        .Range(Left(Str, Len(Str) - 1)).Select
    End With
    MsgBox Selection.Address
End Sub


合并单元格操作(20220809).rar

19.88 KB, 下载次数: 4

回复

使用道具 举报

发表于 2022-8-9 10:08 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2022-8-9 10:33 | 显示全部楼层
我行我速2008 发表于 2022-8-9 10:08
附件呢??????

老师好,我的错,明明上传了,切换悬赏后未点击使用。已更新
回复

使用道具 举报

发表于 2022-8-9 11:08 | 显示全部楼层
本帖最后由 我行我速2008 于 2022-8-9 11:28 编辑

先写段依次选取D列合并单元格的代码。

Sub 依次选取D列合并单元格()
    Dim R%, I%, Str$
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 4).End(xlUp).Row
            If .Cells(R, 4).MergeCells Then I = .Cells(R, 4).MergeArea.Count
            R = R + I - 1
            .Cells(R, 4).Select
            MsgBox Selection.Address
        Next R
    End With
End Sub
回复

使用道具 举报

发表于 2022-8-9 11:23 | 显示全部楼层
本帖最后由 我行我速2008 于 2022-8-9 11:34 编辑

再来段:

Sub 根据F列合并单元格获取I列对应数据()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 6).End(xlUp).Row
            If .Cells(R, 6).MergeCells Then
                I = .Cells(R, 6).MergeArea.Count
                For K = R To R + I - 1
                    If .Cells(K, 9) <> "" Then Str = Str & .Cells(K, 9) & ","
                Next K
                If Len(Str) > 0 Then
                    MsgBox .Cells(R, 6).Resize(I, 6).Address & "的I列对应数据是:" & Str
                Else
                    MsgBox .Cells(R, 6).Resize(I, 6).Address & "的I列对应数据为空"
                End If
                Str = ""
                R = R + I - 1
            End If
        Next R
    End With
End Sub
回复

使用道具 举报

发表于 2022-8-9 11:28 | 显示全部楼层
这里的格子数会增加,怎么动态知道某个project有多个行

这个问题参见4楼的变量I
回复

使用道具 举报

 楼主| 发表于 2022-8-9 11:33 | 显示全部楼层
我行我速2008 发表于 2022-8-9 11:08
先写段依次选取D列合并单元格的代码。

Sub 依次选取D列合并单元格()

那请问该如何获取合并的单元格里边的值呢?
回复

使用道具 举报

发表于 2022-8-9 11:36 | 显示全部楼层
Quincy_xiao 发表于 2022-8-9 11:33
那请问该如何获取合并的单元格里边的值呢?

Sub 依次获取D列合并单元格的值()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 4).End(xlUp).Row
            If .Cells(R, 4).MergeCells Then I = .Cells(R, 4).MergeArea.Count
            MsgBox .Cells(R, 4).Text
            R = R + I - 1
        Next R
    End With
End Sub
回复

使用道具 举报

 楼主| 发表于 2022-8-9 11:48 | 显示全部楼层
我行我速2008 发表于 2022-8-9 11:36
Sub 依次获取D列合并单元格的值()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Coun ...

我将它改为了F列,存在了数组里
Sub GetFcolumn()
    Dim R%, I%, Str$
    Dim arr()
    Dim arr1()
    With Sheets("project")
        For R = 4 To .Cells(Rows.Count, 6).End(xlUp).Row
            ReDim Preserve arr(1 To .Cells(Rows.Count, 6).End(xlUp).Row + 3)
            If .Cells(R, 6).MergeCells Then
                I = .Cells(R, 6).MergeArea.Count
                arr(R) = .Cells(R, 6).Text
                R = R + I - 1
            Else
                .Cells(R, 6).Select
                arr(R) = .Cells(R, 6).Text
            End If
        Next R
    End With
End Sub



现在我想将前面的循环放置,应该根据您的代码能够实现了,先拿到F列的内容行数x,然后放置x个project名称。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 15:49 , Processed in 0.348030 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表