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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You're welcome & thanks for the feedback.
 
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
whoops! i have found one problem (hopefully simple)

I instead tried putting as it's own sub as a macro...

If I want to call this from a different button e.g. "Print all" button that calls each sub. it says invalid use of the Me keyword, how can i get around this?
 
Upvote 0
If you are using a textbox on a userform, just leave the code in the userform module.
 
Upvote 0
whoops! i have found one problem (hopefully simple)

I instead tried putting as it's own sub as a macro...

If I want to call this from a different button e.g. "Print all" button that calls each sub. it says invalid use of the Me keyword, how can i get around this?
Nevermind - sorted this by changing "Me" with the form name
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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