I have created an Excel-Document with macros that my customer should fill out save pressing a button.
Under the button is just this macro:
Sub filesave()
Dim bFileSaveAs As Boolean
bFileSaveAs = Application.Dialogs(xlDialogSaveAs).Show
End Sub
The problem is that as default option you get to save the document as macro enabled excel workbook.
How can do show as default non macro enabled excel format or not show the posibility to save the document as macro enabled document in oder to make sure that the macros will not be saved in the filled out copy of the document?
Some ideas about how the code should look like?
You can use GetSaveAsFilename to let the user choose a location and filename and then save with Workbook.SaveAs Method by choosing a file format from XlFileFormat Enumeration.
Public Sub SaveFileAs()
Dim FileToSave As Variant
FileToSave = Application.GetSaveAsFilename(fileFilter:="xlsx Files (*.xlsx), *.xlsx")
If FileToSave <> False Then
ActiveWorkbook.SaveAs Filename:=FileToSave, FileFormat:=xlOpenXMLWorkbook
Else
'user chose cancel
End If
End Sub
Related
I am new to Excel Add-ins and I am not sure how to write mi programm.
I would like to put in an add-in a code so that, when the workbook that uses the add-in is opened, it creates a sheet named "mainSheet".
I can use the event handler in the Workbook, but is it possible to put the code in the module of the add-in and still be able to run it?
I found this on the "Automate Excel" web site. Hope this helps
The following code works opening a workbook. It automatically adds a new sheet and labels it with the name. It also checks to see that the sheet doesn’t already exist – to allow for the possibility of it being opened more than once a day.
This code makes use of the Workbook Open Event and must be placed in the workbook module under the “Open work Book” event. The function Sheet_Exists must be placed in a module and this checks whether or not the sheet exists:
Private Sub Workbook_Open()
Dim New_Sheet_Name As String
New_Sheet_Name = "mainSheet"
If Sheet_Exists(New_Sheet_Name) = False Then
With Workbook
Worksheets.Add().Name = New_Sheet_Name
End With
End If
End Sub
==
Function Sheet_Exists(WorkSheet_Name As String) As Boolean
Dim Work_sheet As Worksheet
Sheet_Exists = False
For Each Work_sheet In ThisWorkbook.Worksheets
If Work_sheet.Name = WorkSheet_Name Then
Sheet_Exists = True
End If
Next
End Function
I have 3 checkboxes. When a user opened my sheet, he/she must not check checkboxes. I want them to be disabled. How can I do that?
Not sure if you meant ActiveX or FormControl, so here you go
Code
Private Sub Worksheet_Activate()
Dim myActiveX As Object
Set myActiveX = Sheet1.OLEObjects("CheckBox1")
myActiveX.Object.Value = True
myActiveX.Object.Locked = False ' Make it False if you wish to enable it
myActiveX.Object.Enabled = False ' Another option to disable
Dim myFormControl As CheckBox
Set myFormControl = ActiveSheet.Shapes("Check Box 1").OLEFormat.Object
myFormControl.Value = True
myFormControl.Enabled = False ' Make it True if you wish to enable it
End Sub
Live GIF demo
You have to write some VBA code in order to do that.
Let's suppose that you have 3 CheckBoxes in your first sheet.
By holding the "Alt" key on your keyboard and the pressing one time the "F11" key, opens Microsoft Visual Basic. ( Alt + F11 )
At your left hand you can see the "VBAProject" tree.
Double click on the "ThisWorkbook" file and copy the following code in the window it will appear:
Private Sub Workbook_Open()
void = uncheckAllCheckboxes()
End Sub
Function uncheckAllCheckboxes()
ThisWorkbook.Worksheets(1).CheckBox1.Value = False
ThisWorkbook.Worksheets(1).CheckBox2.Value = False
ThisWorkbook.Worksheets(1).CheckBox3.Value = False
End Function
Save the excel file as "Excel 97-2003 Workbook" type (.xls)
Close your excel.
Open the file you have previously saved and everything will work fine.
;)
P.S.: It is important to enable macros from your excel settings
I have an excel that hides certain sheets when opening the document. So like
Private Sub Workbook_Open()
Worksheets("screen_2").Visible = xlHidden
Worksheets("screen_3").Visible = xlHidden
end sub
Now I have the case that someone fills in either sheet "screen_2" or "screen_3". Right now when someone has filled in either screen_2 of screen_3 sheet closes the document and reopen the excel sheet "screen_2" and "screen_3" are not visible.
What I would like to achieve is that the first time you open the excel document both sheets should be hidden but that when you fill in one of the two sheets that particular sheet should not be hidden when reopening the excel file.
So when I open the excel sheet and fill in screen_2, save, close en reopen the file "screen_2" sheet should be visible (and screen_3 hidden) and when I open the excel doc and fill in screen_3, save, close en reopen the file "screen_3" sheet should be visible (and screen_2 hidden)
Any thoughts on how I can get this working?
once you get through how to fill a hidden sheet, then you could use:
Private Sub Workbook_Open()
CheckIfHideSheet Worksheets("screen_2")
CheckIfHideSheet Worksheets("screen_3")
End Sub
Sub CheckIfHideSheet(sht As Worksheet)
With sht
If .UsedRange.Address = "$A$1" And IsEmpty(.Cells(1, 1)) Then .Visible = xlHidden
End With
End Sub
I am trying to come up with some vba code to open an input box automatically as soon as the workbook is opened and have the user enter a date and then have the date placed in the A1 cell. I have written the code below but the input box is not pulling up at all it just opens the workbook and moves on.. not sure what is happening. Any and all help is appreciated.
Thanks!
Option Explicit
Private Sub workbook_open()
Dim cellvalue As Variant
Dim ws As Worksheet
Set ws = Worksheets("Workbench Report")
ReShowInputBox: cellvalue = Application.InputBox("Please Enter Todays Date (dd/mm/yyyy)")
If cellvalue = False Then Exit Sub
If IsDate(cellvalue) And CDate(cellvalue) < Date Then
ws.Range("A1").Value = DateValue(cellvalue)
Else: MsgBox ("Invalid Date!")
GoTo ReShowInputBox
End If
End Sub
Your code triggers upon the Workbook opening for me. Try these steps.
Open up Excel and Save As, changing the extension to .XSLM
Open up the VBA Editor (ALT + F11)
In the left-hand window, locate your macro file (the one you just created and named - it's in brackets after "VBA Project"), drilldown to "This Workbook" and double-click it.
Paste your code into the right-hand window
Save the file and re-open.
See attached diagram.
By the way, "cellValue = false" should probably be cellValue = "" since InputBox is returning a string and not a boolean value.
For Workbook_Open events the script needs to reside in the private module (ThisWorkbook)
From Ozgrid:
the Workbook_Open event is a procedure of the Workbook Object and as
such, the Workbook_Open procedure MUST reside in the private module of
the Workbook Object (ThisWorkbook).
Background: I want to use a specific entered text from a TextBox for the default filename in the SaveAs dialog.
I have implemented the following VBA script in my document, a Word 2010 template .dotm
Sub FileSaveAs()
'for testing
Dim fileName As String
fileName = Me.tb_myTextBox.Value & "_MyFileNameToSave"
MsgBox fileName
'use specific file name in save dialog
With Dialogs(wdDialogFileSaveAs)
.Name = fileName
.Show
End With
End Sub
It works fine, when I run it. I saved the .dotm, closed it and reopened it out from the Windows Explorer (means as "end user").
BUT in this case, means after open the template document as "end user" (so that I can save a new doc out of it and not overwrite the template), the content/value of the TextBox is empty, even if I entered something into it.
So, how can I read out the data of the TextBox in "document mode" of a template?
Presumably, the OP's intention was something along the lines of:
Sub FileSaveAs()
Dim StrNm As String
With ActiveDocument
StrNm = Split(.Shapes(1).TextFrame.TextRange.Text, vbCr)(0) & "_MyFileNameToSave"
'use specific file name in save dialog
With Dialogs(wdDialogFileSaveAs)
.Name = StrNm
.Show
End With
End With
End Sub
where .Shapes(1) identifies the particular textbox Shape object.
how can I read out the data of the TextBox in "document mode" of a template?
Not sure what you mean. This works for me:
create a form:
Private Sub btn_OK_Click()
Dim fileName As String
fileName = tb_myTextBox.Value & "_MyFileNameToSave"
With Dialogs(wdDialogFileSaveAs)
.name = fileName
.Show
End With
End Sub
create a sub to call this form:
Sub FileSaveAs()
UserForm1.Show
End Sub
This is all saved in a template / .dotm.
Now, create a document off of the template (double click the template to launch document off of it). Alt + F8 and run the macro from the template (you may have to select the template from the "Macros in" drop down). Result: my form comes up, I enter a name for the document, press ok, and the Word Save As dialog appears with the name I gave to the document.