SYSTEM DEVELOPMENT

STUDY GROUP

Excel VBA Part 2

ダブルクリックで違うシートにコピー Part 2

今回はシート1のA列からC列のデータをシート2のB2から横方向にE列まで転記するVBAを考えてみました。
実際の業務で使用し、↓これだけですが、時間短縮することもできました。感動です💧
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim sh1 As Range
Dim sh2 As Range
Dim r As Range
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Set sh1 = Me.Range(“A:C”)
Set sh2 = Worksheets(“リスト”).Range(“B2:E” & lr)
If Not Intersect(Target, sh1) Is Nothing Then
If Application.CountA(sh2) = sh2.Cells.Count Then
MsgBox “転記できません”, vbInformation
Else
Set r = sh2.Cells(1)
On Error Resume Next
Set r = sh2.SpecialCells(xlCellTypeBlanks).Cells(1)
On Error GoTo 0
r.Value = Target.Value
End If Cancel = True End If
Set sh1 = Nothing
Set sh2 = Nothing
End Sub
 

menu