我需要一些幫助來縮短這段代碼。程序過大VBA excel
我需要使用此代碼If (linha >= 20 And linha <= 21)
50線(linha)間隔
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim foto As Range
Dim destino As Range
Dim linha As Long
Dim fName As String
Dim pName As String
Dim iName As String
Dim iNameClean As String
Dim iNameExcel As String
Dim fNameExcel As String
Set foto = Target.Cells(1)
Set destino = Me.Range("AU:BC,BN:BV,CG:CO,CZ:DH,DS:EA,EL:ET,FE:FM,FX:GF,GQ:GY,HJ:HR,IC:IK,IV:JD,JO:JW,KH:KP,NF:NN,NY:OG,OR:OZ,PK:PS")
If Not Application.Intersect(foto, destino) Is Nothing Then
linha = foto.Row
If (linha >= 20 And linha <= 21) Then
With ActiveSheet
fName = Application.GetOpenFilename("Picture files (*.jpg;*.gif;*.bmp;*.tif), *.jpgs;*.gif;*.bmp;*.tif", , _
"Select picture to insert")
iName = Dir("" & fName & "")
If fName = "False" Then Exit Sub
iNameClean = Left(iName, Len(iName) - 4)
iNameExcel = "+Info"
fNameExcel = "F:\path\EXCEL\" & foto.Offset(1, 3).Value & ".xlsx"
With ActiveSheet
.Unprotect Password:="1234"
ActiveSheet.Pictures.Insert(fName).Select
foto.Offset(0, 2).Formula = "=HYPERLINK(""" & fName & """,""" & iNameClean & """)"
foto.Offset(0, 2).Font.ColorIndex = 1 ' preto
foto.Offset(0, 2).Font.Size = 9
foto.Offset(0, 2).Font.Underline = False
foto.Offset(0, 3).Formula = "=HYPERLINK(""" & fNameExcel & """,""" & iNameExcel & """)"
foto.Offset(0, 3).Font.ColorIndex = 1 ' preto
foto.Offset(0, 3).Font.Size = 9
foto.Offset(0, 3).Font.Underline = False
With Selection.ShapeRange
.LockAspectRatio = msoFalse
.Height = ActiveCell.MergeArea.Height
.Width = ActiveCell.MergeArea.Width
.Top = ActiveCell.Top
.Left = ActiveCell.Left
End With
.Protect Password:="1234"
End With
End With
End If
End Sub
如果你能[編輯]標題簡潔地解釋*這段代碼的功能*,並擴大了一下關於這個問題的身體周圍的背景下,這將是[codereview.se]一個完美的問題。就目前來看,對於Stack Overflow的主題來說,這個問題有點過於寬泛。 –
爲什麼你需要縮短你的代碼?如果你得到錯誤「程序太大」,然後把它分解成幾個程序。如果超出模塊尺寸,則將代碼分佈在多個模塊中。 – Ralph
@Ralph如果你得到的「程序太大」的錯誤,你有更大的問題,並需要讀一點[SRP](https://en.wikipedia.org/wiki/Single_responsibility_principle);-) –