How to loop through named range label automatically - excel

I have same many Named Range in my wsI with different label (list with different row number but same column number) (es. listA=Sheet1!$A$2:$E$4, listB=Sheet1!$A$5:$E$6 etc). Now I want to copy my Named Range in a wsO, this code works as I expect:
Sub CopyNamedRange()
Dim wsI As Worksheet, wsO As Worksheet
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
wsO.Range("A1")= "listA"
wsO.Range("listA").Copy wsO.Range("B1")
End Sub
The result is to copy listA cells from the Sheet1!$A$2:$E$4 to Sheet2!$B$1:$F$3 if in Sheet2!A1 was write "listA".
Now, I want to know if it's possible to create a macro that loop through all my labels of the Named Range in wsI and according to the value in Sheet2!A1, copy all the cells.
Secondly, I will introduce a second loop through the Column "A" on Sheet2 in order to find all the different "listX" (es. listA, listB, listA, listC, listB, etc.) and copy automatically the cells in the Sheet2 (obviously if A1=listA will be occupy 3 rows from 1 to 3 the next cell in column A with a "listX" will be in A4 and so on).

This is how:
Option Explicit
Sub Test()
Dim MyNAmes As Name
For Each MyNAmes In ThisWorkbook.Names
'Your code
Next MyNAmes
End Sub

Here the code for my question, if anyone is interested:
Option Explicit
Sub Protocols()
ActiveSheet.UsedRange.Select
Dim wsI As Worksheet, wsO As Worksheet
Dim cell As Range
Dim nm As Name
Set wsI = ThisWorkbook.Sheets("Sheet1")
Set wsO = ThisWorkbook.Sheets("Sheet2")
On Error Resume Next
For Each cell In Columns("A").Cells
If IsEmpty(cell) = True Then
Else
For Each nm In ThisWorkbook.Names
If cell.Value = nm.Name Then Exit For
Next nm
wsI.Range(nm.Name).Copy wsO.Cells(cell.Row, "B")
End If
Next cell
On Error GoTo 0
End Sub
Now the doubt is: it's possible to limit the nm Name searching only in the Named Range stored in Sheet1 (wsI in the example) instead of the whole workbook? I tried to replace
For Each nm In ThisWorkbook.Names
with
For Each nm In wsI.Names
But it seems not work.
Any ideas? Do you think it was possible?
P.S.: It's just to limit the research to the Named Range and avoid unecessary loop!

Related

To stop pasting the formula if the last line in Column A ends

I am working on a vba small code which extract date from Column A into Column C. the current code puts the formulas to extract date from Cell C2 to C2500, However if the data in Column A ends at line A600 it still goes down till C2500. Is it possible if we amend the code to stop pasting the formula exactly at the last line of Column A. so that i do not need to manually delete those cells "#Value". e.g. see print shot.
Sub Formula_property()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheets("Sheet3")
wb.Activate
ws.Select
Range("C2:C2500").Formula = "=extractDate(A2:A2)"
End Sub
Assuming that all columns have the same number of rows (except column B which is empty) - we can use CurrentRegion to get the size of the target "for free"
Sub formula()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet3")
Dim rg As Range
Set rg = ws.Range("A1").CurrentRegion
Set rg = rg.Columns(1)
With rg
.Offset(1, 1).Resize(.Rows.Count - 1).formula = "=extractDate(A2:A2)"
End With
End Sub
BTW: activate/select is not necessary - I recommend reading How to avoid using select.

Unable to fetch ID from one sheet and write to another workbook

I have two Excel workbooks.
First Workbook has two sheets: "Sales" and "Lookup".
Second Workbook has one sheet: "ID"
From the first workbook (Sales), I have to read column 'B' values, search it in column A of "Lookup" sheet and get name from column B.
After fetching ID, I have to write to column E of "ID" workbook.
I tried the first part, but it is not iterating through the cells of Sales and not picking value from "Lookup".
Sub btnExport_Click()
Dim rng As Range
Dim ws1, ws2 As Worksheet
Dim MyStringVar1 As String
Set ws1 = ThisWorkbook.Sheets("Lookup")
Set ws2 = ThisWorkbook.Sheets("Sales")
Set rng = ws2.Range("B2")
With ws2
On Error Resume Next 'add this because if value is not found, vlookup fails
MyStringVar1 = Application.WorksheetFunction.VLookup(Left(rng, 6), ws1.Range("A2:C65536").Value, 2, False)
On Error GoTo 0
If MyStringVar1 = "" Then MsgBox "Item not found" Else MsgBox MyStringVar1
End With
End Sub
*** Edited ***
Code fixed. It is now reading from first cell of Sales but not iterating. Also, while iterating and fetching from Lookup, it has to write to another workbook. This I am not able to fix.
There are two changes that you should make to start. First, try not to reference ActiveSheet (as mentioned in the comments). If the macro is run while a different sheet is selected, then it will mess things up. Store the appropriate worksheet in a variable, such as:
Dim ws As Worksheet
Set ws = Sheets("Sales")
The other item that stands out is in your loop, you are using the .Cells off of the rng object. In your case, you set rng to be the used range in Column B. Let's assume that's cells B2:B10. When you then say rng.Cells(i, 2), if actually offset to the second column of the range, which starts with Column B. You end up using column C.
Instead, try something like
Sub btnExport_Click()
Dim rng As Range
Dim i As Long
Dim ws As Worksheet
Set ws = Sheets("sales")
With ws
Set rng = .Range("B2:B" & .Cells(.Rows.Count, 1).End(xlUp).Row)
For i = 2 To rng.Rows.Count
.Cells(i, 2) = Application.WorksheetFunction.VLookup(.Cells(i, 1), Sheets("Lookup").Range("A:B"), 2, False)
MsgBox (.Cells(i, 2))
Next
End With
End Sub

Performing calculation for multiple rows from one sheet on input data on a different worksheet

I have a dataset on Worksheet "Results" and the dataset is in cells B8:K900 (Actual data in these cells, rest all cells have other meta information this dataset)
Each column of this data set refers to a certain variable like B column has Steam flow, C column has Steam temp etc.
I would like to use these values to plug into a calculator on another sheet, Row by row.
Eventually I will have results in 2 Cells on another sheet which I would like to bring back to Column L and M
Code so far, (I am getting error on first line "ws1.Range("B").Copy Destination:=ws2.Range("B6")" while debugging) :
Note: Goal Seek and Rerun are 2 macros which I would like run after one row of Scenario is in input, as these will help to get results. Hence I have added them in loop also
Any help would be appreciated.
Sub Goal_Seek()
Range("I33").GoalSeek Goal:=Range("P33").Value, ChangingCell:=Range("E3")
End Sub
Sub Rerun()
Do Until Range("P20") = Range("P22").Value
Range("P20") = Range("P22").Value
Loop
End Sub
Sub Calcs()
Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet, ws5 As Worksheet
Dim rng As Range
Set wb = ActiveWorkbook
Set ws1 = wb.Worksheets("Results")
Set ws2 = wb.Worksheets("PI Data")
Set ws3 = wb.Worksheets("Mole_avg")
Set ws4 = wb.Worksheets("eff")
Set ws5 = wb.Worksheets("FlueGas")
Set rng = ws1.Range("B8:M9")
For Each Row In rng.Rows
ws1.Range("B").Copy Destination:=ws2.Range("B6")
ws1.Range("C").Copy Destination:=ws2.Range("B3")
ws1.Range("D").Copy Destination:=ws2.Range("B4")
ws1.Range("E").Copy Destination:=ws2.Range("B5")
ws1.Range("G").Copy Destination:=ws2.Range("B7")
ws1.Range("H").Copy Destination:=ws2.Range("B8")
ws1.Range("I").Copy Destination:=ws2.Range("B9")
ws1.Range("J").Copy Destination:=ws2.Range("B10")
ws1.Range("K").Copy Destination:=ws2.Range("B11")
Application.Run "Goal_Seek"
Application.Run "Rerun"
ws3.Range("P17").Copy Destination:=ws1.Range("L")
ws3.Range("T22").Copy Destination:=ws1.Range("M")
Next Row
End Sub

Loop worksheets containing certain characters

I want to loop the below code on all sheets that contain "12MN" in the sheet name. The code is currently built to just run on the one sheet. If there's a cleaner code of what I already have, that'd be great too. TIA.
Sub TransferValuesOnly()
Dim rng As Range
'Grab Some Data and Store it in a "Range" variable
Set rng = Worksheets("John Smith 12MN").Range("B7:R18")
'Transfer Values to same spot in another worksheet (Mimics PasteSpecial Values Only)
Worksheets("John Smith 12MN").Range("B6").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
'Grab Some Data and Store it in a "Range" variable
Set rng = Worksheets("John Smith 12MN").Range("B24:R35")
'Transfer Values to same spot in another worksheet (Mimics PasteSpecial Values Only)
Worksheets("John Smith 12MN").Range("B23").Range("B23").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
End Sub
Add a Worksheet loop
Check name with InStr
Swap your hardcoded sheet references with the loop sheet reference (ws)
Dim rng As Range
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If InStr(ws.Name, "12MN") Then
Set rng = ws.Range("B7:R18")
ws.Range("B6").Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
End If
Next ws

How to find string in another sheet and copy/paste data to current sheet

I'm trying to construct a macro to find the same part name as I have highlighted in my "Tests" sheet in my "Part Catalog" sheet.
From here, I would like to copy the date when the part was manufactured (which is one column to the right of the part name) from the "Part Catalog" sheet, and paste it in a cell one column to the right of the part name in the "Tests" sheet.
I get an error saying that "Object doesn't support this property or method."
The code below is taken and slightly modified from this link: (http://www.ozgrid.com/forum/showthread.php?t=158840&p=578982#post578982). Previous attempts included me using for loops, but the majority of people seem to agree that the .find function works the best for things like this.
Any help would be appreciated! Thank you!
Sub Get_Date()
Dim Partname As String
Dim sh As Worksheet
Dim ws As Worksheet
Set sh = Sheets("Tests")
Set ws = Sheets("Part Catalog")
Partname = ActiveCell.Value
ws.Cells.Find(Partname).Offset(0, 1).Copy
sh.Cells.Find(Partname).Offset(0, 1).Paste
End Sub
For your follow on question:
Assumes you have the entire list of part names selected first:
Dim sh As Worksheet
Dim ws As Worksheet
Dim c, rng As Range
Set sh = Sheets("Tests")
Set ws = Sheets("Part Catalog")
Set rng = Selection
For Each c In rng
ws.Cells.Find(c.Value).Offset(0, 1).Copy Destination:=c.Offset(0, 1)
Next c
This uses a loop to go through each part (cell) in your selection.

Resources