VBA - Changing the value of a cell when another cell in the same row is changed

KellyTheKid

New Member
Joined
Mar 7, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, I am new to VBA and I have been learning from google and these forums. I am stuck on a problem and I have no clue how to address it; I am hoping someone could help me.

I Have a named Table "Log" and two columns in question; Column "Site" (Column Q) and Column "Country" (Column T). Column "Site" has a validation of five values (DEUBAR,DEUBER,DEUSAL,FRAPAR,FRALAD). These are country and location codes. I would like to update Column "Country" when the user changes the value of any cell in Column "Site" with the first three characters of the value (either DEU or FRA). My table will continue to grow so I need this code to apply to each new added row. I have been playing with the Worksheet_Change(ByVal Target As Range) sub, but I am so lost. Any assistance will be much appreciated.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I think you are overcomplicating it. You can
a) just have a formula directly in the spreadsheet pulling the first 3 characters using LEFT function - or -
b) using worksheetfunction.LEFT within the VBA

In general I am opposed to using worksheet_change unless absolutely necessary since
a. slows down execution of your spreadsheet and
b. has things going on behind the scenes that user is not aware of
 
Upvote 0
I think you are overcomplicating it. You can
a) just have a formula directly in the spreadsheet pulling the first 3 characters using LEFT function - or -
b) using worksheetfunction.LEFT within the VBA

In general I am opposed to using worksheet_change unless absolutely necessary since
a. slows down execution of your spreadsheet and
b. has things going on behind the scenes that user is not aware of
Thanks for your reply Roncondor. I was using formulas before but I had to leave empty rows with no data so excel could remember the formula for the column. Once I addded VBA code to insert a new row on demand, excel would not apply the formula when a new row was inserted. I am curious however, regarding the VBA entry you propose on option b). I have the vba code for the left function; how to you make it so when I change a cell in column Q, the corresponding row cell in column T is updated with the left statement?
 
Upvote 0
when you insert the row, you can copy down the formula from the row above/

something along the lines of the below (where Yourfromrange is a cell (or rangename) that contains the formula and YOURTORANGE is a cell (or preferably a rangename that contains where you want it copied

Range("YOURFROMRANGE").Cells(1).Copy
Range("YOURTORANGE").PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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