LoginSignup
0
0

More than 1 year has passed since last update.

Excel VBA 選択した写真を指定セルに合わせてペースト

Last updated at Posted at 2021-08-24
Sub ChangeImageSizeBySelectCell()
Dim targetCellName As String
Dim targetRange As Range
Dim targetTop As Variant
Dim targetLeft As Variant
Dim targetHeight As Double
Dim targetWidth As Double
Dim objType As Object
Dim objName As String

On Error Resume Next
objName = Application.Selection.Name
If Not objName Like "Pic*" Then
MsgBox ("写真を選択して下さい。")
End
End If


targetCellName = InputBox("", "セルを選択")
    If checkCellValue(targetCellName) = False Then
        MsgBox "無効なセル範囲"
        End
    End If

Set targetRange = Range(targetCellName)
targetTop = targetRange.Top
targetLeft = targetRange.Left
targetWidth = targetRange.Width
targetHeight = targetRange.Height

  With Selection.ShapeRange
    .LockAspectRatio = msoFalse
    .Top = targetTop
    .Left = targetLeft
    .Width = targetWidth
    .Height = targetHeight
  End With
End Sub
Function checkCellValue(str As String) As Boolean
'要参照設定 Microsoft VBScript Regular Expressions 5.5
Dim reg As New regExp
  With reg
    .IgnoreCase = False
    .Pattern = "[A-Z]+[1-9]+\d*"
  End With
checkCellValue = reg.Test(str)
End Function

解説

ポイント 解説
RegExpオブジェクト VBAで正規表現を使用できる
RegExpオブジェクト Microsoft VBScript Regular Expressions 5.5を参照設定すること
.IgnoreCase = False 大文字小文字を区別する
[A-Z]+ A-Zを1回以上繰り返し
[1-9]+ 1-9を1回以上繰り返し
\d* d(=digit=0-9)を0回以上繰り返し

参考
https://excel-ubara.com/excelvba4/EXCEL232.html
https://www.officepro.jp/excelvba/sub/index6.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0