VBA - Auto fill a template in excel

mageeg

Board Regular
Joined
Jun 6, 2021
Messages
81
Hi, i have created a very simple spreadsheet to demonstrate what i am after.

Lets say i have a sheet in excel called "Data"

1631792187082.png


I then have another sheet called "Template" this is my template i want to fill in. For the sake of the explanation i have created a very simple one.

1631792360722.png


I then want to have a form like the below, in which the user enters the ID No (as it is unique) presses print and then it pulls the data from that row and fills the template above.

1631792496218.png


How would i go about doing this? I would like the user to enter the ID number, and pull the data from that row into the correct position on the template sheet.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Are you happy to use a combobox, rather than a textbox? It makes things a lot easier & helps prevent typos.
 
Upvote 0
Are you happy to use a combobox, rather than a textbox? It makes things a lot easier & helps prevent typos.

Hi, I did think about having a combo box

The issue is as time goes on there are going to be a lot of data entries in the database. This means there could be over 100 rows in which looking through a combo box could be painful
 
Upvote 0
OK, are you IDs actual numbers or text?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   Dim Fnd As Range
   
   Set Fnd = Sheets("Data").Range("A:A").Find(Me.TextBox1.Value, , , xlWhole, , , , , False)
   If Fnd Is Nothing Then
      MsgBox Me.TextBox1.Value & " not found"
      Exit Sub
   End If
   With Sheets("Template")
      .Range("B2").Value = Fnd.Offset(, 1).Value
      .Range("B4").Value = Fnd.Offset(, 2).Value
   End With
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   Dim Fnd As Range
  
   Set Fnd = Sheets("Data").Range("A:A").Find(Me.TextBox1.Value, , , xlWhole, , , , , False)
   If Fnd Is Nothing Then
      MsgBox Me.TextBox1.Value & " not found"
      Exit Sub
   End If
   With Sheets("Template")
      .Range("B2").Value = Fnd.Offset(, 1).Value
      .Range("B4").Value = Fnd.Offset(, 2).Value
   End With
End Sub

This is the sort of thing, i am looking for. That's great thanks!

just a question, if for example i wanted to put first name and second name together so had a box on the template called "Full Name", how would i join these two fields together in the above code?
 
Upvote 0
Like
VBA Code:
.Range("B2").Value = Fnd.Offset(, 1).Value & " " & Fnd.Offset(, 2).Value
 
Upvote 0
This is the sort of thing, i am looking for. That's great thanks!

just a question, if for example i wanted to put first name and second name together so had a box on the template called "Full Name", how would i join these two fields together in the above code?

That's amazing thank you!

Lastly, I have set a print area for this label, can i possibly add a piece of code to the end of this, which print the template once it is filled in?
 
Upvote 0
As that's a totally different question, it needs a new thread.
Thanks
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top