Insert a value in a cell based on what macro is run

katkth7533

New Member
Joined
Aug 18, 2014
Messages
14
Hi. I have a spreadsheet where I added macros (attached to buttons) that filter information on a long report based on values in 1 column (the information is a department name). There are 7 different macros for the 7 departments. The code I used is
Code:
Sub DC_Click()
    Sheets("Tasks").Visible = True
    Sheets("Tasks").Range("V5:V6223").AutoFilter Field:=1, Criteria1:=Array("D&C", "PN"), Operator:=xlFilterValues
    Sheets("Tasks").Range("B1:I6223").PrintPreview
    Sheets("Tasks").Range("V5:V6223").AutoFilter
    Sheets("Tasks").Visible = False
End Sub
The Department is in the array ("D&C" in this instance. "PN" will appear in all the macros) and I named the Macro "Sub DC_Click()". All the other macros follow the same format with the department name changing. What I want to do is insert the Department name in a cell in the Header of the report based on what macro is running. So if the user selects this macro for "D&C", "D&C" will appear in cell E2 (not the "PN"). How would I do this? The code will need to say something like if Sub DC_Click() is run, insert "D&C"; if Sub Legal_Click() is run, insert "Legal", etc., continuing for all 7 departments. Thank you.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
what about adding a constant in each sub which contains what you want to insert?

Sub DC_Click()
const WhatToInsert as string = "D&C"
Sheets("Tasks").Visible = True
Sheets("Tasks").Range("V5:V6223").AutoFilter Field:=1, Criteria1:=Array("D&C", "PN"), Operator:=xlFilterValues
Sheets("Tasks").Range("B1:I6223").PrintPreview
Sheets("Tasks").Range("V5:V6223").AutoFilter
Sheets("Tasks").Visible = False

' insert example
range("a1").value = WhatToInsert
End Sub
 
Upvote 0
Thank you for the reply. I am having a syntax error when I try this. I am sure it is because I have been staring at this too long. What I did was the following:
Code:
Sub DC1_Click()
 const Sheets("Tasks").range("E2").Value As String = "D&C"
 Sheets("Tasks").Visible = True
 Sheets("Tasks").range("V5:V6223").AutoFilter Field:=1, Criteria1:=Array("D&C", "PN"), Operator:=xlFilterValues
 Sheets("Tasks").range("B1:I6223").PrintPreview
 Sheets("Tasks").range("V5:V6223").AutoFilter
 Sheets("Tasks").Visible = False
 ' insert example
 range("a1").Value = WhatToInsert
 End Sub
and also tried
Code:
Sub DC1_Click()
 const range("E2").Value As String = "D&C"
 Sheets("Tasks").Visible = True
 Sheets("Tasks").range("V5:V6223").AutoFilter Field:=1, Criteria1:=Array("D&C", "PN"), Operator:=xlFilterValues
 Sheets("Tasks").range("B1:I6223").PrintPreview
 Sheets("Tasks").range("V5:V6223").AutoFilter
 Sheets("Tasks").Visible = False
 ' insert example
 range("a1").Value = WhatToInsert
 End Sub
The error is in the first line where I inserted the range. I get a Compile error and it says "Expected: As or =" and is highlighting the where the first range meets the (. Any clues here? I am sorry to keep asking when you have been nice enough to help but my brain is not seeing it right now. Thank you.
 
Upvote 0
you cannot use constants this way.

const Sheets("Tasks").range("E2").Value As String = "D&C"

the part in bold is supposed to be the name of your constant which in our case is WhatToInsert

so it should be: Const WhatToInsert as string = "D&C"

also you cannot use in your constants anything that VBA has to retrieve/calculate at runtime, VBA has to be able to read and store all their values before the code actually starts, in fact if your go in debug mode and press F8 to walk through your code, the yellow line will never touch the Constants
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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