I've written a macro that has an InputBox appear, asks the user to enter a client name and company name, searches row 1 for the company name, and if a match is found, adds the client name into the first empty cell beneath a list of client names for that company. (At present, there are a few companies listed, with at least 1 client name under each company).
The problem occurs if a user is adding a client from a new company. If the company name is not found in row 1, I'd like to create a new column with the company name in row 1, and then have the client name appear under it. Any suggestions about what type of macro I need?
Sub CreateNew()
Dim myValue As Variant
myValue = InputBox("Enter client name:")
Dim myValue1 As Variant
myValue1 = InputBox("Enter client company:")
Range("A12").Value = myValue
Range("A13").Value = myValue1
'Select cells in row 1
Rows("1:1").Select
'Find the company name (directs you to the appropriate column)
Selection.Find(What:=Range("A13").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'Select the active cell
ActiveCell.Select
'Move to bottom cell in column with text
Selection.End(xlDown).Select
'Move down 1 cell
Selection.Offset(1, 0).Select
'Select the active cell
ActiveCell.Select
'Make cell value equal to A12
ActiveCell.FormulaR1C1 = Range("A12").Value
'Copy and paste values only
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
The problem occurs if a user is adding a client from a new company. If the company name is not found in row 1, I'd like to create a new column with the company name in row 1, and then have the client name appear under it. Any suggestions about what type of macro I need?
Sub CreateNew()
Dim myValue As Variant
myValue = InputBox("Enter client name:")
Dim myValue1 As Variant
myValue1 = InputBox("Enter client company:")
Range("A12").Value = myValue
Range("A13").Value = myValue1
'Select cells in row 1
Rows("1:1").Select
'Find the company name (directs you to the appropriate column)
Selection.Find(What:=Range("A13").Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'Select the active cell
ActiveCell.Select
'Move to bottom cell in column with text
Selection.End(xlDown).Select
'Move down 1 cell
Selection.Offset(1, 0).Select
'Select the active cell
ActiveCell.Select
'Make cell value equal to A12
ActiveCell.FormulaR1C1 = Range("A12").Value
'Copy and paste values only
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End Sub