EXCEL VBA鼠标动作问题,请高手来帮忙!
第一个点击的单元格是日期格式,想将它的格式变为:“DDMMM”,后面再加上一个“/”,一同与点击的第二个单元格中的内容合并,并复制到剪切板
比如
A1=6月6日,B1=AAA按顺序单击A1和B1后,将”6jun/AAA“复制到剪切板中? 展开
有点意思,做了个
Option Explicit
Dim strPre, s As String
Dim lastrange As String
Dim lCount As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'第一次改变所选的单元格
If strPre <> ThisWorkbook.str And Not (lCount > 0) Then
lastrange = ThisWorkbook.str
Else
lastrange = strPre
End If
GetPreAddress Target.Address
If Target.Column = 2 Then
If lastrange = Target.Offset(0, -1).Address Then s = Format(Target.Offset(0, -1), "ddmmm") & "/" & Target
End If
With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
.SetText s
.PutInClipboard
End With
End Sub
Sub GetPreAddress(strPreA As String)
strPre = strPreA
lCount = lCount + 1
End Sub
Thisworkbook:
Option Explicit
Public str As String
Private Sub Workbook_Open()
str = ActiveCell.Address
End Sub
录制宏
随便录制个宏,主要是设置快捷键
修改宏代码实现上面基本功能
每次选择好之后,按快捷键实现
这个是比较方便的了
录制好后,把下面代码拷贝到函数内部即可
Dim cel As Range, tempStr As String
Dim i As Integer
i = 1
For Each cel In Selection
If i = 1 Then tempStr = tempStr & Format(cel, "ddmmm")
If i = 2 Then tempStr = tempStr & "/" & cel
i = i + 1
Next
首先,非常感谢您的回答帮助!
另,我需要的是鼠标点击后,自动将结束复制到剪切板,并不是新建一个模块手动运行宏。
我认为应将代码写在SHEET名下。
我是一个初级VBA新手,还请大侠帮忙,谢谢!
2016-06-28
比如
A1=6月6日,B1=AAA按顺序单击A1和B1后,将”6jun/AAA“复制到剪切板中?
如何编写?
Alt+F11打开VBA编辑器
输入下面的代码
Dim rng As Range '用于记录上一次点击单元格
Dim strA
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count = 1 Then
If Target.Column = 1 Then
Set rng = Target
strA = Day(Target.Value) & Choose(Month(Target.Value), "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
Else
If Target.Column = 2 Then
If rng.Row = Target.Row Then
'Target.Offset(0, -1).NumberFormatLocal = "ddmmm"
Target.Offset(0, -1) = strA & "/" & Target
Target.ClearContents
Range(rng, Target).Select
Selection.Merge
Selection.Copy
End If
End If
End If
End If
End Sub
说明:只有在之前点同行A列,并且接着点同行B列时才能实现。