VBA does not work if I copied the worksheet

musicfreer

New Member
Joined
Aug 26, 2022
Messages
4
Hi,

I have a VBA which works fine in original worksheet with a button. However, when I copied the worksheet, the button is still referencing to the original worksheet. I am totally new to coding. Please teach me how to correct the VBA or button.

Thank you

VBA Code:
Sub InsertPicsr1()
Dim fPath As String, fName As String
Dim r As Range

Application.ScreenUpdating = False
fPath = "E:\listings\listing photo\"
For Each r In Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
    On Error GoTo errHandler
    If r.Value <> "" Then
        With ActiveSheet
                .Shapes.AddPicture fPath & r.Value & ".jpg", _
                msoFalse, msoTrue, _
                .Cells(r.Row, 2).Left, _
                .Cells(r.Row, 2).Top, _
                .Columns(2).Width, _
                .Rows(r.Row).Height
        
        End With
    End If
errHandler:
If Err.Number <> 0 Then
    Debug.Print Err.Number & ", " & Err.Description & ", " & r.Value
    On Error GoTo -1
End If
Next r
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Should I put the VBA in a standalone worksheet, and all the other worksheet reference to that VBA worksheet? Sorry I am not even sure if this makes sense.
 
Upvote 0
The code will run in which ever the active sheet workbook is in. If its a specific workbook, worksheet this needs to be specified.
 
Upvote 0
The code will run in which ever the active sheet workbook is in. If its a specific workbook, worksheet this needs to be specified.
thanks. I made a button to link the VBA, but when I copied the worksheet, the button on the new worksheet still links to the VBA of the original worksheet. How can I correct it?
 
Upvote 0
I found this

VBA Code:
Public Sub FixCopiedButtonMacroLinks(ByVal theSheet As Worksheet)
    ' UPDATED: by HackSlash to accept a worksheet parameter.
    'PURPOSE: Remove an external workbook reference from all shapes triggering macros
    'Source: www.TheSpreadsheetGuru.com

    Dim control As Shape
    Dim MacroLink As String
    Dim SplitLink As Variant
    Dim NewLink As String

    'Loop through each shape in worksheet
    For Each control In theSheet.Shapes

        'Grab current macro link (if available)
        MacroLink = control.OnAction

        'Determine if shape was linking to a macro
        If MacroLink <> vbNullString And InStr(MacroLink, "!") <> 0 Then
            'Split Macro Link at the exclaimation mark (store in Array)
            SplitLink = Split(MacroLink, "!")

            'Pull text occurring after exclaimation mark
            NewLink = SplitLink(1)

            'Remove any straggling apostrophes from workbook name
            If Right(NewLink, 1) = "'" Then
                NewLink = Left(NewLink, Len(NewLink) - 1)
            End If

            'Apply New Link
            control.OnAction = NewLink
        End If
    Next control
End Sub
 
Upvote 0
I found this

VBA Code:
Public Sub FixCopiedButtonMacroLinks(ByVal theSheet As Worksheet)
    ' UPDATED: by HackSlash to accept a worksheet parameter.
    'PURPOSE: Remove an external workbook reference from all shapes triggering macros
    'Source: www.TheSpreadsheetGuru.com

    Dim control As Shape
    Dim MacroLink As String
    Dim SplitLink As Variant
    Dim NewLink As String

    'Loop through each shape in worksheet
    For Each control In theSheet.Shapes

        'Grab current macro link (if available)
        MacroLink = control.OnAction

        'Determine if shape was linking to a macro
        If MacroLink <> vbNullString And InStr(MacroLink, "!") <> 0 Then
            'Split Macro Link at the exclaimation mark (store in Array)
            SplitLink = Split(MacroLink, "!")

            'Pull text occurring after exclaimation mark
            NewLink = SplitLink(1)

            'Remove any straggling apostrophes from workbook name
            If Right(NewLink, 1) = "'" Then
                NewLink = Left(NewLink, Len(NewLink) - 1)
            End If

            'Apply New Link
            control.OnAction = NewLink
        End If
    Next control
End Sub
Thanks for your reply.

Sorry that I am really new to coding. How should I use these codes?
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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