PDA

View Full Version : [SOLVED:] VBA newcomer - help with IF to determine whether to run code



CharlieG
12-20-2019, 08:15 AM
Hi everyone,

forgive such a basic question but this is the first time have been confronted by VBA and need to demonstrate to colleagues that even simple modules can help!

have the following code:


Sub CgSign()
'
' CgSign Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
'
If ThisWorkbook.ActiveSheet = 2 Then

ActiveSheet.Pictures.Insert("C:\Users\charlieg\Documents\cg signature.tif"). _
Select
Selection.ShapeRange.ScaleWidth 0.4, msoFalse, msoScaleFromBottomRight
Selection.ShapeRange.ScaleHeight 0.4, msoFalse, msoScaleFromTopLeft
Selection.Left = ActiveSheet.Range("R4").Left + 10
Selection.Top = ActiveSheet.Range("R4").Top + 10
'End If

End Sub

Workbook has 2 sheets - "Contract review" and "Jobcard review"

have got the code sorted to get the image into the correct place on the Jobcard review sheet but need the code to first check which sheet is active and then determine whether to use the code for the jobcard sheet or to switch to a code section to apply it to the contract review sheet instead.

Please could anyone correct the IF statement in the code as it errors and am way too new to have got to grips with details beyond recording a simple macro!

thank you!!

Bob Phillips
12-20-2019, 10:14 AM
Sub CgSign()
Dim img As Object

With ActiveSheet

If .Name = "Jobcard Review" Then

Set img = .Pictures.Insert("C:\Users\Bob\Pictures\AllFormulas.png") '"C:\Users\charlieg\Documents\cg signature.tif")
With img

.ShapeRange.ScaleWidth 0.4, msoFalse, msoScaleFromBottomRight
.ShapeRange.ScaleHeight 0.4, msoFalse, msoScaleFromTopLeft
.Left = ActiveSheet.Range("R4").Left + 10
.Top = ActiveSheet.Range("R4").Top + 10
End With
End If
End With
End Sub

CharlieG
12-28-2019, 07:54 AM
thank you xld - really appreciate your help and got it all working fine now.