VBA String Issue

flipcube

Board Regular
Joined
Aug 29, 2011
Messages
134
Hi all,

I don't understand why the below code is not working. I'm Creating a program that formats Pivot Tables based on Fiscal Month. Sheet2 contains a table of FiscalMonth start and end dates. The issue I am having is that my Create_Pivot Subroutine, for some reason, does not "see" the value in FiscalMonth. Below is the part of Create_Pivot () that is giving me the problem. The if FiscalMonth = "September" statement is ignored and the subroutine ends. I'm new to VBA and do not fully understand why it's not working.

Private Sub CommandButton1_Click()
'This Subroutine determines the current Fiscal Month

Dim Today As Date
Dim FiscalMonth As String

Today = Date

If Today > Sheet2.Cells(12, 1) And Today <= Sheet2.Cells(12, 2) Then
FiscalMonth = "September"
Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=1 Create_Pivot
End If
End Sub

--------------

Private Sub Create_Pivot()

If FiscalMonth = "September" Then

--------------

any help is much appreciated.

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try like this

Code:
Private Sub CommandButton1_Click()
'This Subroutine determines the current Fiscal Month

Dim Today As Date
Dim FiscalMonth As String

Today = Date

If Today > Sheet2.Cells(12, 1) And Today <= Sheet2.Cells(12, 2) Then
FiscalMonth = "September"
Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=1
Call Create_Pivot(FiscalMonth)
End If
End Sub


Private Sub Create_Pivot(FiscalMonth As String)
If FiscalMonth = "September" Then
 
Upvote 0
VoG,

I get a Compile Error: Argument not optional. I looked at the help menu, but its Greek to me.
 
Upvote 0
VoG,

I take that back...worked like a charm. I forgot to add (FiscalMonth) to the Call Create_Pivot.

Thanks a bunch...why did it not work the original way?
 
Upvote 0
Did you apply both suggestions (in red)

Rich (BB code):
Private Sub CommandButton1_Click()
'This Subroutine determines the current Fiscal Month

Dim Today As Date
Dim FiscalMonth As String

Today = Date

If Today > Sheet2.Cells(12, 1) And Today <= Sheet2.Cells(12, 2) Then
FiscalMonth = "September"
Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=1
Call Create_Pivot(FiscalMonth)
End If
End Sub


Private Sub Create_Pivot(FiscalMonth As String)
If FiscalMonth = "September" Then
 
Upvote 0
why did it not work the original way?

You declared FiscalMonth within a Sub. That value won't be available to other subs unless you either

Dim it at the top of the module, before any Subs

Or, as I did, pass it as a parameter.
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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