Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofit Method of Range Class Failed (Run Time Error 1004)

Tags:

excel

vba

This is just part of my code. the value from the textbox here already gets copied to the specific cell in the Bank Certification worksheet. I need to make sure that cell C5 is specifically fitted regardless of the length of the text i inputted in the textbox. I tried interchanging range with cells to no avail. This problem seems so simple but I don't know why it doesn't work...

Dim counterparty As String  
counterparty = Sheet1.txt1.Text

Range("C5").Value = counterparty 

Sheets("Bank Certification").Select

Range("C5").Select 

Selection.AutoFit
like image 672
Benedict Solpico Avatar asked Feb 27 '15 06:02

Benedict Solpico


People also ask

How do you AutoFit a range of cells?

Change the column width to automatically fit the contents (auto fit) Select the column or columns that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Column Width.

How do you AutoFit in VBA?

In VBA, you can use the AutoFit method to auto-fit rows, columns, and even an entire worksheet. You need to specify the range, and then you can use the AutoFit method.

What does select method of range class failed mean?

# 3 – VBA Run Time Error 1004: Select Method of Range class failed: It usually occurs when we try to select the cells other than the active sheet without making the sheet select or active.


2 Answers

Try

Dim counterparty As String
counterparty = Sheet1.txt1.Text

Range("C5").Value = counterparty

Sheets("Bank Certification").Select

Columns("C:C").Autofit
like image 119
izzymo Avatar answered Oct 06 '22 01:10

izzymo


Other answers correctly state that AutoFit must be used with a column, not just a cell. However, there are some nuances to using AutoFit that I didn't understand until I started experimenting.

Either of the first two statements below will use all values in column C to AutoFit the width of the column. That means if there is a value in some other cell in column C (for example C10) that is wider than the value in C5, it will fit the column to the widest cell in column C (for example C10).

Range("C5").EntireColumn.AutoFit     ' Will fit to widest cell in column
Range("C:C").AutoFit                 ' Will fit to widest cell in column

If you want to just fit the column on 1 cell (or a certain range of cells, but not the whole column or columns), use a statement like this:

Range("C5").Columns.AutoFit          ' Will fit column C to width of cell C5

And of course, it's always better form to write code like this when you can:

  1. Fully qualify the range unless you're absolutely sure you'll only be working with one worksheet
  2. Use Named Ranges or Range objects.

For example:

Workbooks("MyWorkbook.xlsm").Sheets("Sheet1").Range("MyData").Columns.AutoFit
'  or
Set AutoFitRange = Workbooks("MyWorkbook.xlsm").Sheets("Sheet1").Range("C5")
AutoFitRange.Columns.AutoFit
like image 37
GlennFromIowa Avatar answered Oct 05 '22 23:10

GlennFromIowa