R
RemyMaza
Dave Peterson helped me a bunch with this code previously so cheers to
him. Now I need some more insight with this same code. My values
that I was searching for initially were hard coded but the code needs
to be a little more robust. What I have now is prefixes for these
values that I'd like to look past and get to the value that doesn't
change. i.e. ABC-1 TestCell could be ABC-2 TestCell or MC1 TestCell
could be I1 TestCell. Is there an easy way to search for the text
that doesn't change or will I have to create variable for each prefix
and reference them in? FYI, The prefixes do change in length, so some
of them in the range are 4 characters and some of them may only be 1
character.
Code Begins
******************************************************************************
Sub Button1_Click()
ActiveSheet.Unprotect
Dim myPath As String
Dim myRng As Range
Dim myCell As Range
myPath = "mypath"
With Worksheets("Sheet1")
Set myRng = .Range("E1", .Cells(.Rows.Count, "E").End(xlUp))
For Each myCell In myRng.Cells
Select Case LCase(myCell.Value)
'For Pic #1
Case LCase("TestCell") ' This is what I'd like to search
for within the cell value even though it's not the whole value
InsertPicture myPath & "\" & "1.jpg", _
myCell.Offset(0, -1), True, True
'End Pic #1, Start Pic #2
Case LCase("TestCell2") ' This is what I'd like to
search for within the cell value even though it's not the whole value
InsertPicture myPath & "\" & "2.jpg", _
myCell.Offset(0, -1), True, True
Case Else
End Select
Next myCell
End With
ActiveSheet.Protect
End Sub
Sub InsertPicture(PictureFileName As String, TargetCell As Range, _
CenterH As Boolean, CenterV As Boolean)
' inserts a picture at the top left position of TargetCell
' the picture can be centered horizontally and/or vertically
' http://www.exceltip.com/st/Insert_pictures_using_VBA_in_Microsoft_Excel/486.html
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(TargetCell.Parent) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = TargetCell.Parent.Pictures.Insert(PictureFileName)
' determine positions
With TargetCell
t = .Top
l = .Left
End With
' position picture
With p
.Top = t
.Left = l
End With
Set p = Nothing
End Sub
*****************************************************************************************
End Code
Thanks for any suggestions!
Regards,
Matt
him. Now I need some more insight with this same code. My values
that I was searching for initially were hard coded but the code needs
to be a little more robust. What I have now is prefixes for these
values that I'd like to look past and get to the value that doesn't
change. i.e. ABC-1 TestCell could be ABC-2 TestCell or MC1 TestCell
could be I1 TestCell. Is there an easy way to search for the text
that doesn't change or will I have to create variable for each prefix
and reference them in? FYI, The prefixes do change in length, so some
of them in the range are 4 characters and some of them may only be 1
character.
Code Begins
******************************************************************************
Sub Button1_Click()
ActiveSheet.Unprotect
Dim myPath As String
Dim myRng As Range
Dim myCell As Range
myPath = "mypath"
With Worksheets("Sheet1")
Set myRng = .Range("E1", .Cells(.Rows.Count, "E").End(xlUp))
For Each myCell In myRng.Cells
Select Case LCase(myCell.Value)
'For Pic #1
Case LCase("TestCell") ' This is what I'd like to search
for within the cell value even though it's not the whole value
InsertPicture myPath & "\" & "1.jpg", _
myCell.Offset(0, -1), True, True
'End Pic #1, Start Pic #2
Case LCase("TestCell2") ' This is what I'd like to
search for within the cell value even though it's not the whole value
InsertPicture myPath & "\" & "2.jpg", _
myCell.Offset(0, -1), True, True
Case Else
End Select
Next myCell
End With
ActiveSheet.Protect
End Sub
Sub InsertPicture(PictureFileName As String, TargetCell As Range, _
CenterH As Boolean, CenterV As Boolean)
' inserts a picture at the top left position of TargetCell
' the picture can be centered horizontally and/or vertically
' http://www.exceltip.com/st/Insert_pictures_using_VBA_in_Microsoft_Excel/486.html
Dim p As Object, t As Double, l As Double, w As Double, h As Double
If TypeName(TargetCell.Parent) <> "Worksheet" Then Exit Sub
If Dir(PictureFileName) = "" Then Exit Sub
' import picture
Set p = TargetCell.Parent.Pictures.Insert(PictureFileName)
' determine positions
With TargetCell
t = .Top
l = .Left
End With
' position picture
With p
.Top = t
.Left = l
End With
Set p = Nothing
End Sub
*****************************************************************************************
End Code
Thanks for any suggestions!
Regards,
Matt