Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to name project 1

Status
Not open for further replies.

lychee

Civil/Environmental
Aug 26, 2003
4
Is there a way to write a macro to prompt user to key in...say the project title and its client's name and automatically puts the answer to cell A1 and B1 respectively? If the cell A1 and B1 is already filled, the macro automatically writes the answer to the following cell (i.e A2 and B2). I'd written something like this but i would like the macro to loop back to the 1st question insstead of continuing to the 2nd, any help?

Sub Macro1()
Message1 = "Enter project title"
Title1 = "Project info"
Message2 = "Enter client's name"
Title2 = "Client info"
answer1 = InputBox(Message1, Title1, "")
answer2 = InputBox(Message2, Title2, "")
If answer1 = Empty Then
MsgBox Prompt:="You must enter a value."
answer1 = InputBox(Message1, Title1, "")
Else
Range("A1") = answer1
End If
If answer2 = Empty Then
MsgBox Prompt:="You must enter a value."
Else
Range("B1") = answer2
End If
End Sub
 
Replies continue below

Recommended for you

Try this:

Sub Macro1()
Dim answer1 As String
Dim answer2 As String

Message1 = "Enter project title"
Title1 = "Project info"
Message2 = "Enter client's name"
Title2 = "Client info"
answer1 = ""
answer2 = ""

Do
answer1 = InputBox(Message1, Title1, answer1)
Loop Until Len(answer1)
Range("A1") = answer1

Do
answer2 = InputBox(Message2, Title2, answer2)
Loop Until Len(answer2)
Range("B1") = answer2


End Sub

good luck,
killswitch
 
Thanks, it does work...
 
but the macro still doesn't write the next input to the next row, any trick to do so? by the way, anyway to make excel to recognise repeatative input and prompt warning message when input is repeated? I'm new in visual basic, so help anyone?
 
...still doesn't write the next input to the next row

Do you mean put the second answer into cell A2 rather than B1?

...recognise repeatative input
Do you mean compare the values in A1 and A2/B1?

Or do you mean maintain a "log" of sorts to keep appending new project info below existing info?

In short, yeah - VBA can do any of that kind of stuff. You need to define a bit more clearly what you're trying to do.
 
This updated macro will keep adding entries in the next available row in columns A and B. Change the constants to start the lists in cells other than A1 and B1.

Killswitch

Sub Macro1()
Const ProjectCol = "A"
Const ClientCol = "B"
Const FirstRow = 1

Dim answer1 As String, LastRow As Integer

'get the project name
Message1 = "Enter project title"
Title1 = "Project info"
answer1 = ""
Do
answer1 = InputBox(Message1, Title1, answer1)
Loop Until Len(answer1)

' determine the row to write the data to
LastRow = FirstRow
If Not IsEmpty(Range(ProjectCol & LastRow)) Then
' if the first row has an entry
If IsEmpty(Range(ProjectCol & LastRow + 1)) Then
' use next row if it's empty
LastRow = 2
Else
LastRow = Range(ProjectCol & LastRow).End(xlDown).Rows.Row + 1
' find the next empty row in column A
End If
End If

' write the Project Name in column A
Range(ProjectCol & LastRow) = answer1

' get the client name
Message1 = "Enter client's name"
Title1 = "Client info"
answer1 = ""

Do
answer1 = InputBox(Message1, Title1, answer1)
Loop Until Len(answer1)

' write the Client Name in column B and the same row as the Project
Range(ClientCol & LastRow) = answer1

End Sub
 
Sorry,

If IsEmpty(Range(ProjectCol & LastRow + 1)) Then
' use next row if it's empty
LastRow = 2


should be:

If IsEmpty(Range(ProjectCol & LastRow + 1)) Then
' use next row if it's empty
LastRow = LastRow + 1


Killswitch
 
Fabulous!! I think that solved my question & improved my knowledge in VB, thanks killswitch...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor