Locking a Command Button to a cell:VBA

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
821
Office Version
  1. 365
Platform
  1. Windows
I am currently using the code below to create my command button.The problem im having is that columns are adjusted sometimes so the button gets in the way.
PHP:
ActiveSheet.Buttons.Add(1200.15384615385, 26.5846153846154, 179.446153846154, 23.8153846153846).Select
Is there away in the code to lock it in a cell????
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Maybe like this

Code:
Dim bt As Button
Set bt = ActiveSheet.Buttons.Add(1200.15384615385, 26.5846153846154, 179.446153846154, 23.8153846153846)
bt.Placement = xlMove
 
Upvote 0
Peter, can I insert just this part
ActiveSheet.Buttons.Add(1200.15384615385, 26.5846153846154, 179.446153846154, 23.8153846153846)
bt.Placement = xlMove
<!-- / message --><!-- sig -->
 
Upvote 0
Peter, I tried it and it errored out,
Sub FINALIZED_BY_QC()

Dim newFileName As String
Dim appendtext As String
With ActiveSheet
.Unprotect Password:="1288"
With .Range("L23").Interior
.Pattern = xlSolid
.PatternColorIndex = 1
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
appendtext = "-FINAL"
.Range("L23").FormulaR1C1 = appendtext



Cells.Select
Selection.Locked = True
Range("V4:V21").Select
Selection.Locked = False
Selection.FormulaHidden = False
Range("V3").Select
ActiveCell.FormulaR1C1 = "Assistant Purchasing/" & Chr(10) & "Dept. Comments"
Range("V3").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
End With
Range("V3:V21").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Range("V3").Select
ActiveWorkbook.Save
'ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells

Dim bt As Button
Set bt = ActiveSheet.Buttons.Add(1250.15384615385, 26.5846153846154, 179.446153846154, 23.8153846153846)
bt.Placement = xlMove
'ActiveSheet.Buttons.Add(1250.15384615385, 26.5846153846154, 179.446153846154,
' _23.8153846153846).Select
Selection.OnAction = "PURCH_COMMENTS"
Selection.Characters.Text = "SAVE COMMENTS"
With Selection.Characters(Start:=1, Length:=13).Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
End With






'.UsedRange.Locked = True
.Protect Password:="1288", DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
With ActiveWorkbook
oldFileName = .FullName
newFileName = Left(.FullName, InStrRev(.FullName, ".xls") - 1) _
& appendtext
.SaveAs Filename:=newFileName
End With
Kill oldFileName

End Sub
 
Upvote 0
What error did you get? This worked for me

Code:
Sub test()
Dim bt As Button
Set bt = ActiveSheet.Buttons.Add(1200.15384615385, 26.5846153846154, 179.446153846154, 23.8153846153846)
bt.Placement = xlMove
End Sub
 
Upvote 0
Peter, It made it down on the code above untill it got to the line under were it built the command button


Actually its the line that says
Selection.OnAction = "PURCH_COMMENTS"
Set bt = ActiveSheet.Buttons.Add(1220.15384615385, 26.5846153846154, 179.446153846154, 23.8153846153846)
bt.Placement = xlMove
 
Last edited:
Upvote 0
Try

Code:
Dim bt As Button
Set bt = ActiveSheet.Buttons.Add(1250.15384615385, 26.5846153846154, 179.446153846154, 23.8153846153846)
With bt
    .Placement = xlMove
    .OnAction = "PURCH_COMMENTS"
    .Characters.Text = "SAVE COMMENTS"
    With .Characters(Start:=1, Length:=13).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
    End With
End With
 
Upvote 0

Forum statistics

Threads
1,214,566
Messages
6,120,266
Members
448,953
Latest member
Dutchie_1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top