Forum Discussion
Function or Macro to copy to a cell where cell index is calculated
I have computed the index of a cell, e.g. the cell (=B1, 5).
What is the function or Macro to copy A1 to that cell?
Example function: =Copy(A1, B1, 5)
This will copy the content of A1 to the cell with row number =B1, and column number 5.
When A1 or B1 change, the copy is performed.
Thank you.
To achieve the desired functionality of copying the content of cell A1 to a dynamically calculated cell based on row and column numbers, you can use VBA macros in Excel. Here's an example of a VBA macro that can accomplish this task:
Vba code:
Sub CopyToDynamicCell() Dim sourceRange As Range Dim targetRow As Long Dim targetColumn As Long ' Set the source range (cell A1 in this example) Set sourceRange = ThisWorkbook.Worksheets("Sheet1").Range("A1") ' Get the target row and column numbers from cells B1 and C1 (modify as needed) targetRow = ThisWorkbook.Worksheets("Sheet1").Range("B1").Value targetColumn = ThisWorkbook.Worksheets("Sheet1").Range("C1").Value ' Copy the value from the source range to the dynamically calculated target cell ThisWorkbook.Worksheets("Sheet1").Cells(targetRow, targetColumn).Value = sourceRange.Value End Sub
To use this macro, follow these steps:
- Press Alt+F11 to open the VBA editor in Excel.
- Insert a new module by clicking on "Insert" > "Module".
- Copy and paste the above VBA code into the module.
- Modify the sheet name and cell references (Sheet1, A1, B1, C1) to match your specific worksheet and cell locations.
- Save the workbook as a macro-enabled (.xlsm) file.
- Close the VBA editor.
- Run the macro by pressing Alt+F8, selecting the CopyToDynamicCell macro, and clicking "Run".
Once the macro is executed, it will copy the value from cell A1 to the dynamically calculated target cell based on the row and column numbers in cells B1 and C1. Whenever the values in B1 or C1 change, the copy operation will be performed automatically.
You can also assign the macro to a button or create a keyboard shortcut for quick access.
Function:
In Excel, there is no built-in function that can directly achieve the dynamic copying of a cell based on calculated row and column numbers. Functions in Excel are designed to return a single value, and they cannot perform actions like copying data to specific cells.
However, you can use a combination of functions to achieve a similar result indirectly. One approach is to use a helper formula in a target cell to reference the source cell. Here's an example formula you can use: =IF(ROW()=$B$1, IF(COLUMN()=5, $A$1, ""), "")
In this example, the value of cell A1 will be copied to the cell with the row number specified in cell B1 and column number 5. To use this approach, follow these steps:
- Enter the formula in the target cell where you want to copy the value.
- Adjust the cell references in the formula based on your specific scenario.
- Copy the formula down and across as needed to cover the desired range of cells.
Note that this method requires manually dragging or copying the formula to the desired range, and the values will not update automatically if the source cell or the row/column numbers change. You would need to manually adjust the formula if there are any changes. The text, steps and the code was created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
2 Replies
- NikolinoDEGold Contributor
To achieve the desired functionality of copying the content of cell A1 to a dynamically calculated cell based on row and column numbers, you can use VBA macros in Excel. Here's an example of a VBA macro that can accomplish this task:
Vba code:
Sub CopyToDynamicCell() Dim sourceRange As Range Dim targetRow As Long Dim targetColumn As Long ' Set the source range (cell A1 in this example) Set sourceRange = ThisWorkbook.Worksheets("Sheet1").Range("A1") ' Get the target row and column numbers from cells B1 and C1 (modify as needed) targetRow = ThisWorkbook.Worksheets("Sheet1").Range("B1").Value targetColumn = ThisWorkbook.Worksheets("Sheet1").Range("C1").Value ' Copy the value from the source range to the dynamically calculated target cell ThisWorkbook.Worksheets("Sheet1").Cells(targetRow, targetColumn).Value = sourceRange.Value End Sub
To use this macro, follow these steps:
- Press Alt+F11 to open the VBA editor in Excel.
- Insert a new module by clicking on "Insert" > "Module".
- Copy and paste the above VBA code into the module.
- Modify the sheet name and cell references (Sheet1, A1, B1, C1) to match your specific worksheet and cell locations.
- Save the workbook as a macro-enabled (.xlsm) file.
- Close the VBA editor.
- Run the macro by pressing Alt+F8, selecting the CopyToDynamicCell macro, and clicking "Run".
Once the macro is executed, it will copy the value from cell A1 to the dynamically calculated target cell based on the row and column numbers in cells B1 and C1. Whenever the values in B1 or C1 change, the copy operation will be performed automatically.
You can also assign the macro to a button or create a keyboard shortcut for quick access.
Function:
In Excel, there is no built-in function that can directly achieve the dynamic copying of a cell based on calculated row and column numbers. Functions in Excel are designed to return a single value, and they cannot perform actions like copying data to specific cells.
However, you can use a combination of functions to achieve a similar result indirectly. One approach is to use a helper formula in a target cell to reference the source cell. Here's an example formula you can use: =IF(ROW()=$B$1, IF(COLUMN()=5, $A$1, ""), "")
In this example, the value of cell A1 will be copied to the cell with the row number specified in cell B1 and column number 5. To use this approach, follow these steps:
- Enter the formula in the target cell where you want to copy the value.
- Adjust the cell references in the formula based on your specific scenario.
- Copy the formula down and across as needed to cover the desired range of cells.
Note that this method requires manually dragging or copying the formula to the desired range, and the values will not update automatically if the source cell or the row/column numbers change. You would need to manually adjust the formula if there are any changes. The text, steps and the code was created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
- Brom_NaderCopper Contributor
Thank you so much NikolinoDE!
If you don't mind, another question:
How can I implement iteration? For example, I need Macro1 to be executed repeatedly as long as cell A1 is not zero.
Also formatting. I need to automatically display the contents of a cell such that only 3 significant digits are rounded and displayed. Example:
34,262.55 is displayed automatically as 34,300
3,426,255 is displayed as 3,430,000
0.00003426255 is displayed as 0.00003430.003404 is displayed as 0.00340
Thank you.