Autofit VBA question

helpme20

Board Regular
Joined
Aug 28, 2010
Messages
102
I am trying to write a VBA formula using the Workbook Sheetchange formula and I need some help please.

Private Sub Workbook_Open()

End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Columns().AutoFit
End Sub

I only want it to work with one of my sheets. it is named ProductFinder and I only want it to work for the data in cells, $Q$6:$W$104 (BTW I have named that cell range "Overall" if that helps)



Can someone help me with this? Thank you!
 
in Sheet4 the Product Finder sheet, when you enter anything in cell T4, it returns the information from Sheet3 to columns Q thru W. each row has formulas in cells Q6 thru W104

q6 =IFERROR(VLOOKUP(ROWS($Q$6:Q6),$H$6:$O$104,2,0),"")

r6 =IFERROR(VLOOKUP(ROWS($Q$6:U6),$H$6:$O$104,8,0),"")
...
That is very curious, as those formulas do not appear to be looking at cell T4 at all. So, I don't see how entering/changing the value in cell T4 would have any impact on any of those formulas.
Unless, you are using Conditional Formatting on those cells that looks at whether or not T4 has a value, and if it doesn't, it hides them (by making the text appear white).
Can you explain the relationship between T4 and those cells?
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If anyone else has any suggestions.

I can send the spreadsheet to any of you to look at if you're willing.
 
Upvote 0
That is very curious, as those formulas do not appear to be looking at cell T4 at all. So, I don't see how entering/changing the value in cell T4 would have any impact on any of those formulas.
Unless, you are using Conditional Formatting on those cells that looks at whether or not T4 has a value, and if it doesn't, it hides them (by making the text appear white).
Can you explain the relationship between T4 and those cells?


T4 is tied into a search in column F6-f104

=IF(ISNUMBER(SEARCH($T$4,@INDIRECT($B6))),MAX(F$5:$F5)+1,0)
 
Upvote 0
Have you saved the workbook as macro enabled?
 
Upvote 0
yes, I have save it as macro enabled sheet .xlsm file.

I need it to autofit both the column width and row height

1598289936465.png
 
Upvote 0
Do you have wrap text turned on for those columns?
 
Upvote 0
In that case you will have to remove it, if you want to autofit the columns & rows
Try
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Address(0, 0) = "T4" Then
      With Range("Q6:W104")
         .WrapText = False
         .Columns.AutoFit
         .Rows.AutoFit
      End With
   End If
End Sub
 
Upvote 0
In that case you will have to remove it, if you want to autofit the columns & rows
Try
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Address(0, 0) = "T4" Then
      With Range("Q6:W104")
         .WrapText = False
         .Columns.AutoFit
         .Rows.AutoFit
      End With
   End If
End Sub
not sure why it doesn't work.
 
Upvote 0
If you use this
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Address(0, 0) = "T4" Then
      MsgBox "running"
      With Range("Q6:W104")
         .WrapText = False
         .Columns.AutoFit
         .Rows.AutoFit
      End With
   End If
End Sub
do you get the message box appear?
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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