viernes, 25 de agosto de 2023

Excel - Label text linked to cells

Dim counter As Integer

Sub InsertCellTextIntoTextbox()
    counter = counter + 1
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Dim xCoord As Double
    Dim yCoord As Double
    
    ' Get the coordinates of the active cell
    xCoord = ActiveCell.Left
    yCoord = ActiveCell.Top
    
    Dim shp As Shape
    Set shp = ws.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
                                Left:=xCoord, Top:=yCoord, Width:=100, Height:=30)
    
    ' Get the text from the cell corresponding to the counter value
    shp.TextFrame.Characters.Text = ws.Cells(counter, 1).Value
    
    shp.TextFrame.Characters.Font.Size = 16
    shp.TextFrame.Characters.Font.Bold = True
End Sub

Sub AssignShortcutKey()
    Application.OnKey "^+B", "InsertCellTextIntoTextbox" ' Ctrl + Shift + B
End Sub
 

Excel - Label insert When Ctrl+Shift+B

 Dim counter As Integer

Sub InsertIncrementedLabel()
    counter = counter + 1
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    Dim xCoord As Double
    Dim yCoord As Double
    
    ' Get the coordinates of the active cell
    xCoord = ActiveCell.Left
    yCoord = ActiveCell.Top
    
    Dim shp As Shape
    Set shp = ws.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
                                Left:=xCoord, Top:=yCoord, Width:=100, Height:=30)
    shp.TextFrame.Characters.Text = "Label " & counter
    shp.TextFrame.Characters.Font.Size = 16
    shp.TextFrame.Characters.Font.Bold = True
End Sub

Sub AssignShortcutKey()
    Application.OnKey "^+B", "InsertIncrementedLabel" ' Ctrl + Shift + B
End Sub