VBA excel populate textbox from range

Cozzy123

New Member
Joined
Aug 10, 2016
Messages
28
Hi,

I would really appreciate some help. I am trying to auto populate TB2 either A/B/C/NA with the value of TB1.

For example

If TB1 has a value less than 5150 then TB2 will populate NA
If TB1 has a value of 5150 - 5350 then TB2 will populate A
If TB1 has a value of 5351 - 5550 then TB2 will populate B
If TB1 has a value of 5551 - 5750 then TB2 will populate C
If TB1 has a value greater than 5751 the TB2 will populate NA

I have managed to do it in Excel but not in VBA.

Assistance appreciated.

Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
insert a module (if you dont have any)
paste this code.


usage:
=CalcVal(cell or textbox)


Code:
Function CalcVal(ByVal pvTb1)
Select Case True
   Case pvTb1 < 5150
      CalcVal = "NA"
   Case pvTb1 >= 5150 And pvTb1 <= 5350
      CalcVal = "A"
   Case pvTb1 >= 5151 And pvTb1 <= 5550
      CalcVal = "B"
   Case pvTb1 >= 5551 And pvTb1 <= 5750
      CalcVal = "C"
   Case pvTb1 > 5751
      CalcVal = "NA"
End Select
end function
 
Last edited:
Upvote 0
Many thanks for the reply, I'm not great with VBA so apologies.

I have copy+pasted the code into TB2 but it didn't show. I'm sure I'm doing something wrong? do I need to name TB1 somewhere in the code?

Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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