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.
Related
I want to run the macro 'HighlightMisspelledCells' defined in the module node of Personal.xlsb on cell change of any workbook sheet.
I know that if I insert the following code on the sheet node in VBA Editor then this will call the macro defined in Personal.xlsb, but then for this, I have to insert this code every time for every new sheet, so is there any other way by which I simply add few lines in the PERSONAL.XLSB macro which runs on cell change of any opened sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.Run "Personal.xlsb!HighlightMisspelledCells"
End Sub
Note : There is also another problem (not specific to the question) is, when I save the above code then get an error message box "The Following feature cannot be saved in macro-free workbook" but still code works if I click on 'Yes' to save. But when I opened the same workbook next time then this 'Sheet specific' VBA code disappears.
PERSONAL.XLSB
Sub HighlightMisspelledCells()
Dim cl As Range
For Each cl In ActiveSheet.UsedRange
If Not Application.CheckSpelling(word:=cl.Text) Then
cl.Interior.Color = vbRed
End If
Next cl
End Sub
Note : I am using MS Excel 2016 on Windows PC
I have already tried searching on the internet but have not been able to find a solution to my specific problem.
As I click on the particular cells in excel, comments appears that disturb me much so I want vba code to delete all the comments instantly in the active worksheet.
All you really need to do is get a range, then clear comments:
Worksheets("MySheet").Activate
ActiveSheet.UsedRange.ClearComments
Does that help?
More Detail
To get the above code to work, there are several approaches. The one I recommend here is:
Open your Excel workbook.
Click the Visual Basic option on the Developer tab. This opens a VBA window with a tree control to the left, which shows the worksheets and workbooks.
Right-click the worksheet and select Insert Module.
In the module window that opens, paste the code I show at the bottom of these instructions.
Save the worksheet as type Excel Macro-Enabled Workbook.
Close the VBA window.
When back in Excel, hit to bring up the Run Macro window. You should see your RemoveComments macro listed. Now click Run and your comments should be removed.
I actually tested this, so it will work if done properly. If it still doesn't work for you, be sure that the worksheet in question is the first worksheet in your workbook. If it isn't, then change Worksheets(1).Activate in your RemoveComments Sub so that it refers to the correct worksheet.
Sub RemoveComments()
Worksheets(1).Activate
ActiveSheet.UsedRange.ClearComments
End Sub
Please see my reply
Sub delete_comments()
Dim i As Range
For Each i In ActiveSheet.UsedRange
i.ClearNotes
Next i
End Sub
I understand the logic behind this but I'm unsure how to right the macro. I import up to 63 sheets of data into excel.
ALL of the sheets have a status in Column B Row 9. I would like to make a macro to hide all sheets in the workbook when B9 = 100%
If Worksheet.Column.B, Row.9= 100%
Worksheet.hide
Open the VB Editor ALT+F11. Under Microsoft Excel Objects right click Insert --> Module. Paste in the following code.
Option Explicit
Public Sub HideSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("B9").Value = 1 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
The Option Explicit setting forces you to declare variables. I include this because, on top of good coding practice, it has saved me hours of debugging only to find I spelled a variable name wrong (such errors are captured before the code begins when adding this line).
The basic principle is that the code uses a For..Each loop to iterate through each worksheet in the workbook. IF cell B9 is 1 (corresponding to 100%) then the worksheet's Visible property is set to xlSheetHidden which hides the sheet. Sheets with this visible property can be unhidden if the user right-clicks along the worksheet tabs and selects Unhide.... If you don't want users to be able to unhide the sheets, you can set it to xlSheetVeryHidden which hides the sheet and disabled unhiding the sheet from the UI.
To run this macro you can click anywhere inside the code and click the button that looks like play (this is the Run Sub/Userform button) or you can press F5.
I would recommend setting the macro to a keyboard shortcut, or if you prefer to a button located somewhere on the worksheet.
To assign the macro a keyboard shortcut:
Under the Developer tab select Macros (or simply press ALT+F8) to display the Macro window
Under Macro name: select the name of your macro (HideSheets in this example)
Click Options...
Put the key in that you want to press to run the macro (in this case I chose CTRL+h for hide)
Select OK
Test by pressing the keyboard combination you specified
Additionally, you can assign a macro to run when a button on the worksheet is clicked, to do this:
Under Developer go to the Insert dropdown
Under ActiveX controls, select the command button
Draw the button anywhere on the page
Right click the button --> CommandButton Object --> Edit
Change the button text to whatever you want (like Hide Sheets for example)
Double click the button to open the code, you should see a Sub entitled CommandButton1_Click()
Type HideSheets into the subroutine like this (or whatever the name of your subroutine is)
Private Sub CommandButton1_Click()
HideSheets
End Sub
Exit design mode by clicking Design Mode under the Developer tab
Click the button to ensure the macro functions
Building on the answer from Soulfire, you can run the automatically anytime the value in the cell changes value. Just put the following code under the worksheet (not the module), which will run the macro 'HideSheets' whenever the value in cell C9 changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Call HideSheets
End If
End Sub
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.
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.