If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Target.Address wont triger AutoFit code in Excel 07

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-01-10, 14:29
Cheek Cheek is offline
Registered User
 
Join Date: Dec 2010
Posts: 3
Target.Address wont triger AutoFit code in Excel 07

Hi, Im trying to get rows to autofit every time cell b5 changes.

In Cell b5, there is a drop-down list via data validation. The data in the range of B10:E61 will automatically change referencing to the same location of another worksheet specified in the drop-down list of b5.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$b$5" Then
Rows("10:61").Select
Rows("10:61").EntireRow.AutoFit
End If
End Sub


Thanks
Reply With Quote
  #2 (permalink)  
Old 12-01-10, 14:35
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
probably best to ask this in the Excel forum....
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 12-01-10, 14:42
Cheek Cheek is offline
Registered User
 
Join Date: Dec 2010
Posts: 3
Thanks.

Im new to this, and didn't look far down enough to See MS Excel before Visual Basics.

Anyways when I went to look at MS Excel Forum, this appears there. Does that mean it is under that forum now? Ot do i need to create a new thread directly in that forum?

Thanks and appreciate the tip.
Reply With Quote
  #4 (permalink)  
Old 12-01-10, 15:15
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Welcome to the forum.

I suspect that the event handler is being called but that your logical check has a flaw: the address should have an uppercase "B". Also, there's no need to select the rows to autofit them using code.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$5" Then
        Me.Rows("10:61").EntireRow.AutoFit
    End If
End Sub
An easy way to tell if your event handler is being called is to add a breakpoint. If you find it is not being called then it probably means that events have been disabled. To re-enable them type this into the immediate window:
Code:
Application.EnableEvents=True
However, I think it is most likely that the problem was the lower case "b" in the code you posted.

Hope that helps...
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #5 (permalink)  
Old 12-01-10, 15:28
Cheek Cheek is offline
Registered User
 
Join Date: Dec 2010
Posts: 3
Thanks Colin!

I've got codes to work with lowercases before so not sure if that had to do with what was wrong. Anyhow, I tested the code you provided and it works perfectly.

Appreciate it.

Cheek
Reply With Quote
Reply

Tags
autofit, excel, target.address, vba, worksheet_change

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On