How to modify two codes into one?

Mötley

Board Regular
Joined
Aug 3, 2011
Messages
93
Hello, I need help with this. I found a thread from previous posts, and it has a code that's doing what I want, and it's working perfect. Taking multiple pictures and inserting them one below.

There's only one problem I need solution. Code has a default-folder selected for pictures, "C:\etc.", I need modify this code so that it opens me a browser where to select right folder, because my default-folder can be anything else.

Here's the code I found

Code:
Sub 123()
    InsertAllPix Range("A50"), _
                "C:\Documents and Settings\123\My Pictures\My Directory", _
                "*.jpg"
End Sub
 
Sub InsertAllPix(r As Range, ByVal sDir As String, sFilt As String)
    Dim **** As String
    Dim iRow As Long
 
    If Right(sDir, 1) <> "\" Then sDir = sDir & "\"
    **** = Dir(sDir & sFilt)
 
    Do While Len(****)
        iRow = iRow + 1
        With ActiveSheet.Pictures.Insert(sDir & ****)
            .ShapeRange.LockAspectRatio = msoFalse
            .Height = r(iRow, 1).Height
            .Width = r(iRow, 1).Width
            .Top = r(iRow, 1).Top
            .Left = r(iRow, 1).Left
            .Placement = xlMoveAndSize
        End With
        **** = Dir
    Loop
End Sub

Here's the code I was previously using. I need to connect these two codes into one. I've tried everything but this goes beyond my abilities :P Can you help me with this?

Code:
Sub ExampleUsage()
 Dim myPicture As String, myRange As Range
 myPicture = Application.GetOpenFilename _
 ("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", _
 , "Select Picture to Import")
 If myPicture = "False" Then
    Exit Sub
End If
 Set myRange = Range("A50")
 InsertAndSizePic myRange, myPicture
End Sub
Sub InsertAndSizePic(Target As Range, PicPath As String)
 Dim p As Picture
 Application.ScreenUpdating = False
 Set p = ActiveSheet.Pictures.Insert(PicPath)
 If Target.Cells.Count = 1 Then Set Target = Target.MergeArea
 With Target
 p.Top = .Top
 p.Left = .Left
 p.Width = .Width
 p.Height = .Height
 p.Placement = xlMoveAndSize
End With
End Sub
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try something like this...

Code:
Sub Macro123()

    Dim myPicture As String
    myPicture = Application.GetOpenFilename _
                ("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", _
                 , "Select Folder to Import")
    If myPicture = "False" Then Exit Sub
    myPicture = Left(myPicture, InStrRev(myPicture, "\"))
    InsertAllPix Range("A50"), _
                 myPicture, _
                 "*.jpg"
End Sub
 
Upvote 0
It says "Sub or function not defined", do I need to add anything else except that code you gave?

I changed macro name to Sub InsertPic.

EDIT: Nevermind, got it! Thanks!! :)
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top