Cannot see excel sheet in VBE - excel

I am working with an Excel file that was created by somebody else.
One sheet containing Macros appears to be password protected, but what I don't understand is that I cannot see it in VBE under the sheet list. The sheet tab is visible in Excel, but I cannot see the content.
Is there any way to unhide it in VBE?

One sheet containing Macros
Does that refer to Excel 4.0 macros?
Worksheets containing Excel 4.0 macros don't appear to be visible within the list in VBE.
They do appear to be accessible from VBA to some extent: using Excel 2007 I inserted an Excel 4.0 macro sheet to a workbook then tried the following:
Public Sub TestAccessToXL4MacroSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet ' succeeds
Debug.Print ws.Name ' outputs "Macro1"
Set ws = Worksheets("Macro1") ' fails: "Subscript out of range"
End Sub

As far as I know, there is no way you can hide a sheet from VBE! However, you can rename it there (in effect changing the .CodeName of the worksheet). Thus, if you know the Excel worksheet name (the one you see in the Excel worksheet tab), but cannot find it in VBE, go to the Immediate window in VBE (Ctrl-G) and run
? Worksheets("YourName").CodeName - this should give you the name under which it can be found in the VBE Project tree.

Related

Excel - VBA - Add-In - Worksheet

I created an excel macro "add-in" for the first time. So now I can use the same macro across multiple workbooks using the quick link at the top of the workbook.
My issue is that the first command of my Macro is to add in a sheet "Sheet1". My workbook has 2 sheets in it currently. "Attrition 2017" and "Attrition 2018".
When I added in "Sheet1" the first time nothing happened, and the rest of my workbook errored out because of it. The second time I went through it said "Sheet1" already exists. This is the only workbook I have open. I tried it with numerous sheet names. it keeps adding sheets to an unknown spot then stating that they already exist. the rest of my code works with the add-in.
My code for adding in the worksheet works when not using the add-in function. here it is.
Dim ws As Worksheet
With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "Sheet1"
End With
ThisWorkbook is the workbook where the code is running - in this case your add-in.
You probably need ActiveWorkbook here

ISO How to program ActiveX button in Workbook 1, to run macros in Workbook 2?

Situation:
I have two workbooks:
Workbook #1: A Downloaded data set from online data repository
Workbook #2: A Master collection of Macros
I have built a collection of Macros to format a data set after it's been downloaded from an online repository. This data set can differ greatly based on features that users have chosen before downloading the data. The macros I created cover all possible scenarios. Thus, when opening the View Macros dialog box there are an overwhelming amount to choose from. Even with efficient naming conventions it's too much to sift through for my audience, who has an average to low experience level with Excel.
In order to simplify their experience, I wanted to level the playing field by simply providing a "Go" button after they choose from an ActiveX dropdown list.
I successfully created the dropdown, populated the list, and upon activation of the "Go" button, the selection triggers a specific Macro to run.
MY PROBLEM:
I need the Subcode on the "Go" button in Workbook #2 to force the macros to run in Workbook #1.
Thank you ahead of time for all your help!
You can take cues from the macro recorder.
Here is some basic code that is stored in one workbook and manipulates cells in another workbook.
Sub Macro2()
Dim mlib As Workbook
Dim wb As Workbook
Dim ms As Worksheet
Dim ws As Worksheet
Set mlib = ActiveWorkbook ' the file with the macros
Set ms = mlib.Worksheets("Sheet1")
' write into the macro workbook
ms.Range("B3").FormulaR1C1 = "asdf"
'activate another workbook that is already open and is called somefile.xlsx
Windows("somefile.xlsx").Activate
' set variables to reference that workbook
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
' write something into somefile.xlsx
ws.Range("B1").FormulaR1C1 = "copy me"
'copy something within somefile.xlsx
ws.Range("B1").Copy ws.Range("B2")
' copy something from the macro workbook to the somefile workbook
ms.Range("A1").Copy ws.Range("A5")
' activate the macro workbook
mlib.Activate
End Sub
I asked for some sample code so I could explain in your context.
There are about a dozen different ways how you can identify the "other" file instead of hard-coding it into the macro. If you could be bothered to provide a bit more information, that little detail could also be taken care of.

VBA Well-hidden macro

I'm working on an inherited spreadsheet (creation date 1998, or maybe earlier) and need to change the references on a dialog sheet. I can't see the macro in the run macro list, or anything similar, the sheet itself doesn't show up in the VBA project explorer window, which sounds like it could be an Excel 4.0 macro from other questions I've read here. A lot of people suggest rewriting an Excel 4.0 macro from the beginning, but the problem is that I don't actually know what the macro does.
Specifically, the macro is linked to a command button, and when I click on 'Assign macro', the name of the macro is written in the 'Macro name' text box, but it's not actually listed in the drop-down menu. If I try to assign this macro to another command button by typing the same name into the 'Macro name' text box, I get an error that the macro is unavailable.
(Screenshot link: http://imgur.com/HlxvLV2)
I tried using the solution here (Cannot see excel sheet in VBE), and because the sheet I'm interested in is not actually a worksheet, I declared ws as a dialog sheet.
Public Sub TestAccessToXL4MacroSheet()
Dim ws As DialogSheet
Set ws = ThisWorkbook.ActiveSheet ' succeeds
Debug.Print ws.Name ' outputs "Macro1"
Set ws = Worksheets("Macro1") ' fails: "Subscript out of range"
End Sub
The result is that the name of the sheet appears in the immediate window.
(Screenshot link: http://imgur.com/C1L2SQP)
Is this the intended result? I can already see the sheet name as a tab, I want to see the sheet itself in the VBA editor.
XL4 worksheets (your macro1 sheet for example) are not visible in VB IDE.
To view the sheet in Excel right click on one of the visible sheet tabs and from the menu select the command "Unhide...". You can select the macro1 sheet and click "OK".
Alternatively, try
Sub ShowAllSheets()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
sh.Visible = True
Next
End Sub
Then you will need to convert the XL4 code to VBA manually.

How to Delete Multiple Blank "Microsoft Excel Objects"

I have a spreadsheet with one sheet named "Sheet1". I have unhidden all other sheets and removed them. When I open the VBA editor, under "Microsoft Excel Objects" I have nearly 4000 Sheets listed, and I cannot remove them via right click.
It is my belief that this is causing the file's size to be way too large (~6 MB) when there is no data held in "Sheet1". I have tried using the "goto>special>objects" method, and no objects are found this way.
Any ideas on how to clear out all these empty objects?
I think I found a solution, at least it's working for me and I'm not very savvy with VBA. Add the Properties Window (under view), select each sheet, and change the Visible option to -1 - xlsheetvisible, it will make it appear in the excel and then you can delete the tab.
There are many ways to achieve what you want. The fastest way is right click on Sheet1 and click on "Move or Copy" ~~> "New Book" ~~> "OK"
Now you can save the new workbook and delete the old one :)
FOLLOWUP
Unfortunately this won't work - we have some pretty lengthy formulas in other sheets with this exact problem and Excel will only copy the first 255 characters of them. Additionally, the VBA code in the original sheet is not copied, involving additional steps to rebuild each spreadsheet affected with this problem.
In such a case try this code
Option Explicit
Sub Sample()
Dim ws As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Sheets
If ws.Name <> "Sheet1" Then ws.Delete
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

EXCEL 2007 - Need Help creating a button which takes the contents of active worksheet and pastes it in a new worksheet

I have search throughout this site to find a answer to my problem and most of the related solutions are for a far more complicated problem. Here is what I need to have done. I created a simple form in Excel 2007. I am looking for the ability to add a button at the bottom of the form which allows the user to click on the button and copy that worksheet into a new worksheet within the same excel document. Basically just duplicating the active worksheet.
I tried to do it with macros but did not get the desired results, and most of our co-workers still use Excel 2003 so I am not sure if macros will work in the older version of excel. I do not know any VBA which is why I come here in search of help from you all.
So to recap.
One sheet Excel document with a simple form and a command button at the bottom of the active worksheet
The command button "Copy and Paste" that worksheet into a new worksheet within the same excel document
A solution that could work in both Excel 2003 and 2007 if possible. If not, for 2007.
Thanks so much ahead of time for anyone who is willing to help out a Novice Excel User.
Assuming that you know how to add a button here is a simple line of code to duplicate the active worksheet:
Sub Button1_Click()
ActiveSheet.Copy after:=ActiveSheet
End Sub
Maybe something like this (tested in Excel 2003 only):
Dim srcSheet, dstSheet
Set srcSheet = ActiveSheet
Sheets.Add
Set dstSheet = ActiveSheet
srcSheet.Activate
srcSheet.Cells.Select
Selection.Copy
dstSheet.Activate
dstSheet.Cells.Select
ActiveSheet.Paste
You should find this method will work in both Excel 2003 and Excel 2007. In your form, add the following method:
Sub CopySheet(WorkSheetName as String)
Dim WrkSht As Worksheet
Set WrkSht = Sheets(WorkSheetName)
WrkSht.Copy After:=Sheets(WorkSheetName)
Set WrkSht = Nothing
End Sub
From the button click event, call it using:
Sub Button1_Click()
Call CopySheet("WorkSheetToCopyName")
'You could also replace the string name with ActiveSheet if you so wish
End Sub
This will dump a copy of the worksheet in between the current sheet and the next one. I've tested it in Excel 2003 and Excel 2007 and it works in both. It doesn't give the second one a pretty name sadly - it just gets the same name as the source worksheet with (2) put after it.
All the formatting, protection and formulas are copied across too - it's a carbon copy of the first.
I know the question is quite old, but just wanted to note that you (and the user) can do the exact same thing with zero code: right-click on the sheet name at the bottom and select Move or Copy..., then check the Create a copy box and click Ok. Yes, it takes 4 clicks, but it is super easy and avoids code.

Resources