ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Adding a dymanic formula to a cell using visual basic (https://www.excelbanter.com/excel-discussion-misc-queries/112061-adding-dymanic-formula-cell-using-visual-basic.html)

Ian Web

Adding a dymanic formula to a cell using visual basic
 
Hello,

I have been trying to add a dymanic formula to a cell using visual
basic and I am running into a little difficulty. Any assistance would
be appreciated.

I attempted using the following code. The problem line is marked with
the words "Problem Line" at the end.

Thank you in advance.

--Ian Web

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Phone Numbers")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

'check for Last Name
If Trim(Me.Txt_Last_Name.Value) = "" Then
Me.Txt_Last_Name.SetFocus
MsgBox "Please enter an Employee Last Name"
Exit Sub
End If

'check for First Name
If Trim(Me.Txt_First_Name.Value) = "" Then
Me.Txt_First_Name.SetFocus
MsgBox "Please enter an Employee First Name"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.Txt_Last_Name.Value
ws.Cells(iRow, 2).Value = Me.Txt_First_Name.Value
ws.Cells(iRow, 3).Value = "=CONCATENATE(A"&iRow&","", "",B"&iRow&")"
<-- Problem Line
ws.Cells(iRow, 4).Value = Me.Txt_Phone.Value
ws.Cells(iRow, 5).Value = Me.Txt_Floor.Value
ws.Cells(iRow, 6).Value = Me.Txt_Area.Value

'Sort The Columns by Last Name, First Name

ws.Columns("A:F").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending,
Key2:=Range("B2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1,
MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal


'clear the data
Me.Txt_Last_Name.Value = ""
Me.Txt_First_Name = ""
Me.Txt_Phone = ""
Me.Txt_Floor = ""
Me.Txt_Area = ""
Me.Txt_Last_Name.SetFocus

End Sub



All times are GMT +1. The time now is 03:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com