Ensure Macro Code Applies To Correct Sheet & Workbook
Mar 21, 2008
I am trying to write some code that is linked to a Command button. The code in the command button is in my workbook called "MF Consolidated ACTUAL DAILY REPORT - Dev.xls" but then I have another workbook called "MF BANK EXPOSURE SUMMARY.xls" that I want to do some work with - namely delete blank columns and it is here that I am having the problem.
My problem is this: my code module is contained in my project "MF Consolidated ACTUAL DAILY REPORT - Dev.xls" and the columns that I want deleted are in the "MF BANK EXPOSURE SUMMARY.xls". Now even though I believe I have activated the "MF BANK EXPOSURE SUMMARY.xls" workbook the action, that of deleting the blank columns is performed on the "MF Consolidated ACTUAL DAILY REPORT - Dev.xls" workbook. Why?? Here is the code that I am using:
'ASK FOR DATE AND SET IT IN THE REPORT
Workbooks("MF Consolidated ACTUAL DAILY REPORT - Dev.xls").Worksheets _
("Seg and Non Seg Bank Summary"). Range("I1") = Application.InputBox("PLEASE ENTER REPORT DATE IN THE DD/MM/YYYY FORMAT")
'ACTIVATE MF BANK EXPOSURE SUMMARY
Workbooks("MF BANK EXPOSURE SUMMARY.xls"). Sheets("Seg and Non Seg").Activate
Dim iCol As Integer
Dim Isheet As Integer
I have a workbook that starts off with the following sheets:
MAIN_PAGE - Where filter criteria is selected via checkbox and PLOT button
RAW_DATA - unfiltered raw data
After running a series of macros tied to the PLOT button, two new sheets are created:
FILTERED_DATA(HIDDEN) - where the filter criteria from the MAIN_PAGE is applied, and the filtered data is stored.
CHART - The plot of the hidden FILTER_DATA sheet.
Every time the PLOT button is pressed, the the macro searches for the FILTERED_DATA and CHART sheets, and if present, clears and overwrittes with the new appropriately filted data. This works and suits the user's needs 95% of the time. However....
QUESTION If the user decides to keep the old CHART and FILTER_DATA as well as the new FILTER_DATA and CHART, I need something to differentiate the two charts and data sheets. My thought was to have the user rename the CHART. The problem is that because the FILTERED_DATA sheet is hidden (and has to stay that way), how can the name change of the CHART be carried over onto the hidden FILTERED_DATA sheet, therefore keeping the plot and it's data in tact? For example, if I append the name CHART with a 1 to make it CHART1, how do I automatically change FILTERED_DATA to FILTERED_DATA1?
I would like to know whether its possible to fix a sheet to always be the second sheet in a work book no matter how many other sheets are added. Currently I have set up macros to add subsequent sheets before the last sheet, so that Sheets("Number 2") remain sheets(2). Is there some way I can lock the first two sheets so that when I reference them in my code as sheets(1) and sheets(2) it will reference the right sheets. Right now I have it set up so that Sheets("Number 2") can be renamed by the user, and data inputted on this sheet, but I require that the user not be able to move this sheet and the sheet preceding it.
So I have a macro that sends an email of the active spread sheet. I want to add that macro to another macro. The tricky part is that i call other macros inside the macro. So is there a way i can add the send email macro and have it apply to all the macros i call. Here my code:
Code: Sub Run_All_Billing() ' ' Run_All_Phones Macro ' Make sure you open "gateway.csv" "phone.csv" "ctiroutepoint.csv" and "Billing Contact.xls" ' Call Gateway Workbook and clean it up
I have attached a 97-2003 .xls file with data for multiple store locations on sheet 1, and the desired result on sheet 2. I am actually using excel 2007, but I dont think I need any special features that it provides.
I will try to explain the issue here without opening the attachment.
I have written some code that copies certain cells from a spreadsheet constructed form back to a master spreadsheet database. The idea being to prevent users of the form from accessing the database and mucking it up. I write it the long way round and am now trying to modify it to not have to open and close the database everytime BUT whenever I change it I get errors that I think relate to the code not understanding the change of object...
' 3. send updated data to the database (all yellow boxes will update)
' set parameters for cells to copy from Dim r As Long, e4 As Long, e6 As Long, e18 As Long, e20 As Long, e22 As Long, e24 As Long, _ e26 As Long, e28 As Long, e30 As Long, e32 As Long, e34 As Long, e36 As Long, e38 As Long, _ e40 As Long, e42 As Long, e44 As Long, e46 As Long r = Range("D2") e4 = Range("B4") e6 = Range("B6") e18 = Range("B18") e20 = Range("B20") e22 = Range("B22") e24 = Range("B24")
The little blocks of code go on for 17 open/closes!
This code opens a UserForm in the Workbook_Open event when the file is opened. After 10 seconds, the message is supposed to go away if the user doesn't click anything, then macros that follow are supposed to run. It is intended to allow a user to stop the macros. This works when you open the file manually (macro security must be low), you'll see a msgbox pop up after the UserForm goes away.
However, when the workbook is opened as a Windows Scheduled Task (Windows/ Control Panel/ Scheduled Tasks), the UserForm does not come to the front of the screen unless a user actually clicks on the window. It just flashes at the bottom indefinitely.
I'm trying to find a way to save a single sheet of an excel workbook and in the same process delete all vba code and shapes from the new single sheet workbook. I was looking around and found this code which does save only the single sheet to a new one sheet workbook but doesn't delete the vba and shape that I have used to assign macros to in the original.
Code: Sub SaveSheetAsNewBook() Dim wb As Workbook Dim InitFileName As String Dim fileSaveName As String Dim wshape As Shape InitFileName = ThisWorkbook.Path & Format(Date, "mm.dd.yy")
I have data in 2 different workbooks, and I need to transfer all data in 'Jx_ex1' to sheet1 in the 'template' workbook, and all data in 'Gr_ex1' to sheet2 in 'template' workbook. All data should be transferred to the same cell numbers, from A1 to Y5000. I actually have loads of workbooks to transfer to the template but if I have an example with the two then im sure I can work out how to modify it. All files will be in the same folder.
I have a userform with several fields located in it. For the most part, it looks like a user information form, Name, Date,, stuff like that. There is a field that the user enters some information, and using the afterUpdate function for the field, it searches to see if the value is unique. The problem is that when I tab from that field, if the value is not unique, I want the cursor placed back in that field (textbox). SetFocus doesn't seem to do what I want, unless I am using it incorrectly.
I am having a problem with this script below. I think it is in the way the worksheet is picked. Can someone take a look and offer some advice on how to pick between the 2 sheets? I'm getting an error on this line in red below( object doesnt support this property or method)
I have a protected worksheet with some macros running on it, and I have been requested to make the workbook shared. Trying to implement this, I keep getting the standard "1004 You cannot use this command on a protected sheet" error when applying an autofilter.
Here is what is making this problem a little tricky:
- As the workbook is shared, I cannot do Protect UserInterfaceOnly because you can't change the protection settings on a shared workbook without unsharing it. Needless to say, I cannot unshare the workbook.
- When I protect the sheet, I do allow the user to use autofilter. Consequently, Sheet.Protection.AllowFiltering and Sheet.EnableAutofilter both are on. Still, I keep getting the error.
where shData is the codename of the sheet, lngField is the number of the field and strArg is the filtering criteria.
My guess is, that the error comes from the fact that (for some silly reason) in VBA the .AutoFilter method is used to create a new filter, not only to use an existing one. The members of Sheet.AutoFilter.Filters collection are read-only, and cannot be used to modify the existing filter.
I copied the sheet and redid the format. I renamed the original dashboard sheet from count to "keep" and named the copy after the original "count" Everything still works great - until i either hide or delete the original count which is now named "keep". I get an error at the red colored line below ".publish false".
I am making a spreadsheet that sorts and pastes, but I need to know if I can add a code to the Sort and Paste Macro that will open the second spread sheet needed without just already having it open and using the
I have some VBA code that formats my sheets and works fine, but the issue is that I run this same piece of code more than once in different sheets with the only difference being the formula references a different cell.
' Green Cells indicate that the supervision is within the year .FormatConditions.Add Type:=xlExpression, Formula1:= _ "=Datedif(D8,today(),""m"")
I need vba code to retrieve the workbook name and place it into macros. the workbook name does not change in the macro if the name of the workbook changes. so i need to assign the name to a variable then use the variable name as the workbook name............
I am trying to write code to open a new (blank) workbook while in an existing workbook (I am then going to pass data between the two which is easy). When I try to record the code to get the syntax for opening a new workbook it will not record any code. I also need to name the new workbook based on text in a cell in the existing workbook. I just saw before posting this that the code;
will open a new workbook now I just need to name the new workbook based on text in my existing workbook.
I want is a Macro that will save the file as .csv, with the same filename and location as the original (just in csv format). A hotkey would be quite handy for this. I tried using the macro recorder and fiddled around with the code a bit but my programming knowledge is quite limited and I ended up with something that doesn't quite work as i'd like. I'm sure this would only take a few seconds to code for an experienced user, which is why I post here. I would post my attempt at doing it myself but i'm on a computer that doesn't have excel at the moment.
The code below works correctly on certain sheets. The code is supposed to loop through worksheets in an array, calculate the percent change from 1990 to 2012 and from 2005 to 2012, and put the calculations on the 4th and 5th row from the last non-empty row, respectively. All the sheets are identical except for 3. The sheets that are different only have a different number of years of data. For some reason this causes the macro to put the percent change calculations in random rows below the correct location. Also, the macro doesn't work correctly on one of the identical sheets.
Example of how the macro runs correctly on an identical worksheet : correct.gif
Example of how the macro runs incorrectly on one of the 3 sheets that are not identical : incorrect.gif
I have Monthly sales sheets that import my cash register data into them. I wanted to set them up to do everything without being there. So I have my task manager open excel at 9:30pm everyday and it runs the macro to import the data into the correct day of the month. Here is the workbook
Private Sub Workbook_Open() Dim dTime As Date dTime = Time If dTime >= TimeValue("9:30 PM") And _ dTime < TimeValue("9:40 PM") Then ImportData End If End Sub
This is in my July spreadsheet only. So is there a way to make it know which month spreadsheet to open on the 1st of the month? So come August 1st it will automatically open the August workbook and input the data for the first day? By using the date?
I have a worksheet that changes in length. There is a formula that calculates how many pages the sheet will be. The also is a print button on the sheet that will print a coversheet "Sheets("Title")" and then it is supposed to print the data on the second sheet. Below the code I have so far... I know I am missing something, but knowing very little about macros I just can't seem to figure it out.
Okay, I have this code and it works very well - but it only works if my WORKBOOK is unprotected. I know how to Unprotect then Protect an ActiveSheet, but I can't figure out how to Unprotect then Protect my workbook when the macro runs..
Sub BLM_RENAME_SHEET() Dim WS As Worksheet Application.ScreenUpdating = False For Each WS In ActiveWindow.SelectedSheets
Each report has the same layout, so the data I want will be in the same cells. However, as the report names change as the weeks in the year go on, I can't write the code for weeks 1-4 as they will not work with week 5 onwards!
I have found out how to chose a file of my choice using:
Sub GetImportFileName() Dim FInfo As String Dim FilterIndex As Integer Dim Title As String Dim FileName As Variant
' Set up list of file filters FInfo = "All Files (*.*),*.*"
' Display *.* by default FilterIndex = 5 ......................
Following syntax is incorrect, what is wrong? This should happen: Select op a specific worksheet (named DATA) in the workbook Report.xls and I empty the contents
Then I try with a 'With' statement to read the contents of a file named "sourcedata.xls' with helds a worksheet named "source". The values in this sheet should be tranfered to the sheet DATA in the workbook "Report.xls"
Sub FetchDataFromClosedWorkbook() Dim FileName As String Dim SheetName As String Dim cellRange As String Const ReportPath = "C:ExcelReports" Windows("Report.xls").Activate Sheets("DATA").Select Cells.Select....................
I'm trying to create a formula that will added the correct amount in the correct cells, I have create a dunny sheet in trying to achieve this. If Cell B8:B11 = ABS or Dum that any points won should be added to Cell L8:L11 right now its adding it into K8:K11. If Cell B8:B11 = is Blank any player points should be added to cells K8:K11. I'm using this formula throughout cells K8:K28 =IF(J8>J25,1,IF(AND(J8<>0,J8=J25),0.5,0)) Any thing in red is incorrect anything in blue is what I'm trying to achieve.