Update excel cells vba excel

I am using all the solutions that appear in:

How to refresh ALL cell through VBA

Getting Excel to refresh data on sheet from within VBA

ActiveSheet.EnableCalculation = False  
ActiveSheet.EnableCalculation = True

or

Application.Calculate

or

Application.CalculateFull

None of them works in Excel 2010. When I go to the cell and right click refresh it works. How can I refresh within VBA?

Sheets("Name_of_sheet").Range("D424").Refresh raises an

exception 438

Questions:

  1. How can I make the script support Excel 2003, 2007, 2010?
  2. How can I choose the source file to refresh from using VBA?

EDIT:

  1. I want to simulate a right mouse click and choose refresh in the menu in worksheet 3. That is the entire story.

  2. I work on an Excel file created 10 years ago. When opening in Excel 2010, I can go to a cell and right click on it and choose refresh and then choose the .txt file to refresh from. I am trying to do it automatically within VBA.

Community's user avatar

asked Nov 22, 2012 at 13:39

0x90's user avatar

6

You could try using Application.Calculation

Application.Calculation = xlCalculationManual
Application.Calculation = xlCalculationAutomatic

answered Nov 22, 2012 at 13:42

Anirudh Ramanathan's user avatar

4

For an individual cell you can use:

Range("D13").Calculate

OR

Cells(13, "D").Calculate

0x90's user avatar

0x90

38.9k36 gold badges163 silver badges244 bronze badges

answered Nov 22, 2012 at 13:46

InContext's user avatar

InContextInContext

2,46112 silver badges24 bronze badges

1

I finally used mouse events and keystrokes to do it:

Sheets("worksheet34").Select
Range("D15").Select
Application.WindowState = xlMaximized
SetCursorPos 200, 600 'set mouse position at 200, 600
Call mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) 'click left mouse
Application.SendKeys ("R")

answered Nov 29, 2012 at 5:52

0x90's user avatar

0x900x90

38.9k36 gold badges163 silver badges244 bronze badges

2

just a reminder;

be careful when using

Application.Calculation = xlCalculationManual
Application.Calculation = xlCalculationAutomatic

this sets the entire excel application to calculate formula’s either automatically or manually. If you use

Application.Calculation = xlCalculationManual

you’ll notice your automatic formulas no longer work.

cheers

answered Feb 16, 2016 at 17:53

LeeF's user avatar

You can force excel to recalculate a cell or range of cells by marking the cell/range as dirty.

Example :

' Recalculate Column D4 to D24
Sheets("Name_of_sheet").Range("D4:D24").Dirty

or

' Recalculate Cell D4<br>
Sheets("Name_of_sheet").Range("D4").Dirty<br>

Ike's user avatar

Ike

7,8904 gold badges11 silver badges29 bronze badges

answered Sep 5, 2021 at 11:16

XYZLOL's user avatar

Application.Calculate didn’t work for my function. Not even when followed by DoEvents.

What I found that works is to re-enter the formula in the cell.
A simple way to get the formula is to start recording a macro, use F2 to edit the cell, then press enter. The macro will make a great copy of the function text with all needed quotes.

Below is an example.

Sheets("Name_of_sheet").Range("D424").FormulaR1C1 = "=now()"

svyat1s's user avatar

svyat1s

8689 gold badges12 silver badges21 bronze badges

answered Mar 5, 2021 at 19:36

Scott's user avatar

Cells(x, y).Select
ActiveCell.FormulaR1C1 = Selection.Value

works perfectly for me

answered Aug 2, 2022 at 20:21

user19260138's user avatar

I have a long «macro» in a workbook > 20 MB, tens of thousands of lines, that calls a dll written in Fortran. None of these methods worked:

Call Application.Calculate
Call Application.CalculateFull
Call Application.CalculateFullRebuild
Re-entering the formula
Range.Dirty

Application.Calculation = xlCalculationManual
Application.Calculation = xlCalculationAutomatic

ThisWorkbook.ActiveSheet.EnableCalculation = False
ThisWorkbook.ActiveSheet.EnableCalculation = True

This worked:

On Error Resume Next
Application.SendKeys "{F2}{Enter}{NumLock}"  'SendKeys turns off NumLock for some reason
On Error GoTo 0

This even worked when a chart was selected.

answered Feb 9 at 14:47

Rocky Scott's user avatar

Rocky ScottRocky Scott

4364 silver badges13 bronze badges

We use VBA to automate our tasks in excel. The idea of using VBA is to connect the interface of excel with the programming. One of the very most connections between them is by changing the cell values. The change in cell value by programming shows the power of VBA. In this article, we will see how to set, get and change the cell value. 

Set Cell Value

Assigning a cell with a value can be achieved by very two famous functions in VBA i.e. Range and Cells function. 

Range Function in VBA

The range function helps access the cells in the worksheet. To set the cell value using the range function, we use the .Value

Syntax: Range(cell_name).Value = value_to_be_assinged. 

Set the value to a single cell

If you want to assign ’11’ to cell A1, then the following are the steps: 

Step 1: Use the Range function, in the double quotes type the cell name. Use .Value object function. For example, Range(“A1”).Value = 11

Applying-range-function

Step 2: Run your macro. The number 11 appears in cell A1

Running-macro

Set the value to multiple cells at the same time

Remember the days, when your teacher gives you punishment, by making you write the homework 10 times, those were the hard days, but now the effort has exponentially reduced. You can set a value to a range of cells with just one line of code. If you want to write your name, for example, “Arushi” 10 times, in the range A2 to A11. Use range function. Following are the steps: 

Step 1: Use the Range function, in the double quotes, write “Start_of_cell: End_of_cell”. Use .Value object function. For example, Range(“A2:A11”).Value = “Arushi”

Applying-range-function

Step 2: Run your macro. The text “Arushi” appears from cell A2 to A11 inclusive. 

Running-macro

Cells Function in VBA

The Cells function is similar to the range function and is also used to set the cell value in a worksheet by VBA. The difference lies in the fact that the Cells function can only set a single cell value at a time while the Range function can set multiple values at a time. Cells function use matrix coordinate system to access cell elements. For example, A1 can be written as (1, 1), B1 can be written as (1, 2), etc. 

Syntax: Cells(row_number, column_number).Value = value_to_be_assign

For example, you want to set the cell value to “Arushi cleared CA with Rank “, in cell B1. Also, set cell C1, to ‘1’. Following are the steps:

Step 1: Open your VBA editor. Use cells function, as we want to access cell B1, the matrix coordinates will be (1, 2). Type, Cells(1, 2).Value = “Arushi cleared CA with Rank” in the VBA code. 

Using-cell-functions

Step 2: To access cell C1, the matrix coordinates are (1, 3). Type, Cells(1, 3).Value = 1 in the VBA code. 

Accessing-cell-C1

Step 3: Run your macro. The required text appears in cell B1, and a number appears in C1. 

Running-macro

Setting Cell values by Active cell and the input box

There are other ways by which you can input your value in the cell in a worksheet. 

Active Cell 

You can set the cell value of a cell that is currently active. An active cell is the selected cell in which data is entered if you start typing. Use ActiveCell.Value object function to set the value either to text or to a number. 

Syntax: ActiveCell.Value = value_to_be_assigned

For example, you want to assign the active cell with a text i.e. “Arushi is practicing CA”, also want to change the color of the cell to yellow. Following are the steps: 

Step 1: Use the ActiveCell object to access the currently selected cell in the worksheet. Use ActiveCell.Value function object to write the required text. 

Using-active-cell-function

Step 2: Color the cell by using ActiveCell.Interior.Color function. For example, use vbYellow to set your cell color to yellow. 

Using-active.cell.interior.color-function

Step 3: Run your macro. The currently selected cell i.e. B1 has attained the requirements. 

Running-macro

Input Box

You can use the input box to set the cell value in a worksheet. The input box takes the custom value and stores the result. This result could further be used to set the value of the cell. For example, set the cell value of A1, dynamically by taking input, from the input box.

Following are the steps

Step 1: Open your VBA editor. A sub-procedure name geeks() is created. Use the Range function to store the value given by the input box. 

Using-range-function

Step 2: Run your Macro. A dialogue-box name Microsoft Excel appears. Enter the value to be stored. For example, “geeks for geeks”. Click Ok

Entering-value-to-be-stored

Step 3: Open your worksheet. In cell A1, you will find the required text is written. 

Text-written-in-A1

Get Cell Value

After setting the cell value, it’s very important to have a handsome knowledge of how to display the cell value. There can be two ways two get the cell value either print the value in the console or create a message box. 

Print Cell Value in Console

The console of the VBA editor is the immediate window. The immediate window prints the desired result in the VBA editor itself. The cell value can be stored in a variable and then printed in the immediate window. For example, you are given a cell A1 with the value ’11’, and you need to print this value in the immediate window. 

Following are the steps

Step 1: Press Ctrl + G to open the immediate window. 

Opening-immediate-window

Step 2: The cell value in A1 is 1

Value-in-A1

Step 3: Open your VBA editor. Declare a variable that could store the cell value. For example, Val is the variable that stores the cell value in A1. Use the Range function to access the cell value. After storing the cell value in the val, print the variable in the immediate window with the help of Debug.Print(val) function.

Printing-variable-in-immediate-window

Step 4: Run your macro. The cell value in A1 is printed in the immediate window. 

Running-macro

Print Cell Value in a Message Box 

A message box can also be used to show the cell value in VBA. For example, a random string is given in cell A1 of your string i.e. “Arushi studies in Hansraj”. Now, if you want to display the cell value in A1, we can use Message Box to achieve this. 

Text-present-in-A1

Following are the steps

Step 1: Open your VBA macro. Create a message box by using MsgBox. Use the Range(cell).Value function to access the cell value. 

Creating-a-message-box

Step 2: Run your macro. A message box appears, which contains the cell value of A1

Running-macro

Change Cell Values 

The value, once assigned to the cell value, can be changed. Cell values are like variables whose values can be changed any number of times. Either you can simply reassign the cell value or you can use different comparators to change the cell value according to a condition. 

By reassigning the Cell Value

You can change the cell value by reassigning it. In the below example, the value of cell A1 is initially set to 1, but later it is reassigned to 2

Following are the steps

Step 1: Open your VBA code editor. Initially, the value of cell A1 is assigned to 1. This initial value is printed in the immediate window. After that, we changed the value of cell A1 to 2. Now, if we print the A1 value in the immediate window, it comes out to be 2

Changing-value-in-cell

Step 2: The immediate window shows the output as 1 and 2. 

Immediate-window-shows-output

Changing cell value with some condition

We can use if-else or switch-case statements to change the cell value with some condition. For example, if your age is greater than 18 then you can drive else you cannot drive. You can output your message according to this condition. 

Following are the steps

Step 1: A code is written in the image below, which tells whether you are eligible for a driving license or not. If your age is greater than 18 then cell A1 will be assigned with the value, “You are eligible for the driving license”, else A1 will be assigned with “You are not eligible for driving license”. 

Text-assigned-to-A1

Step 2: Run your macro. An input box appears. Enter your age. For example, 19. 

Running-macro

Step 3: According to the age added the cell value will be assigned. 

Cell-value-assigned

Содержание

  1. Get, Set, or Change Cell value in Excel VBA
  2. Set Cell Value
  3. Set the value to a single cell
  4. Get Cell Value
  5. Change Cell Values
  6. update cell and paste it to another cell vba
  7. 3 Answers 3
  8. How to Change Another Cell with a VBA Function
  9. The VBA Tutorials Blog
  10. Introduction — Change Another Cell with a VBA UDF
  11. Change a Different Cell using an Excel VBA Function
  12. Update Another Cell using VBA UDF
  13. Change Cell Next to the Formula Cell
  14. Prevent User from Typing into Any Cell
  15. Final Thoughts

Get, Set, or Change Cell value in Excel VBA

We use VBA to automate our tasks in excel. The idea of using VBA is to connect the interface of excel with the programming. One of the very most connections between them is by changing the cell values. The change in cell value by programming shows the power of VBA. In this article, we will see how to set, get and change the cell value.

Set Cell Value

Assigning a cell with a value can be achieved by very two famous functions in VBA i.e. Range and Cells function.

Range Function in VBA

The range function helps access the cells in the worksheet. To set the cell value using the range function, we use the .Value.

Syntax: Range(cell_name).Value = value_to_be_assinged.

Set the value to a single cell

If you want to assign ’11’ to cell A1, then the following are the steps:

Step 1: Use the Range function, in the double quotes type the cell name. Use .Value object function. For example, Range(“A1”).Value = 11.

Step 2: Run your macro. The number 11 appears in cell A1.

Set the value to multiple cells at the same time

Remember the days, when your teacher gives you punishment, by making you write the homework 10 times, those were the hard days, but now the effort has exponentially reduced. You can set a value to a range of cells with just one line of code. If you want to write your name, for example, “Arushi” 10 times, in the range A2 to A11. Use range function. Following are the steps:

Step 1: Use the Range function, in the double quotes, write “Start_of_cell: End_of_cell”. Use .Value object function. For example, Range(“A2:A11”).Value = “Arushi”.

Step 2: Run your macro. The text “Arushi” appears from cell A2 to A11 inclusive.

Cells Function in VBA

The Cells function is similar to the range function and is also used to set the cell value in a worksheet by VBA. The difference lies in the fact that the Cells function can only set a single cell value at a time while the Range function can set multiple values at a time. Cells function use matrix coordinate system to access cell elements. For example, A1 can be written as (1, 1), B1 can be written as (1, 2), etc.

Syntax: Cells(row_number, column_number).Value = value_to_be_assign

For example, you want to set the cell value to “Arushi cleared CA with Rank “, in cell B1. Also, set cell C1, to ‘1’. Following are the steps:

Step 1: Open your VBA editor. Use cells function, as we want to access cell B1, the matrix coordinates will be (1, 2). Type, Cells(1, 2).Value = “Arushi cleared CA with Rank” in the VBA code.

Step 2: To access cell C1, the matrix coordinates are (1, 3). Type, Cells(1, 3).Value = 1 in the VBA code.

Step 3: Run your macro. The required text appears in cell B1, and a number appears in C1.

Setting Cell values by Active cell and the input box

There are other ways by which you can input your value in the cell in a worksheet.

Active Cell

You can set the cell value of a cell that is currently active. An active cell is the selected cell in which data is entered if you start typing. Use ActiveCell.Value object function to set the value either to text or to a number.

Syntax: ActiveCell.Value = value_to_be_assigned

For example, you want to assign the active cell with a text i.e. “Arushi is practicing CA”, also want to change the color of the cell to yellow. Following are the steps:

Step 1: Use the ActiveCell object to access the currently selected cell in the worksheet. Use ActiveCell.Value function object to write the required text.

Step 2: Color the cell by using ActiveCell.Interior.Color function. For example, use vbYellow to set your cell color to yellow.

Step 3: Run your macro. The currently selected cell i.e. B1 has attained the requirements.

Input Box

You can use the input box to set the cell value in a worksheet. The input box takes the custom value and stores the result. This result could further be used to set the value of the cell. For example, set the cell value of A1, dynamically by taking input, from the input box.

Following are the steps

Step 1: Open your VBA editor. A sub-procedure name geeks() is created. Use the Range function to store the value given by the input box.

Step 2: Run your Macro. A dialogue-box name Microsoft Excel appears. Enter the value to be stored. For example, “geeks for geeks”. Click Ok.

Step 3: Open your worksheet. In cell A1, you will find the required text is written.

Get Cell Value

After setting the cell value, it’s very important to have a handsome knowledge of how to display the cell value. There can be two ways two get the cell value either print the value in the console or create a message box.

Print Cell Value in Console

The console of the VBA editor is the immediate window. The immediate window prints the desired result in the VBA editor itself. The cell value can be stored in a variable and then printed in the immediate window. For example, you are given a cell A1 with the value ’11’, and you need to print this value in the immediate window.

Following are the steps

Step 1: Press Ctrl + G to open the immediate window.

Step 2: The cell value in A1 is 1.

Step 3: Open your VBA editor. Declare a variable that could store the cell value. For example, Val is the variable that stores the cell value in A1. Use the Range function to access the cell value. After storing the cell value in the val, print the variable in the immediate window with the help of Debug.Print(val) function.

Step 4: Run your macro. The cell value in A1 is printed in the immediate window.

Print Cell Value in a Message Box

A message box can also be used to show the cell value in VBA. For example, a random string is given in cell A1 of your string i.e. “Arushi studies in Hansraj”. Now, if you want to display the cell value in A1, we can use Message Box to achieve this.

Following are the steps

Step 1: Open your VBA macro. Create a message box by using MsgBox. Use the Range(cell).Value function to access the cell value.

Step 2: Run your macro. A message box appears, which contains the cell value of A1.

Change Cell Values

The value, once assigned to the cell value, can be changed. Cell values are like variables whose values can be changed any number of times. Either you can simply reassign the cell value or you can use different comparators to change the cell value according to a condition.

By reassigning the Cell Value

You can change the cell value by reassigning it. In the below example, the value of cell A1 is initially set to 1, but later it is reassigned to 2.

Following are the steps

Step 1: Open your VBA code editor. Initially, the value of cell A1 is assigned to 1. This initial value is printed in the immediate window. After that, we changed the value of cell A1 to 2. Now, if we print the A1 value in the immediate window, it comes out to be 2.

Step 2: The immediate window shows the output as 1 and 2.

Changing cell value with some condition

We can use if-else or switch-case statements to change the cell value with some condition. For example, if your age is greater than 18 then you can drive else you cannot drive. You can output your message according to this condition.

Following are the steps

Step 1: A code is written in the image below, which tells whether you are eligible for a driving license or not. If your age is greater than 18 then cell A1 will be assigned with the value, “You are eligible for the driving license”, else A1 will be assigned with “You are not eligible for driving license”.

Step 2: Run your macro. An input box appears. Enter your age. For example, 19.

Step 3: According to the age added the cell value will be assigned.

Источник

update cell and paste it to another cell vba

I am quite new in excel vba and I would really appreciate if you can assist me. The thing is that I have cell which updates each minute because it is linked with a function to Blomberg. The thing is that I want that each time cell updates excel copies it and pastes to another, new cell that i can observe the intra day changes. I have come up with some codes but I can copy and paste only to one, similar cell.It looks like following:

Any help would be highly appreciated.

3 Answers 3

If I understand your problem correctly you want to copy the value to a new cell, for logging purposes? What I would do in this case is have another sheet for logging the values named «logger_sheet» I paste a value in cell a1 when the blomberg cell updates, copy the value into my logger_sheet cell a2 when it changes copy it to a3 then a4 etc.

Here is your updated code. It assumes you have a sheet named «logger_sheet» (if you dont have one, create it) to store all the previous values. When the blomberg cell updates, it copies the value and pastes it to the next avaliable logging_sheet cell. I have developed a function that finds the last used row in a specified sheet and column. Try it out

Also there is a line you can uncomment if you want to prevent excel from flashing, I labeled it in the code

If my answer solves your problem please mark it as the solution

Источник

How to Change Another Cell with a VBA Function

The VBA Tutorials Blog

Introduction — Change Another Cell with a VBA UDF

Despite what others say, it is possible to change another cell with a VBA user-defined function. Microsoft even says it’s impossible for a custom function to change another cell’s value, but I’m going to show you a couple UDFs that prove all these people wrong.

There aren’t very many practical applications where you would want to enter a formula in one cell and change the value of a different cell, but people regularly want to do it.

One common example of why people want to do this is to copy a static snapshot of what a cell used to be at a given point in time. In other words, they don’t want their copy of the cell to update when the target cell changes. Yes, this can be done by copying and pasting as values, but some people prefer a custom formula.

Another example of where this can be useful is to copy a value to another cell such that that cell cannot be overwritten. This is actually a pretty cool method. Scroll down to my second example to see how it works!

Another common example is to use a user-defined function to change a color of a cell. I’ll show you how to do this in a future tutorial.

There are also other, umm, less scrupulous applications of this feature. One not-so-nice application is to enter a function hidden somewhere on a spreadsheet that prevents anyone from typing what they want into any other cell. I’ll show you how to do that, too, but don’t blame me when you get beat up for being mean!

Time for three warnings:

  1. These functions can be quite unstable. They may cause Excel to crash. I tested them in Excel 2010, but your mileage may vary. This approach should work for Excel 2010 and earlier versions.
  2. Some of the UDF examples I’m about to show you are not practical. They can be accomplished with different functions. I’m just here to show you that it’s possible to change other cells with VBA functions.
  3. If you thought you knew VBA, what you’re about to see may blow your mind…

Change a Different Cell using an Excel VBA Function

Make powerful macros with our free VBA Developer Kit

It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free VBA Developer Kit and wrote the Big Book of Excel VBA Macros full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more — grab your free copy below.

This example copies the content of cell A1 to whatever cell you select in your function via the CopyTo variable. By not passing the source you want to copy to the function, you’re preserving on old copy of cell A1. In other words, because volatility is False by default, you can change A1 in the future and the value in cell CopyTo will not change.

Here’s what I mean. I entered “3.14” in cell A1 and typed =CopyCellContents(E1) into cell B1. The expectation is that cell E1 will now change to 3.14.

Success. That’s exactly what happened. Now, I can change cell A1 and the value stored in cell E1 will not change.

The timestamp proves it doesn’t change. What we’ve done is recorded a snapshot of what cell “A1” was when we first executed the CopyCellContents function.

Update Another Cell using VBA UDF

This user-defined function is quite similar to the last function, except it accepts 2 arguments: CopyFrom and CopyTo .

One thing I like about this UDF is you don’t even see that there’s a formula entered into cell B1 . It’s just quietly doing it’s thing.

With Excel Volatility set to false, the default, the formula will recalculate anytime one of the arguments is changed. What this means is that when you change range CopyFrom , the value in cell CopyTo will automatically update.

You know what else the Excel Volatility setting does for this function? It prevents you from overwriting the value in cell CopyTo .

That’s right! You can use a VBA UDF to prevent overwriting a cell. Try to type anything you want into cell E1 in the above example and the moment you press enter to get out of the cell, the value in cell A1 will automatically pop back into the cell. It’s a self-protected cell!


Before


After

Change Cell Next to the Formula Cell

This function changes the value of the cell to the left of wherever your formula is entered. It doesn’t matter where you typed your formula. As long as there’s a cell to the left, that cell will change.

All it does is change the text to “Hello!” You can adapt the macro to your own needs. If you’d rather change multiple cells, you certainly can.

Since we didn’t pass our formula any arguments, it will never be recalculated unless we enter the formula again. If you want your user-defined function to update anytime a cell is changed, add Application.Volatile to the top of the function, like we do in the next example. If you don’t want your formula to show up on every single sheet, you’ll need to pass your Sub the sheet name, as well.

Prevent User from Typing into Any Cell

Alright, this one isn’t very nice, but it sure is funny. Copy and paste the above example into a module in your VBA Editor and enter =MeanFunction() into any cell in an Excel workbook — preferably someone else’s workbook! Because the function returns an empty string and has no arguments, it’s really difficult to find where you put it. That’s what makes it such an effective function. For maximum impact, hide the command way over on the right side of the workbook so it’s even harder to find.

Whenever someone tries typing in another cell, they’ll be greated with one of 5 random taunts. This is what it looks like:

Because we set Application.Volatile equal to true, the function executes anytime someone tries changing a cell. Whatever cell the person has selected will automatically be overwritten with one of the pre-defined messages.

What makes it even funnier is if your user tries to select all the cells to delete them, look at what happens:

All the cells get filled! This makes a pretty good april fools jokes for school or work, if you’re brave enough to do it. I’m not!

Okay, okay, that’s enough fooling around.

On a serious note, you can modify this example to prevent people from entering data into a certain range. This is good protection if you don’t want to lock your sheet.

Final Thoughts

Be honest. You thought it was impossible to change another cell using a custom function, didn’t you? Nothing is impossible with VBA!

When you’re ready to take your VBA to the next level, subscribe using the form below.

Ready to do more with VBA?
We put together a giant PDF with over 300 pre-built macros and we want you to have it for free. Enter your email address below and we’ll send you a copy along with our VBA Developer Kit, loaded with VBA tips, tricks and shortcuts.

Before we go, I want to let you know we designed a suite of VBA Cheat Sheets to make it easier for you to write better macros. We included over 200 tips and 140 macro examples so they have everything you need to know to become a better VBA programmer.

Источник

  • #2

Try:

Code:

Sub Test()
    Dim LastRow As Long
    Dim i As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Range("A" & i).Value = "refrin" Then
            Range("B" & i).Value = "RefrinDHP"
        End If
    Next i
End Sub

  • #3

It works perfectly.

Many thanks for your answer

  • #4

Try:

Code:

Sub Test()
    Dim LastRow As Long
    Dim i As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Range("A" & i).Value = "refrin" Then
            Range("B" & i).Value = "RefrinDHP"
        End If
    Next i
End Sub

Thank you. Please could you help if a cell appears blank but has space in it, for example i am entering this in B4, =IF(LEN(A4)>2,»Cell has value»,»»)

  • #5

Thank you. Please could you help if a cell appears blank but has space in it, for example i am entering this in B4, =IF(LEN(A4)>2,»Cell has value»,»»)

Please start your own thread rather than hijacking one that seems to be unrelated.

  • #6

Please start your own thread rather than hijacking one that seems to be unrelated.

Sorry sir, i did not mean to hijack. i though i would able to explain it better in this way, since the code works as expected based on this original thread, and it worked fine as i my case when i replaced the keyword ‘refrin’ to «», and just then i realised there should be some way to check if the cell is really blank, as sometimes there is space and we are not knowing… Thank you.

  • #7

What if instead of plain text «RefrinDHP» (Range(«B» & i).Value = «RefrinDHP») I wanted to input a formula referencing another column, ex. =workday(«b» & i, «c» & i)?

If range(«b» & i).value = «refrin» then
range(«a» & i).value = formulaR1C1 = «

workday(«b» & i, «c» & i)»

  • #8

How can you use «refrin» in a formula that uses the WORKDAY function?

  • #9

I was using the example already given, but my situation is obviously different. I do realize that I used the text field in the workday formula, it should’ve referenced another date or number field.
Basically I need a cell updated (workday formula) if it meets a certain value (text, «refrin» or in my case «Calendar») from a picklist field of another cell along the same row. Example below.

a b c d
1 =workday(b1,c1) 12/31/14 3 Calendar
2 =workday(b2,c3) 12/31/15 4 Fiscal
3 =workday(b3,c3) 12/31/16 -5 Calendar

<tbody>

</tbody>

The code would be something like this:
I believe FormulaR1C1 has to be a child to a range, but not sure on the syntax for it. Cells a1, a2, a3, etc. would be updated with the workday formula if d1, d2, d3, etc. = «Calendar»

Sub Test() Dim LastRow As Long Dim i As Long LastRow = Range(«A» & Rows.Count).End(xlUp).Row For i = 2 To LastRow If Range(«d» & i).Value = «Calendar» Then Range(«a» & i).Value = Range(«a» & i).FormulaR1C1 «= WORKDAY(RC[Range(«b» & i],RC[Range(«c» & i)])» End If Next iEnd Sub

  • #10

That would be:

Code:

Sub Test()
    Dim LastRow As Long
    Dim i As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Range("d" & i).Value = "Calendar" Then
            Range("a" & i).FormulaR1C1 = "= WORKDAY(RC2,RC3)"
        End If
    Next i
End Sub

In this chapter you will learn how to change all the cells in the active sheet to values using VBA in Microsoft Excel.

Let’s take an example and understand how we write the VBA code for change the cells value in Active sheet.

How to change all the cells value in the Active sheet?

We have data in Excel, in which we want the replace all the cells value with only a cell value.

image 1

Follow below given steps:-

  • Press Alt+F11 key to open the Visual Basic Application

image 2

  • In VBAProject Double click on Sheet 1
  • Enter the below given VBA Code
 
 
Sub Values_1()

With ActiveSheet.UsedRange

Range("A1:I7").Value = Range("A2").Value

End With

End Sub

 
image 3

  • To run the code press F5 key
  • Cell A2 value will get update in the define range

image 4

How the change the value from first table to second table?

We have 2 tables, 1st table’s range is A3:I5, and 2nd table range is A8:I10. We want to replace the value of 1st table with the value of 2 table in the active sheet.

image 5

To change the value follow below given steps and code:-

  • Open the Visual Basic Application
  • Enter the below code:-
Sub Values_1()

With ActiveSheet.UsedRange

Range("A3:I5").Value = Range("A8:I10").Value

End With

End Sub

 
image 6

  • Run the code by pressing F5
  • Values will get updated

image 7

image 48

If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write us at info@exceltip.com

Понравилась статья? Поделить с друзьями:
  • Update database with excel
  • Update data from excel
  • Update all references in word
  • Update all links in excel
  • Unhiding a sheet in excel