Multiple modules can call same funtion?

vbanovice123

Board Regular
Joined
Apr 15, 2011
Messages
91
<TABLE class=tborder id=post2742546 cellSpacing=0 cellPadding=6 width="100%" align=center border=0><TBODY><TR vAlign=top><TD class=alt1 id=td_post_2742546 style="BORDER-RIGHT: #ffffff 1px solid">Can we have three modules with three different name used in the same workbook and calling the same Public function?

I get an error.

example if I have two modules:

CopyTo13MHardCopy and CopyToYearQtrHardCopy. Both modules use the same code below, the only difference being the filename in the filepath has different .xlsm name.

Can I qualify the name if I still need to use the same function and if so how?



'Public Function copy_range(sheet, rowStart, columnStart, row_count, columnCount, slide, aheight, awidth, atop, aleft)
'Public Function copy_range(sheet, rngname, slide, aheight, awidth, atop, aleft, vscale)
Public Function copy_range_sheet(sheet, rngname)
Sheets(sheet).Select
'Cells(rowStart, columnStart).Resize(row_count, columnCount).Select
Range(rngname).Select
' Make sure a range is selected
If Not TypeName(Selection) = "Range" Then
MsgBox "Please select a worksheet range and try again.", vbExclamation, _
"No Range Selected"
Else
Range(rngname).Activate
Selection.Copy
Workbooks.Open Filename:= _
"F:\Focus\MyFolder\Copy Paste Project\OnePagers_Templates\One Pagers_13Month ViewHardCopy.xlsx"
Sheets("HFI").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close

End If
End Function
<!-- / message --></TD></TR><TR><TD class=alt2 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 1px solid; BORDER-BOTTOM: #ffffff 1px solid">
user_online.gif
</TD><TD class=alt1 style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 0px solid; BORDER-LEFT: #ffffff 0px solid; BORDER-BOTTOM: #ffffff 1px solid" align=right><!-- controls -->
progress.gif
</TD></TR></TBODY></TABLE>
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Are you saying that the code is identical except for the filename being opened,which is hard-coded in each module?

If that's the case, why aren't you using one piece of code and passing the filename string as an argument?
 
Upvote 0
Ye. Just the below teo lines will change. Sheets will have 10 different tabs that need their ranges to be copied and saved in three different .xlsx files (for each sheet of the 10 sheets in the workbook).

So "HFI" will need to be copied over in One Pagers_13Month ViewHardCopy.xlsx, One Pagers_YrQtr ViewHardCopy.xlsx and One Pagers_Grid ViewHardCopy.xlsx

The range format for One Pagers_Grid ViewHardCopy.xlsx is a little different compared to the other two .xlsx. Would that be an issue?

I am also indicating the start cell to commence copy from as Range("B1")

"F:\Focus\MyFolder\Copy Paste Project\OnePagers_Templates\One Pagers_13Month ViewHardCopy.xlsx"

I will try using the filename as a string instead and let you know the outcome.

Thanks for your help.
Sheets("HFI").Select
 
Upvote 0
Apologies for the Typo error. I meant to say two lines of code will change.

the filename and the Sheet name, there are 10 sheets in each of the 3 files.

Thanks
 
Upvote 0
I tried the below for the second module. I still get the same error

"Ambigous name copy_range_sheet detected"

When I comment the other two modules the code works fine. I am usnable to use the same code for tow other modules that do the same copy functionality but in different files. I tried passing a string for the filename unless I am doing something wrong.

Thanks


'Public Function copy_range(sheet, rowStart, columnStart, row_count, columnCount, slide, aheight, awidth, atop, aleft)
'Public Function copy_range(sheet, rngname, slide, aheight, awidth, atop, aleft, vscale)
Public Function copy_range_sheet(sheet, rngname)
Dim Filename As String
Set Filename = "F:\Focus\AnitaPradeep\Copy Paste Project\OnePagers_Templates\One Pagers_yr_qtr_viewHardCopy.xlsx"
Sheets(sheet).Select
'Cells(rowStart, columnStart).Resize(row_count, columnCount).Select
Range(rngname).Select
' Make sure a range is selected
If Not TypeName(Selection) = "Range" Then
MsgBox "Please select a worksheet range and try again.", vbExclamation, _
"No Range Selected"
Else
Range(rngname).Activate
Selection.Copy
Workbooks.Open Filename:=Filename
'Workbooks.Open Filename:= _
'"F:\Focus\AnitaPradeep\Copy Paste Project\OnePagers_Templates\One Pagers_yr_qtr_viewHardCopy.xlsx"
Sheets("HFI").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close

End If
End Function
 
Upvote 0
If you are creating different Functions, then name them differently.
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,629
Members
452,933
Latest member
patv

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