Macro to recognize a workbook with variable name

Mangisqa

New Member
Joined
Apr 28, 2011
Messages
9
Hello,

I am trying to write a macro to reference workbooks that have diffrent prefixes.

Windows("Regulatory Report.XLSB").Activate
Columns("A:A").Select
Selection.Copy
Windows("BOReport.XLSX").Activate

I would like the code to recognize the above files if they contain a variable prefix (ie Area1Regulatory Report or Area7BOReport) depending on the location using the macro.

Thank you in advance!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi Mangisqa,

Here is a user-defined function (UDF) you can use to enable you to find the workbook that matches a name pattern:

Code:
Function WkbkName(MatchName As String) As String
   'Returns the name of a workbook matching the name MatchName.  MatchName is a workbook
   'name pattern that may contain wildcards *, ?, [abc].  If no workbook matches, the
   'null string is returned
   Dim Wkbk     As Workbook
   For Each Wkbk In Workbooks
      If Wkbk.Name Like MatchName Then
         WkbkName = Wkbk.Name
         Exit Function
      End If
   Next Wkbk
   WkbkName = ""
End Function

Just place this code in your macro module. Then you can use it in your code like this:

Code:
Dim SourceWB   As String   'Name of source workbook
Dim DestWB      As String   'Name of destination workbook

SourceWB  = WkbkName("*Regulatory Report*")
DestWB = WkbkName("*BOReport*")

Workbooks(SourceWB).Columns("A:A").Copy

Windows(DestWB).Activate
etc.

Note that you don't actually have to activate the source workbook in order to copy a range from it. Nor do you have to activate the destination workbook to paste it, but I left it that way because you probably want to view it when the data are pasted.

Keep Excelling.

Damon
 
Upvote 0
This absolutely did the trick! and thank you for the explanations, as some one who is has a lot of learning to do in this realm, it definitely helps me understand what the coding means and how I can use it in different situations!

Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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