I have been working on a spreadsheet containing a table. I have made a code with inputbox that add new data to my table in column B and C. I am wondering how I, in addition, can add data to a third column that increase the value by 3 based on column C. Whatever I enter into column C, I want the corresponding cell in column D to be thrice as much.
Public Sub InsertCargoNumber3()
Dim aaa As String, ar As Long
aaa = InputBox("Enter cargo number")
If aaa = "" Then
Exit Sub
Else
ar = Range("B" & Rows.Count).End(xlUp).Row + 1
Range("b" & ar).Value = aaa
End If
Dim bbb As String, br As Long
bbb = InputBox("Enter Laycan Start Date")
If bbb = "" Then
Exit Sub
Else
br = Range("B" & Rows.Count).End(xlUp).Row
Range("c" & br).Value = bbb
End Sub
Let's say in column C via the inputbox I enter 23.02.20, then in column D it should read 25.02.20
in advance, thanks
Please try this code.
Public Sub InsertCargoNumber3()
Dim aaa As String
Dim ar As Long
aaa = InputBox("Enter cargo number")
If Len(aaa) Then
ar = Cells(Rows.Count, "B").End(xlUp).Row + 1
Cells(ar, "B").Value = aaa
aaa = InputBox("Enter Laycan Start Date")
If Len(aaa) Then
Cells(ar, "C").Value = aaa
Cells(ar, "D").Value = Val(aaa) * 3
End If
End If
End Sub
Note that Subs and Functions are Public by default. It's when they are Private that they are special.
If the second aaa
is a date, multiplying that number with 3 must give a date far into the future but if you want that number formatted as a date I recommend to exchange the last line of code in the above procedure to be replaced with this.
With Cells(ar, "D")
.Value = Val(aaa) * 3
.NumberFormat = "dd-mm-yyyy" ' or whatever format you prefer
End With
Frankly, I haven't considered that you intend to enter a date there - perhaps because the following multiplication. Had I done so, I would have constructed the second input differently, perhaps like this.
' aaa has a value from previous use
do While Len(aaa)
aaa = InputBox("Enter Laycan Start Date")
If IsDate(aaa) Then
Cells(ar, "C").Value = DateSerial(aaa)
Cells(ar, "D").Value = DateSerial(aaa) * 3
Range(Cells(ar, "C"), Cells(ar, "D")).Numberformat = "dd-mm-yyyy"
Exit do
End If
Loop