ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: jsgj2023

[分享] 常用代码归集

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-9 22:20 | 显示全部楼层
Sub 批量对齐图片()
    Dim sh As Shape
    For Each sh In ActiveSheet.Shapes
        sh.Left = sh.TopLeftCell.Left
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-9 22:24 | 显示全部楼层
Sub 图片自动适应单元格()
Dim sh  As Shape
For Each sh In ActiveSheet.Shapes
    With sh
        .LockAspectRatio = False
        .Left = .TopLeftCell.Left
        .Top = .TopLeftCell.Top
        .Width = .TopLeftCell.Width
        .Height = .TopLeftCell.Height
    End With
Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 10:45 | 显示全部楼层
判断选中单元格区域的类型
Function areatype(rangearea As Range) As String
    If rangearea.Areas.Count > 1 Then
        Select Case True
            Case rangearea.Cells.Count = 1
            areatype = "Cell"
            Case rangearea.Cells.Count = Cells.Count
            areatype = "Worksheet"
            Case rangearea.Rows.Count = Cells.Rows.Count
            areatype = "Column"
            Case rangearea.Columns.Count = Cells.Columns.Count
            areatype = "Row"
            Case Else
            areatype = "Block"
        End Select
    Else
    End If
End Function

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 11:37 | 显示全部楼层
Sub 自动导出图表()
On Error Resume Next
Dim chartcount As Integer
Dim activechart As Integer
Dim fd As Object
Dim pathstr As String
Dim chartname As String
With ActiveSheet.ChartObjects
    chartcount = .Count
    If chartcount <= 1 Then Exit Sub
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd
        If .Show = -1 Then pathstr = .SelectedItems(1) Else Exit Sub
    End With
    If Right(pathstr, 1) <> "\" Then pathstr = pathstr & "\"
    For i = 1 To chartcount
        .Item(i).Chart.Export Filename:=pathstr & IIf(.Item(i).Chart.HasTitle, .Item(i).Chart.ChartTitle.Text, .Item(i).Chart.Name) & ".JPG", filtername:="JPG"
    Next i
End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 15:20 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

Sub 将指定的字符标示为上标()
Dim rng As Range
Dim i As Integer
Dim firststr As String
Dim inputt As String
inputt = InputBox("请输入需要显示为上标的字符", "指定字符", "#")
Application.ScreenUpdating = False
Set rng = Cells.Find(inputt, lookat:=xlPart, LookIn:=xlFormulas)
If Not rng Is Nothing Then
    first = rng.Address
    Do
        For i = 1 To Len(rng)
            If Mid$(rng, i, 1) = Left(inputt, 1) Then rng.Characters(Start:=i, Length:=1).Font.Subscript = True
        Next i
        Set rng = Cells.FindNext(rng)
    Loop Until rng.Address = first
End If
Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 15:32 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub evaluate筛选()
Dim rng As Range
Dim cell As Range
For Each rng In Range(Range("c2"), Cells(Rows.Count, 3).End(3))
    If Len(rng.Offset(0, 1)) > 0 Then
        If Evaluate("datedif(" & rng.Address & ", " & rng.Offset(0, 1).Address & ",""M""" & ")") < 12 Then
            If cell Is Nothing Then Set cell = rng Else Set cell = Application.Union(cell, rng)
        End If
    End If
Next
If Not cell Is Nothing Then cell.EntireRow.Hidden = True
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 16:03 | 显示全部楼层
Sub 集合法查找最后一次报价()
On Error Resume Next
Dim colt As New Collection
Dim i As Integer
Dim j As Integer
Dim arr As Variant
Dim arr2() As Variant
arr = Range(Range("a1"), Cells(Rows.Count, 3).End(3)).Value
For i = UBound(arr) To 1 Step -1
    colt.Add arr(i, 1), CStr(arr(i, 1))
    If Err = 0 Then
        j = j + 1
        ReDim Preserve arr2(1 To 3, 1 To j)
        arr2(1, j) = colt(j)
        arr2(2, j) = arr(i, 2)
        arr2(3, j) = arr(i, 3)
    End If
    Err.Clear
Next
Range("e1").Resize(j, 3) = Application.Transpose(arr2)
Range("g1").Resize.NumberFormat = "m月d日"
Range("h1").Resize(j, 1) = Evaluate("=row(1:" & j & ")")
Range("e1").Resize(j, 4).Sort key1:=[h1], order1:=xlDescending, ordercustom:=1, Orientation:=xlTopToBottom, dataoption1:=xlSortNormal
Range("h1").Resize(j, 1).Clear
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 16:07 | 显示全部楼层
Sub 字典法求第一次报价()
Dim arr()
On Error Resume Next
Set d = CreateObject("scripting.dictionary")
arr = Range("b1:c" & Cells(Rows.Count, 3).End(xlUp).Row)
For i = 1 To UBound(arr)
    d.Add arr(i, 1), arr(i, 2)
Next
[e1].Resize(d.Count) = Application.Transpose(d.keys)
[f1].Resize(d.Count) = Application.Transpose(d.items)
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 16:08 | 显示全部楼层
Sub 字典法求最后一次报价()
Dim arr()
Set d = CreateObject("scripting.dictionary")
arr = Range("b1:c" & Cells(Rows.Count, 3).End(xlUp).Row)
For i = 1 To UBound(arr)
    d(arr(i, 1)) = arr(i, 2)
Next
[i1].Resize(d.Count) = Application.Transpose(d.keys)
[j1].Resize(d.Count) = Application.Transpose(d.items)
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-2-10 16:11 | 显示全部楼层
Sub 多列合并计算_()
Dim arr As Variant
Dim arr1() As Variant
Dim d As Object
Dim i As Integer
Dim n As Integer
Set d = CreateObject("scripting.dictionary")
arr = Range("a2:d" & Cells(Rows.Count, 2).End(3).Row)
For i = 1 To UBound(arr)
     If Not d.exists(arr(i, 1)) Then
        n = n + 1
        d(arr(i, 1)) = n
        ReDim Preserve arr1(1 To 4, 1 To n)
        arr1(1, n) = arr(i, 1)
        arr1(2, n) = arr(i, 2)
        arr1(3, n) = arr(i, 3)
        arr1(4, n) = arr(i, 4)
    Else
        m = d(arr(i, 1))
        arr1(2, m) = arr1(2, m) + arr(i, 2)
        arr1(3, m) = arr1(3, m) + arr(i, 3)
        arr1(4, m) = arr1(4, m) + arr(i, 4)
     End If
Next
[f2].Resize(n, 4) = Application.Transpose(arr1)
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-10-6 04:18 , Processed in 0.038697 second(s), 5 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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