Share via

VBA Macro Call Within Other Workbook

Anonymous
2023-09-27T21:00:58+00:00

Good afternoon!

I am hoping to obtain some assistance.

I tried looking online, but can't find any reference to the solution I'm trying to develop. I have a set of data I want to run through a template (which uses RegEx to clean, sort, and extract data) and then copy and paste to the current workbook. Ordinarilly I'd try to do it all in one step (i.e. import raw data and clean in the destination workbook), however the size of the data is signficiant and cleaning the data after import appears to be too cumbersome for Excel. Therefore, the only other option is to run it through a template and paste as values into the destination workbook.

Both the macro in the destination workbook and the macro in the template work, but I'm trying to figure out how to

1). a static file (the template)

2). run the import macro within the template

3). copy the data from the template

4). paste as values to the destination workbook

I can get to step 2, and Steps 3 and 4 are a breeze. But I just don't know specifically what syntax to use.

I was trying to use:

Dim FileToOpen As Variant

Dim OpenBook as Workbook

Dim sh as Worksheet

Dim sh2 as worksheet

Dim lastERow as long

Dim lastERow2 as long

Application.ScreenUpdating = False

FileToOpen = Application.GetOpenFilename

If FileToOpen <> False Then

Set sh = ThisWorkbook.Worksheets(3)

sh.showalldata

lastERow = sh.Range("A" & sh.Rows.Count).End(xlUp).Offset(1,0).Row

Set OpenBook=Application.Workbooks.Open(FileToOpen)

Set sh2 = OpenBook.Worksheets(1)

lastERow2 = sh2.Range("A" & sh2.Rows.Count).End(xlUp).Offset(1,0).Row

OpenBook.Application.Run("ImportData()")

sh2.Range("A2:W" & lastERow2).copy

sh.Range("A2:W" & lasteERow).pastespecial xlpastevalues

OpenBook.Application.CutCopyMode = False

OpenBook.Close False

Application.ScreenUpdating = True

End If

End Sub

Any ideas?

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2023-09-27T21:11:30+00:00

Change

OpenBook.Application.Run("ImportData()")

to

Application.Run "'" & OpenBook.Name & "'!ImportData"

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-09-28T15:12:56+00:00

    Thank you!!

    This worked like a charm!!!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-09-27T21:26:37+00:00

    Omg is it really that easy?!

    As soon as I get back to th office tomorrow I'll try it!!

    Was this answer helpful?

    0 comments No comments