I want to iterate over the command buttons and replace the command button captions with the current i value. Each command button is named D plus a number (D1, D2, D3, D4, D5). I tried to use
With Sheet1
For i = 1 To 5
Controls("D" & (i)).Caption = i
Next
End With
But I get a compile error: Sub or function not defined.
The sub I am writing is contained in module1 and the command buttons are placed directly in Sheet1 with ActiveX command buttons (Microsoft Forms 2.0 Command Button). I have used the line: Controls("D" & (i)).Caption before with a form with no issues but I don’t want to use a form for this project. Is there a way to concatenate a letter and a variable in a for loop to match the names of the command buttons created and the current number being set to the caption of that button?
Related
I have an Excel project with a lot of macros.
What I want is assign macros to buttons in the ribbons.
For that I created macros like:
Sub R10_parallel_device()
help.helpON ("parallel_device ")
Call sub_novelty_claims("parallel_device")
End Sub
So the three conditions are met:
a) the macro SUB R10_parallel_device() do not accept parameters
b) it is not private
c) It is not hide
when I go to the list of macros I see all the list of macros named Rnn but all THE BUTTONS ARE GREY OUT EXCEPTFOR CREATE.
Now if I click in that sub for instance SUB R10_parallel_device() I can not edit it and if I click "create" excel sends me to a new created module where
Sub R10_parallel_device()
End Sub
appears.
&
When I go to file/options/ribbon if I want to add that macro to a button it is listed and it is possible to assigne the macro to a button but it would not run, giving the error that such a macro is not found.
NOTE: I checked which macro are listed thisworkbook/all/ etc.
note2: this did not help me. this neither
thx
Confirmed it's the macro names - go to Developer -> View Code (or type Alt + f11), then select the module with your code in it and rename them. For whatever reason it's that R# syntax:
I am the beginner of VBA and need some help from you in code.
My problem is that I want to make some kind of UserForm using VBA in Excel to generate new numbers for new ticket.
The UserForm will be needed to manage tickets about received problems.
The case is that I want to make new number of ticket by clicking button "New Ticket". After that, in UserForm, the "Ticket Number" field will be blocked and show assigned new ticket number, "Date Opened" field will show current date automatically and "Accept" button will change text to "Create" button and will be enabled. After clicking "Create" button, UserForm will close and write line about ticket with details in last row in Excel sheet.
I also want to have ticket number starting with specific number e.g. 60555, 60556, 60557 etc. The problem is that now I am receiving numbers 1, 2, 3 etc.
Below you can see some screenshots of problem and part of created code.
UserForm "Manage Tickets"
After "Create" button, what is showing in last row in Excel Sheet
What I want to show in last row in Excel Sheet after "Create" button
If there is a way also to show text "[BHD#?????]" in UserForm and add it by UserForm, it will be very helpful for me!
Part of code which is responsible for "New Ticket" button:
Private Sub btwNewTicket_Click()
ClearFields
LastRow = ThisWorkbook.Sheets("Issue Log").Cells(Rows.Count, 2).End(xlUp).End(xlUp).Row - 4
Me.tbTicketNo.Value = LastRow
Me.tbTicketNo.Locked = True
Me.tbDateOpen = Date
Me.btnAccept.Caption = "Create"
Me.btnAccept.Enabled = True
End Sub
Thanks in advance!
You can concatenate strings with the & symbol. Hence a possible solution for your needs would be:
Me.tbTicketNo.Value = "[BHD#" & Format(LastRow,"00000") &"]"
The Format(LastRow, "00000") makes the value always five digits long. With LastRow=1 the output would be [BHD#00001].
If you want to start by a value of 60555 than just add this value
Me.tbTicketNo.Value = "[BHD#" & 60555+LastRow &"]"
With LastRow=1 the output would be [BHD#60556]
How to make the conditions of radio button dynamic? for eg. if i have one radio button "yes" in A100 and the condition related to it is appearing in A103. And, if i am inserting the row i want both(button and its value) to get shifted accordingly .
Sub OptionButton21_Click()
Range("A103") = " Not Applicable "
End Sub
Sub DynamicRange()
'Best used when only your target data is on the worksheet
'Refresh UsedRange (get rid of "Ghost" cells)
Worksheets("Sheet1").UsedRange
'Select UsedRange
Worksheets("Sheet1").UsedRange.Select
End Sub
Thanks in advance
Use a named range
1 -Select the target cell (i.e. A103) and give it a name in the "Name Box" on the top-left of the screen. Suppose you chose the name "MyTargetName"
2- Make the control to move with cells.
Right-click the control --> Format control --> Properties --> Move but dont size with cells
3- In VBA code, refer to the target cell by its name instead of its address:
Sub OptionButton21_Click()
Range("MyTargetName") = " Not Applicable "
End Sub
This will make the referenced target shift correctly, along with the control, whenever new rows are inserted.
I have an an Excel sheet that uses VBA to generate some form control buttons on the fly.
the buttons are cleared and then new buttons are created.
I have noticed that even though old buttons are deleted Excel is keeping an internal register of each button. New buttons have button name of over 11K
I don't know if there is some sort of limit excel will allow for this and I don't want to run out of buttons.
I am not sure if this growing registry of buttons past is causing the file size to grow.
I would like to be able to reset the increment back to 0
Anyone have any idea how I can go back to button_0 ? (without starting a whole new Excel sheet)
Seems internal button count is sheet specific. Solution is to copy sheet, rename old sheet, then rename new sheet to old sheet name. Then delete old sheet. Viola! button count reset.
I found the answer somewhere in a forum, but I couldn't retrace it.
You could also pragmatically create the button by a function/sub-routine as a workaround.
Example: below function adds a button and limits the count to a fixed number (in my case, it is the total buttons available).
In my sheet, the first button is named "Button 687" (before I use the macro) and the second button is named "Button 2".
But, it is quite not dynamic when you want to add Drop Down or other form control etc. Macro recording helps you figure out the syntax, methods and properties of the form control you want to add though.
I am not sure why "buttons" is not listed in Properties/Methods after you typed "Activesheet." but Activesheet.Buttons(1).Name or Activesheet.buttons.add are valid codes.
Public Sub Add_Button(ButtonLabel$, ButtonSize#, BFontSize#, BFontName$)
Dim Button__ As Object
Dim One_Unit#: Per_Unit = Application.CentimetersToPoints(1)
'Creates a button at the sheet's first cell (row 1, col 1) with the height and width being 1cm.
Set Button__ = ActiveSheet.Buttons.Add(1, 1, One_Unit, One_Unit)
'button count will be restricted to the number set by user.
With Button__
.Name = "Button " & ActiveSheet.Buttons.Count
With .Characters
.Text = UCase(BLabel)
.Font.Name = BFontName
.Font.Size = BFontSize
End With
End With
End Sub
Sub AddAButton()
Add_Button BLabel:="SAVE"
Debug.Print ActiveSheet.Buttons(ActiveSheet.Buttons.Count).Name
End Sub
I have a custom content control in Microsoft Word from a third party I am trying to resize the width and height of. Normal content control selection via VBA is not working because this control has no Title or Tag. However, if I manually select the object and resize it programmatically using "Selection.ShapeRange.Height = x" or ShapeRange.Width it does work. So to do it all programmatically I need to determine the name of the "selection" without having to manually select it.
Is there a way to "inspect" the complete reference to the currently selected object in word, so we can then get a starting point to work with it in VBA?
It's hard to know what type of object your dealing with. I tested this by inserting a blank ActiveX image control, selecting it and then running the macro. The code has two methods but one is commented out.
Sub FindName()
MsgBox (Selection.Fields.Item(1).OLEFormat.ClassType)
'MsgBox (Selection.InlineShapes.Item(1).OLEFormat.ClassType)
MsgBox (Selection.InlineShapes.Item(1).Field.Index)
MsgBox (Selection.InlineShapes.Item(1).AlternativeText)
'Show current name
MsgBox (Selection.Fields.Item(1).OLEFormat.Object.Name)
'Set new name
Selection.Fields.Item(1).OLEFormat.Object.Name = "Image5"
'Re-display name to show that it changed
MsgBox (Selection.Fields.Item(1).OLEFormat.Object.Name)
End Sub
The result was this: