1st part of code for tab name problem

DThib

Active Member
Joined
Mar 19, 2010
Messages
464
Office Version
  1. 365
Platform
  1. Windows
Hello,

My code will run the rest of the macro, but I am having trouble pulling the tab name to be changed before the save and export.

My tab name will have elements from String names run and identified in other code running in this workbook. I am trying to pull the name in total with the first macro.
The second macro is my attempt at renaming the tab before save and export as CSV of this worksheet.
I keep getting an error 9. Subscript out of range on the sheets("ProName") in the second macro.

Code:
Sub Tabname_Pro()

   Dim ProName As String
   Dim Filenumber, Study As String
    ProName = Study & "_Run" & Filenumber & "_Pro"
'   Study & "_Run" & Filenumber & "_Pro"
    With sheets("XXX_Pro_Run")
    '.Value = ProName
    .Name = ProName
    End With
End sub

Code:
Sub CSVLIMS()

     Call Tabname_Pro
         Dim ProName As String
         
            Application.Left = 1466.5
            Application.Top = 7.75
            
            sheets("ProName").Select
            Range("A1:K42").Select
            Cells.Select
            Selection.Copy
            Workbooks.Add
            ActiveSheet.Paste
            Application.CutCopyMode = False
            ChDir "\\server\Templates"
            ActiveWorkbook.SaveAs Filename:= _
                "\\server\Templates\ProName.csv" _
                , FileFormat:=xlCSV, CreateBackup:=False
        
            ActiveWindow.Close
            sheets("RUN-Setup").Select
            Range("A2").Select
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
First off. You haven't given either Filenumber or Study a value. What should they be?
 
Upvote 0
Actually I have both being first executed in another macro. Those do work. I have them set as Public (Public Filenumber Public Study As String) in that macro
 
Upvote 0
In that case try
Code:
Function Tabname_Pro()

   Dim ProName As String
   Dim Filenumber, Study As String
    ProName = Study & "_Run" & Filenumber & "_Pro"
    With Sheets("XXX_Pro_Run")
    .Name = ProName
    Tabname_Pro = ProName
    End With
End Function

Sub CSVLIMS()
Dim ProName As String
     ProName = Tabname_Pro
         
         
            Application.Left = 1466.5
            Application.Top = 7.75
            
            Sheets(ProName).Select
            Range("A1:K42").Select
            Cells.Select
            Selection.Copy
            Workbooks.Add
            ActiveSheet.Paste
            Application.CutCopyMode = False
            ChDir "\\server\Templates"
            ActiveWorkbook.SaveAs filename:= _
                "\\server\Templates\ProName.csv" _
                , FileFormat:=xlCSV, CreateBackup:=False
        
            ActiveWindow.Close
            Sheets("RUN-Setup").Select
            Range("A2").Select
End Sub
 
Upvote 0
OK, So I Keep getting "_Run_Pro" as the tabname in the workbook. It keeps saving to the folder as ProName. It is providing a CSV file.
 
Upvote 0
What if you remove this line
Code:
 Dim Filenumber, Study As String
and change
Code:
ActiveWorkbook.SaveAs filename:= _
                "\\server\Templates\" & ProName & ".csv" _
                , FileFormat:=xlCSV, CreateBackup:=False
 
Upvote 0
Now it gives "
_Run_Pro" as the .CSV file name. 1 step closer.

Am I calling from Public for Filenumber and Study in another module wrong?

Could that be the issue?

DThib
 
Upvote 0
Did you remove the dim statements?
Also where are your public variables declared?
They should be at the very top of a standard module, before any code. And they should not be declared in any sub
 
Upvote 0
Yes, I removed the Dim statements with Filenumber and Study from the Function macro. I left the ProName reference in both the Sub and Function macros



 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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