Resize textboxes depending on cell value

AndyMb

New Member
Joined
Jul 1, 2015
Messages
20
Is it possible to amend the following code to adjust the size of each textbox that's created?

Code:
Sub TextBox()

Dim Cell As Range, Row As Range
Dim sText As String
Dim iCounter As Long

For Each Row In Range("L2:L1000").Rows
    For Each Cell In Row.Cells
        If sText = "" Then
            sText = Cell
        Else
            sText = sText & " " & Cell
        End If
    Next Cell
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 1000, iCounter, 192, 75).Select
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = sText
    iCounter = iCounter + 100
    sText = ""
Next Row

End Sub
All I want to do is adjust the width of the box by a number that will be in a column.

The code currently creates a text box for each row and puts the text that's in column L into the textbox

If I had a width in say column M is it possible to adjust the column width using this number for each textbox created?

I tried to come up with a solution that would treat the textbox width as a variable so taking the first value, which in this case would be column M row 2 and making that a value of 'w'

Would it be possible to then change the textbox creation line to something like this:


Code:
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 1000, iCounter, [COLOR="#FF0000"]w[/COLOR], 75).Select
where 'w' is the value of M2. the loop would then pick up the next value of M3 etc.

I came up with this but it doesn't seem to create the textboxes. Changes in red.


Code:
Sub TextBoxLine1()

Dim Cell As Range, Row As Range
Dim sText As String
Dim iCounter As Long

[COLOR="#FF0000"]Dim w As Long
Dim rCell As Range
Dim rRng As Range[/COLOR]

For Each Row In Range("A4:A1000").Rows
    For Each Cell In Row.Cells

        If sText = "" Then
            sText = Cell
        Else
            sText = sText & " " & Cell
        End If
        
        
[COLOR="#FF0000"]    Set rRng = Range("C4:C1000")
    For Each rCell In rRng.Rows
        Let w = rCell.Value
    Next rCell[/COLOR]
        

    Next Cell
  
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 811, iCounter, [COLOR="#FF0000"]w[/COLOR], 75).Select
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = sText
    iCounter = iCounter + 75
    sText = ""
Next Row

End Sub
thanks

Andy
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I worked it out for myself!

Those interested I changed the code to the following:

Code:
Sub TextBoxLine2()

Dim Cell As Range, Row As Range
Dim sText As String
Dim iCounter As Long
Dim i As Long
Dim iWidth As Long
i = 5

For Each Row In Range("A4:A1000").Rows
For Each Cell In Row.Cells
  iWidth = Cells(i, 2)
        If sText = "" Then
            sText = Cell
        Else
            sText = sText & " " & Cell
        End If

    Next Cell
  
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 811, iCounter, iWidth, 75).Select
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = sText
    iCounter = iCounter + 75
    sText = ""
    i = i + 1

Next Row

End Sub

Andy
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,258
Members
452,901
Latest member
LisaGo

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