user2878699
user2878699

Reputation: 41

Excel auto fit column in VB.NET

I have here my code in VB6 and its working well.

 For CLms = 1 To 10
      ws.Columns(CLms).AutoFit '<---Autofilt data on XL sheet
 Next CLms

I have searched on what is the equivalent or alternative on how to make the excel columns width autofit in VB.NET, but I'm still not getting the exact AutoFit function.

Any help will be appreciated.

Upvotes: 4

Views: 38205

Answers (6)

llegnaro
llegnaro

Reputation: 13

As mentioned in the @RPh_Coder comment, you can also do this by:

xlSheet.Cells.EntireColumn.AutoFit()

Upvotes: 0

Caves
Caves

Reputation: 1

I used the ff. codes to autofit the used range.

xlWorkSheet.UsedRange.EntireColumn.AutoFit()

Upvotes: -1

ziad mansour
ziad mansour

Reputation: 349

hello i try all these solution..but no one was work.. please take the right solution:

    With oSheet

        .PageSetup.Zoom = False
        .PageSetup.FitToPagesWide = 1
        .PageSetup.FitToPagesTall = False

    End With

Upvotes: 0

Jorge Valdes
Jorge Valdes

Reputation: 11

For example if you don't know the number of columns you can autofit the complete Row.

xlSheet.Rows.Item(1).EntireColumn.AutoFit()

Upvotes: 1

aphoria
aphoria

Reputation: 20189

This is how I've done it in vb.net.

xlSheet.Range("A1:X1").EntireColumn.AutoFit()

Upvotes: 13

Matt Wilko
Matt Wilko

Reputation: 27322

The methods for manipulating an Excel column are the same regardless of whether you are calling the code from VB6 or VB.NET. So there should be no change to your code if it works in VB6.

However for some help - Have a look at this full example in VB.NET that includes autofit

Upvotes: 0

Related Questions