Pookiemeister
Well-known Member
- Joined
- Jan 6, 2012
- Messages
- 626
- Office Version
- 365
- 2010
- Platform
- Windows
Named Cells Spreadsheet Layout:
VendorName =(C7:E7)
VendorNumber=(C8:E8)
QuoteNumber=(C9:E:9)
PONumber=(L7:M7)
Quantity=(C19:C:32)
ItemNum=(D19:E32)
1. ItemNum19=(D19:E19) Merged Cell
2. ItemNum20=(D20:E20) Merged Cell
.....
14. ItemNum32=(D32:E32) Merged Cell
Description=(F19:K32)
1. Description19=(F19:K19) Merged Cell
2. Description20=(F20:K20) Merged Cell
.....
14. Description32=(F32:K32) Merged Cell
UnitCost=(L19:L32)
1. UnitCost19=(L19)
2. UnitCost20=(L20)
......
14. UnitCost32=(L32)
When userform loads it select named cell "VendorName" and clears any values inside that cell, then jumps to the Private Sub Worksheet_Change(ByVal Target As Range). This sub looks at the cell and auto sizes all cells including the merged cells. Then returns back to the userform initialize and goes to then next line of code. When the sub gets to the Named range called Quantity, it select the entire range, then it will loop through each cell in that range and autosizes that cell, with or without text. I believe the problem occurs when the code reaches the ItemNum but code that is highlighted in yellow Debug.Print ActiveCell.Name.Name in the Private Sub Worksheet_Change(ByVal Target As Range). Please let me know if you have any question. Thank you.
VendorName =(C7:E7)
VendorNumber=(C8:E8)
QuoteNumber=(C9:E:9)
PONumber=(L7:M7)
Quantity=(C19:C:32)
- Quantity19=(C19)
- Quantity20=(C20)
ItemNum=(D19:E32)
1. ItemNum19=(D19:E19) Merged Cell
2. ItemNum20=(D20:E20) Merged Cell
.....
14. ItemNum32=(D32:E32) Merged Cell
Description=(F19:K32)
1. Description19=(F19:K19) Merged Cell
2. Description20=(F20:K20) Merged Cell
.....
14. Description32=(F32:K32) Merged Cell
UnitCost=(L19:L32)
1. UnitCost19=(L19)
2. UnitCost20=(L20)
......
14. UnitCost32=(L32)
When userform loads it select named cell "VendorName" and clears any values inside that cell, then jumps to the Private Sub Worksheet_Change(ByVal Target As Range). This sub looks at the cell and auto sizes all cells including the merged cells. Then returns back to the userform initialize and goes to then next line of code. When the sub gets to the Named range called Quantity, it select the entire range, then it will loop through each cell in that range and autosizes that cell, with or without text. I believe the problem occurs when the code reaches the ItemNum but code that is highlighted in yellow Debug.Print ActiveCell.Name.Name in the Private Sub Worksheet_Change(ByVal Target As Range). Please let me know if you have any question. Thank you.
VBA Code:
Private Sub UserForm_Initialize()
Worksheets("Purchase Order").Select
Range("VendorName").Select
Range("VendorName").Value = ""
Range("VendorNumber").Select
Range("VendorNumber").Value = ""
Range("QuoteNumber").Select
Range("QuoteNumber").Value = ""
Range("PONumber").Select
Range("PONumber").Value = ""
Range("Quantity").Select
Debug.Print ActiveCell.Address
Range("Quantity").Value = ""
Range("ItemNum").Select
Debug.Print ActiveCell.Address
Range("ItemNum").Value = ""
Range("Description").Select
Range("Description").Value = ""
Range("UnitCost").Select
Range("UnitCost").Value = ""
Me.Caption = "Purchase Order Form " & " Date: " & Format(Now, "mm/dd/yyyy") & " Time: " & Format(Now, "hh:mm")
answ = 0
End Sub
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i, x As Long
Dim cellName As String
Dim cellName1 As String
'****************************************************
Debug.Print Selection.Cells.Address
ActiveCell.Select
Debug.Print ActiveCell.Name.Name
Debug.Print str01
If Not IsNumeric(Right(ActiveCell.Name.Name, 1)) Then
str01 = ActiveCell.Name.Name
Select Case str01
Case Is = "VendorName"
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
Case Is = "VendorNumber"
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
Case Is = "QuoteNumber"
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
Case Is = "PONumber"
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
End Select
Else
str01 = ActiveCell.Name.Name
If IsNumeric(Right(str01, 1)) Then
For i = 19 To 32
Cells(i, 3).Select
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
Next i
End If
End If
'****************************************************
End Sub