I don't think this is technically a macro but I don't know what else to call it:
Users want to print individual sections from a report on a sheet. These sections are just named ranges.
Some points:
The file is an xlt file.
It is used as a template to generate an xls file.
I am using Excel 2007, but the users will run a mixture of 2007 and 2003.
When the file loads into excel it asks if I want to enable all macros and I confirm.
The function it is calling is public
I created a series of buttons down the edge of the sheet:
' in a loop
With ActiveSheet.Buttons.Add(rngCurrent.Left + 2, rngCurrent.Top + 1, rngCurrent.Width - 2, rngCurrent.Height - 1)
.Caption = "Print"
.OnAction = "PrintRange"
.Font.Size = 7
.Name = CStr(oSite.SiteID)
End With
However when I click on the button it gives "Cannot run the macro 'filename.xls!PrintRange".
The PrintRange function is in the sheet shtPage while the loop is in a module called modPage.
Why can't I call the function I need and how can I make it work?
Even with your PrintRange sub declared as Public you still need to refer to it more specifically so that Excel can find it, because you have put it in the code section of a worksheet instead of a module.
Change:
.OnAction = "PrintRange"
to
.OnAction = "shtPage.PrintRange"
and it will work just fine.
A caution: If you have renamed your worksheet on the page tab to 'shtPage', but in the VBA project explorer, Excel still refers to the worksheet as 'Sheet1(shtPage)', you will need to use the 'Sheet1' name that Excel recognizes, not the 'shtPage' name that appears on the page tab.
Did you make the PrintRange macro public? It needs to be defined as follows:
Public Sub PrintRange
'// ...'
End Sub
Related
I have a sheet with a bunch of ComboBoxes(form control) and I want to detect when a user changes any one of them and write text in a cell. Using Worksheet_Change on the target cells doesn't work. I have tried a bunch of things that don't work. I'm not sure what needs to be in the private sub line or the if statement.
Private Sub DropDowns_DropButtonClick()
If ActiveSheet.DropDowns.Value > 1 Then
Cells(13, 5).Font.Bold = True
Cells(13, 5).Font.Color = vbRed
Cells(13, 5).Value = "!!! Selections have been changed. !!!"
End If
End Sub
I have tried
ComboBox_AfterUpdate()
ComboBox_Change()
DropDowns_AfterUpdate()
DropsDowns_Change()
and anything else I could find. I've also tried a few different things in the if statement with no luck.
I appreciate any help.
Chris
If I'm reading you correctly, you're comboboxes are in a userform. If I'm correct, simply open your userform in 'Visual Basic' and double click on the relavant combobox. This will open the code pane and create an empty Private Sub routine called 'Private Sub <Combobox Name> ()'.
Enter your code to place your data in the sheet (or whatever else you want) into the subroutine and Bob should be your uncle.
Apologies in advance if there's something I've missed.
RannochRob
Edit...
OK, my mistake, it's a form control.
My first comment is that it's easier to use an activex control if you can... however, with a form control, should (a) Use the cell link box in the 'Format Control' drop down ('Control' tab) to place the result in a cell... however, that result will not be the content of the box but an integer equal to the position of the selected entry on the list of entries in the combobox. You then need to (b) assign a macro to the combobox which will pick up the result and use it to get the required information from the range containing the list of entries. Like I say, much easier with an activex control...
RannochRob
Here's how you can do it by assigning a macro to the combobox (right click on the combobox>assign macro) as #BigBen mentioned in the comments section:
Option Explicit
Sub DropDown1_Change()
Dim sht As Worksheet
Dim dd As DropDown
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'Name of the worksheet in which the combobox is located
Set dd = sht.DropDowns("Drop Down 1") 'name of your combobox
sht.Range("G1").Value = "The selected value is: " & dd.List(dd.Value) 'dd.value returns the index of the selected value
End Sub
You can use the same code for each one of your comboboxes.
For demonstration purposes i have used the following set-up:
You can easily modify the code to best fit your needs.
For using Word I have many public subs and functions stored in Normal.dot. I can use all of them in any module I write in any other Word project. For instance this elementary function stored in Normal.dot,
Public Function BooleanString(b As Boolean) As String
If b Then BooleanString = "Yes!" Else BooleanString = "No!"
End Function
can be used from other projects as
Sub TestNormal()
Debug.Print BooleanString(True)
End Sub
Using Excel I cannot duplicate this behavior: Public subs and functions I have stored in Personal.xlsb (among then the one above) seem not to be visible when called from a module in the VBAProject belonging to any other spreadsheet than Personal.xlsb, resulting in “Sub or Function not defined”.
I have made sure that the Excel option box “Have trust in the VBA project model” (translated from the Danish version) is checked. Also, using “View”, I have made sure that Personal.xlsb is not hidden; it shows up every time I open Excel.
What am I doing wrong or missing here?
Best regards
Holger Nielsen
In Excel, using Functions/Subs of Personal.xlsb is a little different...
In order to use a UDF function, let us say, testMultiplication:
Function testMultiplication(x As Long, y As Long) As Long
testMultiplication = x * y
End Function
it can be called from the (other) workbook sheet cell in this way:
=Personal.xlsb!testMultiplication(3, 4)
In order to call Personal.xlsb Subs and Functions, a reference to the hidden workbook VBAProject is necessary...
To do that, follow the next steps, please:
Firstly give to the Personal.xlsb VBAProject an elocvent name (also to avoid name conflict error, keeping it like VBAProject):
a) In VBE - Project Explorer, right click on its VBAProject, choose VBAProject Properties... and change the standard name in, let us say, 'PersVBAProject`.
b) Then click on a module of the workbook you want to add the reference and use Tools -> References. Find 'PersVBAProject, check it and press OK`.
Starting from that moment the subs/functions of Prsonal.xlsb can be directly called from the specific workbook
In order to programmatically add the reference, use the next code, please:
Sub AddPersonalXLSBRef()
'It needs a reference to 'Microsoft Visual Basic for Applications Extensibility ...'
Dim VBe As VBIDE.VBe, vbPr As VBIDE.VBProject
Dim objRef As VBIDE.Reference, boolFound As Boolean
Set VBe = Application.VBe
Set vbPr = ActiveWorkbook.VBProject
'check if reference to Personal.xlsb exists:
'Please, take care to have its VBAProject name modified in `PersVBProject`
'or something else and, in such a case, adapt the following lines accordingly
For Each objRef In vbPr.References
If objRef.Name = "PersVBProject" Then
MsgBox "Reference to Personal.xlsb already exists"
Exit Sub
End If
Next
'add the reference, if not exist:
vbPr.References.AddFromFile Application.StartupPath & "\Personal.xlsb"
MsgBox "Reference to Personal.xlsb added Successfully"
End Sub
It adds the reference to the active workbook project. The code can be adapted to add it to all open workbooks (not having it added, yet)...
I want to add item to a combobox found in excel worksheet from text box which is located in the user form When button is clicked.i see the value added to combobox but it will become empty when I close and reopens the workbook.can any one help me handling this?
Thank u for you're fast response first
thank you both for your feedback and correction.let me make more clear my concern
Create a workbook and save as xlsm.
On the first worksheet define user name as follows:
Name: dn_cmb_items
Range: =""
Using the developer ribbon add an Excel (not ActiveX) combobox onto the Worksheet1 and set its list-by-range to dn_cmb_items
Open VBA editor and add a user form to the workbook, name it as frm_add_cmb_item and set ShowModal to False.
Drop a text box to the form and name it tb_item_text.
Drop a button to the form, name it cmb_add and from its context menu choose View code. This creates the click event handler.
Implement the handler as follows:
Private Sub cmb_add_Click()
Dim v_r As Range, v_n As Name
Set v_n = Names("dn_cmb_items")
If v_n.Value = "=""""" Then
v_n.Value = "=" & Worksheets(1).Name & "!$A$1:$A$1"
v_n.RefersToRange.Value = tb_item_text.Text
Else
Set v_r = v_n.RefersToRange
Set v_r = v_r.Cells(v_r.Rows.Count + 1, 1)
v_r.Value = tb_item_text.Text
v_n.Value = "=" & Worksheets(1).Name & "!$A$1:" & v_r.Address(True, True)
End If
End Sub
Drop onto the workshet a button, then create/set a macros in the workbook. Implement the created macros with the code frm_add_cmb_item.Show.
In the VBA Editor from the Debug menu choose Compile. Then save the VBAProject as well as the workbook. That's all for the coding.
Switch to the worksheet, show the form.
Now when you enter some to the textbox, then click the cmb_add button, a new item will be added to the A column at the end thus changing the value of dn_cmb_items assigned to the combobox on the worksheet.
See the screenshorts attached:
Initial state:
1 added:
2 added:
PS
I have the ready workbook with all the code. Where should I upload it?
Be specific with your question and always post the relevant code, so that it will become easy to solve it for others.
If you want to see the data while executing the userform, just write the required data in userform_activate or Initialize. before executing it will take the values and shows up in the combobox.
the input which you are taking from the worksheet just write those values in another worksheet so that whenever you open the workbook the values will not get erased.
I have created a checkbox on my Excel Work sheet, using design mode I have leftclicked it and named it ChkV, and I wrote a VBA code but when I run it I get an message telling that the variable is not defined.
If ChkV.Value = True Then
' my code
End If
Did I not label the check box correctly, what am I doing wrong ? How should I fix the mistake?
Should it not be
If activesheet.Checkboxes("ChkV") = xlOn Then
'your code
End If
?
You have this error when you call your code outside Sheet module where your checkbox is located. To improve your code you need to add references to sheet where checkbox belongs to, like:
If Sheets("Sheet 1").ChkV.Value = True Then
' my code
End If
Hi i was breaking my head over and over again for this, but searching i found that you need to refer by the CodeName's sheet or using OleObjects
By Code Name is how you see in the VBA Project tree:
Hoja1.[CheckBoxName].Value
'' In English or what ever you may call your sheet is:
Sheet1.[CheckBoxName].Value
By OleObjects is:
Dim Wks as Worksheet
Set Wks = Worksheets("[SheetName]") '' in this case "Prueba"
Wks.OLEObjects("[CheckBoxName]").Object.Value
Note that my Excel is in Spanish thats why you see Hoja1, in English is Sheet1 and you can find something else here.
I have a userform in Excel that works as a calculator.
In this userform I have two ComboBoxs (1 & 2)
In VBA editor, with ComboBox1 selected, In Properties, under Rowsourse I have: Sheet1!a4:a5
In Sheet1, A4 = Auckland and A5 = Christchurch
This is fine and when I run the userform there is a drop down arrow with the two options (Auckland or Christchurch).
However my problem is that when you open this workbook I have a VBA command to hide it from the users sight, leaving them only the userform to work with which is what is desired.
The issue is that if you have another workbook open then open this calculator workbook (which automatically hides itself). Then the combobox list is populated by Sheet1!a4:a5 on the other workbook that was already open, not the workbook that actually contains "Auckland" & "Christchurch" from which the userform is from.
I have tried making the Rowsource for the comboboxes more specific by putting the following in the rowsource box in properties: [book1.xlsm]sheet1!a4:a5 but this comes up with a "Invalid Property Value" error message.
I have also tried making a:
Private Sub Userform1_Initialize()
ComboBox1.Additem "Auckland"
ComboBox1.Additem "Christchurch"
End Sub
And also tried this:
Private Sub Userform1_Initialize()
ComboBox1.RowSource = Workbooks("book1.xlsm").Sheets("Sheet1").Range("a4:a5").Value
End Sub
However with both codes when it opens and runs now the comboboxes are empty and there is no list.
I think the easist solution would be to somehow put the full path (including workbook name) into the rowsource box under properties. But I must be missing something as its coming up with that error for me?
All help would be greatly appreciated.
Thanks
You are missing ' in your full path row source.
It should be like this:
Me.ComboBox1.RowSource = "'[book1.xlsm]Sheet1'!$A$4:$A$5"
I have similar question that can be found HERE.
Set the row source property of the combobox as: SheetName!$Col$Row:$Col$Row, e.g.: Location!$A$1:$A$3.
You may try adding this code on userform:
Private Sub UserForm_Initialize()
ComboBox1.list = Array("Auckland","Christchurch")
End Sub
Then set Combobox propert "MatchEntry" to "1".