VBA to Move a Text Box Based on Values in a Column

cstepp

New Member
Joined
Jul 4, 2016
Messages
20
I have Text Boxes that I would like to move based on the values in Column A. The values in Column A change as new data is added. What I would like is for the Text Boxes to move the correct row based on the value in Column A. The lookup values for Column A will come from a Named Range/Table. To help clarify, I have names in Column A with new names added regularly that are sorted alphabetically. I have a separate list of some of those names that have a Text Box(es) associated with it. As the new names are added, I would like the text box(es) to move with the name. The The Text Boxes are in Columns for eg. E & F. The reason I am using Text Boxes is because the data itself is in a Pivot table. As the Pivot Table grows with new names, I want the Text Boxes to follow.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
So I got the textbox to move to a specified column and row, which I set manually for now. Now I need to search for names in a range and move the textboxes to the corresponding row where those names exists in column 1.

VBA Code:
Sub MoveTextBox()
    Dim ws As Worksheet
    Dim textBox As Shape
    Dim targetRow As Integer
    Dim targetColumn As Integer
    
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    targetRow = 5 ' Change to the desired row number
    targetColumn = 6 ' Change to the desired column number
    
    Dim leftPos As Double
    leftPos = ws.Cells(targetRow, targetColumn).Left
    
    Dim topPos As Double
    topPos = ws.Cells(targetRow, targetColumn).Top
    
    On Error Resume Next
    Set textBox = ws.Shapes("TextBox1")
    On Error GoTo 0
    
    If Not textBox Is Nothing Then
        
        With textBox
            .Left = leftPos
            .Top = topPos
          End With
    Else
        MsgBox "Text box not found."
        
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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