weixin_57163017 2022-04-18 14:46
浏览 5
已结题

请教vba问题,下面代码如何改进(标签-VBA|关键词-VBA)

我想提取我想要的列到新的工作表的指定区域,能否帮忙改进一下,我这个只能提取行。
Private Sub Worksheet_Activate()
Dim sh As Worksheet
Dim DeSh As Worksheet
Dim x As Integer
Dim StartRow As Long
Dim Last As Long
Dim shLast As Long
Dim firstblankrow As Long
Dim CopyRng As Range
On Error Resume Next

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Set DeSh = Me
Last = LastRow(DeSh)
If Last >= 1 Then
    Rows("3:" & Last).Delete
End If
Last = 1
For Each sh In Sheets(Array("1.c", "2.c", "3.c", 4.c"))
    firstblankrow = sh.Range("B1").End(xlDown).Row - 1
    StartRow = 2
    If firstblankrow > 0 And firstblankrow > StartRow Then
        For x = 10 To 20000
            If sh.Cells(x, 2) <> sh.Cells(x, 5) Then
            Set CopyRng = sh.Range(sh.Rows(x), sh.Rows(x))

            CopyRng.Copy
            With DeSh.Cells(Last + 1, "A")
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = xlCopy
                DeSh.Cells(Last + 1, LastColumn(DeSh)).Select
                DeSh.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & sh.Name & "'" & "!B1", TextToDisplay:=sh.Name
            End With
            Last = LastRow(DeSh)
            End If
        Next
    End If
Next

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True

ExitSub:
Application.Goto DeSh.Cells(1)
DestSh.Columns.AutoFit
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next

LastRow = sh.Cells.Find(what:="*", after:=sh.Range("A1"), lookat:=xlPart, LookIn:=xlFormulas, searchorder:=xlByRows, searchdirection:=xlPrevious, MatchCase:=False).Row

End Function

Function LastColumn(sh As Worksheet)
On Error Resume Next

LastColumn = sh.Cells.Find(what:="*", after:=sh.Range("A1"), lookat:=xlPart, LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious, MatchCase:=False).Column
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月26日
    • 创建了问题 4月18日

    悬赏问题

    • ¥100 华为手机私有App后台保活
    • ¥15 sqlserver中加密的密码字段查询问题
    • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
    • ¥20 我的这个coe文件到底哪儿出问题了
    • ¥15 matlab使用自定义函数时一直报错输入参数过多
    • ¥15 设计一个温度闭环控制系统
    • ¥100 rtmpose姿态评估
    • ¥15 通联支付网上收银统一下单接口
    • ¥15 angular有偿编写,
    • ¥15 centos7系统下abinit安装时make出错