Turning VBA Macro into Function

emack1230

New Member
Joined
Jul 9, 2022
Messages
34
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have the following, thank you Davesexcel. Now I am needing to figure out how to turn this into a function that will run based on a certain cell having a text value on a different tab within the workbook.

Sub OpenWorkbook()

Dim wb As Workbook, bk As Workbook

Set bk = Workbooks("destination file name.xlsm")
'assumed open

Set wb = Workbooks.Open("copied from file.xlsx") 'you would need the full address and name of workbook


wb.Worksheets("tab name").Range("A1:I500").Copy bk.Worksheets("destination tab").Range("A1")


wb.Close SaveChanges:=True


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try Code bellow. Does it help?
VBA Code:
Function CopyDataToWorkbook(wb As Workbook, bk As Workbook, wsSource As Worksheet, wsDestination As Worksheet, rngSource As Range, rngDestination As Range)

wb.wsSource.wsSource.Copy bk.wsDestination.rngDestination

wb.Close SaveChanges:=True

End Function
 
Upvote 0
I am getting a Run time '438': Object doesn't support this property or method

I will send the code to you privately since our company name is in the file name for security.
 
Upvote 0
I'm sorry. Does the code open file from SharePoint?
 
Upvote 0
I'm sorry. Does the code open file from SharePoint?
It acted like it was going to and that may be the piece I am missing. Should I build that in from the original I posted so it will open the Sharepoint file?
 
Upvote 0
Maybe you need to rebuilt to open files from SharePoint
VBA Code:
Sub OpenSharePointFile()
    Dim sharePointLink As String
    Dim fileName As String
    Dim fileURL As String

    ' Set the SharePoint link and file name
    sharePointLink = "https://your-sharepoint-site-url/"
    fileName = "example.xlsx"

    ' Construct the file URL
    fileURL = sharePointLink & "/" & fileName

    ' Open the file
    Workbooks.Open fileURL

End Sub
 
Upvote 0
Maybe you need to rebuilt to open files from SharePoint
VBA Code:
Sub OpenSharePointFile()
    Dim sharePointLink As String
    Dim fileName As String
    Dim fileURL As String

    ' Set the SharePoint link and file name
    sharePointLink = "https://your-sharepoint-site-url/"
    fileName = "example.xlsx"

    ' Construct the file URL
    fileURL = sharePointLink & "/" & fileName

    ' Open the file
    Workbooks.Open fileURL

End Sub
Thanks will give it a whirl, most likely tomorrow when I can update and work with my partner.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

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