ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[改进建议] 建议增加一个拉动区域进行求和,并把求和数放进粘贴板的功能

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-4-23 11:47 | 显示全部楼层 |阅读模式
Sub Clipboard11()
On Error Resume Next
rngsum = 0
Dim a As New DataObject '声明新DataObject 类
Dim c As Range, rng As Range

If Selection.Count = 1 Then
Set rng = Selection
Else
Set rng = Selection.SpecialCells(xlCellTypeVisible)
End If

For Each c In rng
rngsum = rngsum + c
Next
'MsgBox rngsum
'a.SetText rngsum  '设定文本
'a.PutInClipboard  '送入剪贴板
rngsum = Round(rngsum, 2)
ClipBoard_SetData (rngsum)
End Sub




=================================


Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare PtrSafe Function CloseClipboard Lib "User32" () As LongPtr
Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Declare PtrSafe Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

Function ClipBoard_SetData(MyString As String)
   Dim hGlobalMemory As Long, lpGlobalMemory As Long
   Dim hClipMemory As Long, x As Long

   ' Allocate moveable global memory.
   '-------------------------------------------
   hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)

   ' Lock the block to get a far pointer
   ' to this memory.
   lpGlobalMemory = GlobalLock(hGlobalMemory)

   ' Copy the string to this global memory.
   lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

   ' Unlock the memory.
   If GlobalUnlock(hGlobalMemory) <> 0 Then
      MsgBox "Could not unlock memory location. Copy aborted."
      GoTo OutOfHere2
   End If

   ' Open the Clipboard to copy data to.
   If OpenClipboard(0&) = 0 Then
      MsgBox "Could not open the Clipboard. Copy aborted."
      Exit Function
   End If

   ' Clear the Clipboard.
   x = EmptyClipboard()

   ' Copy the data to the Clipboard.
   hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

OutOfHere2:

   If CloseClipboard() = 0 Then
      MsgBox "Could not close Clipboard."
   End If

End Function


TA的精华主题

TA的得分主题

 楼主| 发表于 2024-4-23 11:49 | 显示全部楼层
然后再设置一个快捷键,我目前自己用的加载宏设置的是Ctrl+a

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-4-23 13:34 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-4-24 08:07 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
非常感谢分享案例和代码,请问合计数为啥不直接用SUM公式?

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-4-24 17:49 | 显示全部楼层
本帖最后由 saimenl 于 2024-4-25 10:24 编辑
Kevin 发表于 2024-4-24 08:07
非常感谢分享案例和代码,请问合计数为啥不直接用SUM公式?

因为使用场景中,有可能求和不连续的单元格。例如下图的黄色单元格,常规操作是先按下Ctrl键选择,然后记下状态栏的求和数:70819.95。再填入另外一个报表中,这样容易记错数值。也不够快捷

TA的精华主题

TA的得分主题

发表于 2024-4-24 19:45 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-12-27 09:40 , Processed in 0.028326 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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