Macro to copy a Named Range from one Worksheet to another - excel

I have several Named Ranges that contain constant data in one worksheet.
I have a target range where one of the Named Ranges will be copied to, on another worksheet.
The Named Range that will be copied is selected based on user input into other cells.
I have managed to create the name of the relevant Named Range in a single cell.
What I can't do (as I'm a VBA Noob who thought he could do all this without using VBA!), is create a Macro that reads the relevant cell, and then copies whatever Name Range it reads, into the target range.
Any assistance most humbly and gratefully accepted.

Let's say, the name of your range is MyRange
So to copy the range you have to do this
Range("MyRange").Copy
Now let's assume that Cell A1 of Sheet1 has the word MyRange. In such a scenario, you can retrieve the value of the cell A1 using Range("A1").Value
So
Range("MyRange").Copy
becomes
Range(Range("A1").Value).Copy
Here is a complete example. I am assuming that you want to copy to say Cell A1 of Sheet2
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
wsI.Range(wsI.Range("A1").Value).Copy wsO.Range("A1")
End Sub

i am not sure if thats what you need, but, if you need just to copy the content of the A1 cell from sheet1 to sheet2 for example, just do this:
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
you may wnat to encapsulate the code into a sub as well:
Sub copyvalues()
Plan2.Cells(1, 1).Value = Plan1.Cells(1, 1).Value
End Sub
...and finally, you must to insert a button, using onclick() event to fire the sub(which you must to allocate into a module)
sorry my bad english, hope it helps you.

Public Function copyNamevalues(srcName As Name, destName As Name)
srcName.RefersToRange.Cells.Copy (destName.RefersToRange.Cells)
End Function

Related

Renaming sheets to value from cell X depending on value on cell Y on the same row

My goal is to create a new worksheet and name it depending on a value in a drop down list and loop it for every "yes" in the list.
The last string is not working and I have no idea how to loop the renaming process to fetch the correct name.
For example: If cell I65 has the value of "AP" I want the newly generated sheet to be named to the value in A65.
Dim rng As Range, cell As Range, was As Worksheet
Set rng = Range("A1:A3")
Sheets("Setup").Select
For Each cell In Range("I48:I85")
If cell = "AP" Then
Sheets("AP").Select
Sheets("AP").Copy Before:=Sheets(1)
Dim AP(2) As Worksheet
Set AP(2) = ActiveWorkbook.Sheets("AP (2)")
AP(2).Name = Worksheets("Setup").Range("A48:A85").Value
End If
Next cell
Though I did not fully understand what you are trying to do, This statement looks suspicious:
AP(2).Name = Worksheets("Setup").Range("A48:A85").Value
you are trying to rename a worksheet with a range of values? -not allowed.
use a single cell like this or concatenate the values in the range before you remane.
AP(2).Name = Worksheets("Setup").Range("A48").Value
Have a look to the following code.
(1) Range("A48:A85").Value returns an array of values which cannot be used as a name. I have used the Offset-function to get the cell in col A of the same row.
(2) No need to use Select. Read (and understand) How to avoid using Select in Excel VBA
(3) Always qualify all worksheets, ranges and so on. With other words: Tell VBA which worksheet from which workbook you are using.
(4) I have used ActiveWorkbook in the With-clause, but I don't like it. Make up your mind on which workbook you are working. If it is the workbook the code lives, use ThisWorkbook instead. It it is another workbook, it's better to assign it to a workbook variable (eg when you open it). Especially in larger project, don't rely on Activeworkbook.
(5) You are declaring your variable AP as array with 2 elements. I guess you just wanted to name the variable similar to the new created sheet, but using an array and writing AP(2) is highly confusing. Just name it AP2 (or newWs or APCopy)
Dim ws As Worksheet, cell As Range
With ActiveWorkbook ' <-- Better change that.
Set ws = .Sheets("Setup")
For Each cell In ws.Range("I48:I85")
If cell = "AP" Then
.Sheets("AP").Copy Before:=.Sheets(1)
Dim AP2 As Worksheet
Set AP2 = .Sheets("AP (2)")
AP2.Name = cell.Offset(0, -8).Value
End If
Next cell
End With

Find next available cell and paste the data

I'm trying to update value in the next cell.
For example, in cell "A1", I have name. When I click on a button, the value in A1 has to go to cell "A1" in Sheet2. And once that is done, as and when I put new values Sheet1("A1") the data has to get reflected in next cells in Sheet2 (i.e A2, A3, A4 etc).
I tried almost every YouTube video, it still doesn't work.
Please state what you’ve tried and why it doesn’t work in future posts.
This can help you:
Sub CellEqCell()
Dim ws1 as worksheet
Dim ws2 as worksheet
Set ws1 = Thisworkbook.Worksheets(“Sheet1”)
Set ws2 = Thisworkbook.Worksheets(“Sheet2”)
Ws2.Range(“A1”).End(xlUp).Offset(1,0).Value =
ws1.Range(“A1”).Value
End sub
This should take into account your last row, and set cells equal to one another without copy/paste.

VBA to search value then transfer data from one worksheet

a little stuck on this VBA. Havent done VBA in a while so might need to cut me some slack :)
Introduction
Working on a Doc that manages the skills and competencies of several employees in my organisation. All worksheets (with the exception of home have identical layout and cell organisation)
Problem Statement
I need a VBA that searches one particular cell (B2) for a factory location (i.e. York) if this cell = York, I then need it to copy and paste a list of values (i.e. B2) into a range of cells on another worksheet in the same workbook named "York". I then need this to loop through all worksheets in the doc (kinda like looping through all employees; each employee will have a new worksheet) for the cell value York and copy and paste those cells into the next empty row on the worksheet York. I cannot seem to get the loop to run through, but after writing the code I couldnt even get the copy paste function to work.
Anyone up for the challenge.
This is my current code. Very poor I know.
Sub testv2()
Dim ws As Worksheet
Dim sh As Worksheet
Dim rng As Range
Dim c As Range
Set ws = Sheets("MATRIX - MASTER")
"Matrix-master was template copy for all skills"
Set rng = ws.Range("$B$2")
For Each sh In Sheets
For Each c In rng.Cells
If rng.Value = "York" Then
ActiveWorkbook.Range("B1").Copy
Sheets("York").Select
ActiveWorkbook.Range("J6").Paste
End If
Next c
Next sh
End Sub
Any help at all would be appreciated!
I don't understand what you are tring to do.
rng is set to one cell of one sheet "$B$2" on "MATRIX - MASTER"
why do you loop it with for each?
I think you ment to write instead of: ActiveWorkbook.Range("J6").Paste
ActiveSheet.Range("B1").Copy ' source
ActiveWorkbook.Sheets("York").Range("J6").Paste ' target
range on the workbook level is used by mamed range.
Try to avoid Select... If you must, use Activate.

How to copy a cell value from one excel file and paste it to another excel file?

I would like to copy a cell value from one workbook to another, but the problem is that the cell values are from different sheets of the workbook. How can I do this? Could you help me with this?
I'm guessing you want a formula, and not instructions on copy and paste.
Cell references can be made using the following format.
=[Workbook Name]SheetName!CellAddress
Example, you have two workbooks open, Book1 and Book2.
Book1, Sheet1, Cell A1 = =[Book2]Sheet1!A1
Book2, Sheet1, Cell A1 = Hello World
The value of [Book1]Sheet1!A1 will now be "Hello World" (it will always reflect the value that is in [Book2]Sheet1!A1, providing the book is open)
If you do want a copy of the actual value, and not a reference to it, you will need to just use plain copy and paste or use the above formulas and when you're done use copy+paste special (paste values) to convert the formulas to their values.
Update
Just realised it was tagged excel-vba so you'd need to use something like #DavidZemens' answer.
You could also use the macro recorder to do this, but doing it manually like David suggests will result in nicer code that's easier to re-use/modify later on.
This is the bones of what you need, you can do a lot more with looping over ranges of cells, sheets in workbooks, etc., but fundamentally you will need to specify in VBA what is the target workbook, worksheet, and cell range, and then set that .Value equal to a specific workbook/worksheet/cell .Value in another workbook.
I use Workbook and Worksheet variables in this example, you can also use range variables for the cell(s) if you desire or if you need to apply to multiple cells.
Sub TransferValues()
Dim wb1 as Workbook
Dim wb2 as Workbook
Dim ws1 as Worksheet
Dim ws2 as Worksheet
Set wb1 = Workbooks("First Workbook.xlsx") '<modify as needed'
Set ws1 = wb1.Sheets("The sheet name") '<modify as needed'
Set wb2 = Workbooks("Another Workbook.xlsx") '<modify as needed'
Set ws2 = wb2.Sheets("Another sheet name") '<modify as needed'
'This line puts the value from wb1.ws1 in wb2.ws2, cells specified:
ws2.Range("A1").Value = ws1.Range("A1").Value
End Sub

Need a "Simple" excel macro to find bottom cell in a column, create a range and copy it

I have a spreadsheet I'm using to compile text that changes all the time.
In column AD, Row 4(AD4) I put the contents of text, and it can have data going 1000 to 4000 rows down. It changes every time, so there is no static range name. I need a macro that
finds the final piece of data in that column,
then automatically "drags a box" from that spot two columns to the left (AB4)
and copies it... (A 3000 row piece of text would be AB4:AD3004) (Macro stops there, with text to be copied highlighted)
The current version finds the bottom cell correctly, but if I run the macro a 2nd time, with new data, it keeps trying to copy the same range. (I used the Formula Define.Name method, to name the cell, and then selected AB4:LastRow) but it is ALWAYS 3160 whether data goes to row 4000 or not.....
Sub Last_row()
Cells(Application.Rows.Count, 30).End(xlUp).Select
' following lines of code are useless
Range("AB4:AD3160").Select
Range("AD3160").Activate
Selection.Copy
End Sub
To answer your question directly:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy
End With
Copy to specific location WITHOUT using clipboard:
With Sheet1
.Range("AB4", .Cells(Rows.Count, "AD").End(xlUp)).Copy Sheet2.[A1]
End With
Copy and exclude formatting:
With Sheet1
With .Range("AB4", .Cells(Rows.Count, "AD").End(xlUp))
Sheet2.Cells(1, "A").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End With
Note: Replace all sheet codenames (sheet1, Sheet2) above with your actual sheet codenames.
Your current code hard-codes the range of interest with
Range("AB4:AD3160").Select
This code will define a dynamic range starting from AB4 to the last non-empty cell in column AD
You can then use this range (without selecting) for changing values elsewhere (note that you may not need to actually copy rng1, it is possible to dump these values to a separate range directly without a copy and paste.
Sub Last_row()
Dim rng1 As Range
Set rng1 = Range([ab4], Cells(Rows.Count, 30).End(xlUp))
rng1.Copy
End Sub
Update: Example of how to copy a dynamic sized range from one sheet to another without a copy and paste:
Sub Last_row2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng1 As Range
Set ws1 = Sheets(1)
Set ws2 = Sheets(2)
Set rng1 = ws1.Range(ws1.[ab4], ws1.Cells(Rows.Count, 30).End(xlUp))
ws2.[a1].Resize(rng1.Rows.Count, rng1.Columns.Count).Value = rng1.Value
End Sub

Resources