Also just curious if there was a way to make the macro input only the
most current selection into the corresponding cell, and eliminate the
previous inputs. For Example, if I clicked for a 7'2" wide 7' tall / 2
Horse / 4'5" Short Wall under "Trailer Shell (No LQ)" -- the macro
inputs the corresponding information directly below the Trailer shell no
lq and list price -- (we will call this step A) then say I clicked on
the 7'2" WIDE / 7' Tall / 6 Horse / 4'5" Short Wall under "Living
Quarter Trailer" --the macro inputs the corresponding information
directly below the Living Quarter trailer and list price -- (we will
call this step B) ((At this time there are 2 different models displayed
at the bottom, I would like for there to be only the most current
selection.)) I would like for the macro to remove the previous input and
only display the most current selection. In other words if I completed
step A and B, the macro would only display the results of step B.
+-------------------------------------------------------------------+
+-------------------------------------------------------------------+
Try this:
===================================
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rTbl() As Range
Dim c As Range, r As Range
Dim sTblHeader() As String
Dim i As Long, j As Long
Dim lFirstRow As Long
Dim lLastRow As Long
Dim lLastCol As Long
'Get Table Headers and cells
Application.EnableEvents = False
Set r = Range("A1", Cells(1, Columns.Count).End(xlToLeft))
i = WorksheetFunction.CountA(r)
ReDim rTbl(1 To i, 0 To 2) 'dim 1 is table, 2 is TS text, 3 is List
ReDim sTblHeader(1 To i, 0 To 1) 'dimension 1 is text, 2 is address
For Each c In r
If Len(c.Text) > 0 Then
j = j + 1
sTblHeader(j, 0) = c.Text
sTblHeader(j, 1) = c.Address
End If
Next c
For i = 1 To UBound(sTblHeader) 'iterate for each table
'get first row of table
With Cells
Set c = .Find(what:="Horse", after:=Range(sTblHeader(i, 1)), LookIn:=xlValues, _
lookat:=xlPart, searchdirection:=xlNext, searchorder:=xlByColumns, _
MatchCase:=True)
lFirstRow = c.Row
lLastCol = c.End(xlToRight).Column
Set c = .Find(what:="Horse", after:=Cells(Rows.Count, c.Column), searchdirection:=xlPrevious)
lLastRow = c.Row
'If all of the Prices are formed by functions/formulas, then change xlCellTypeConstants to xlCellTypeFormulas
' in the line below. Leave everything else the same
Set rTbl(i, 0) = Range(Cells(lFirstRow, c.Column + 1), Cells(lLastRow, lLastCol)).SpecialCells(xlCellTypeConstants, xlNumbers)
Set c = .Find(what:=Trim(Left(sTblHeader(i, 0), InStr(sTblHeader(i, 0), "-") - 1)), after:=c, _
LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext)
Set rTbl(i, 1) = c.Offset(rowoffset:=1) 'Trailer Text
Set rTbl(i, 2) = c.Offset(rowoffset:=1, columnoffset:=1) 'List Price
End With
Next i
For i = 1 To UBound(rTbl)
If Not Intersect(Target, rTbl(i, 0)) Is Nothing Then
rTbl(i, 1) = Replace(Cells(Target.End(xlUp).Row - 1, Target.End(xlToLeft).Column).Text, "/", " x ", 1, 1) & _
" / " & Target.End(xlToLeft).Text & " / " & _
Target.End(xlUp).Text & " Short Wall"
rTbl(i, 1).ShrinkToFit = True
rTbl(i, 2) = Target.Value
rTbl(i, 2).NumberFormat = "$#,##0"
'clear other entries
For j = 1 To UBound(rTbl)
If j <> i Then
rTbl(j, 1).MergeArea.ClearContents
rTbl(j, 2).ClearContents
End If
Next j
End If
Next i
Application.EnableEvents = True
End Sub
=====================================