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.
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

VBA Geek

MrExcel MVP
Joined
Dec 16, 2013
Messages
2,857
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
 

katkth7533

New Member
Joined
Aug 18, 2014
Messages
14
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.
 

VBA Geek

MrExcel MVP
Joined
Dec 16, 2013
Messages
2,857
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,437
Messages
5,528,766
Members
409,834
Latest member
vexceled

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top