Solved

Variable naming accross a row

Posted on 2016-08-09
6
48 Views
Last Modified: 2016-08-10
In vba I would like to start on Sheet1, give some columns a variable name based on whatever the active row is.  So if I click on a cell, it grabs that row as the active row.  Then name whatever is in column B, give it a variable name of ColB and whatever is in column H, the variable name of ColH,  Once cells are named, goto Sheet2 and put ColH value in column A1 and whatever the value is of ColB in Sheet2 A3

They are random, so I cannot just use a transpose,  Hope this makes sense.  Please advise and thanks.
0
Comment
Question by:RWayneH
  • 3
  • 3
6 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 41749646
1) You would insert a Worksheet_SelectionChange event on Sheet1 (or the "source" sheet) to capture the row and add/update the two range names, ColB and ColH.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Check if more than one row selected or on header row or if below used cells
    If Target.Rows.Count > 1 Or Target.Row = 1 Or _
       Target.Row > Me.UsedRange.Rows.Count Then Exit Sub
    ActiveWorkbook.Names.Add Name:="ColB", RefersToR1C1:="=" & Me.Name & "!R" & Target.Row & "C2"
    ActiveWorkbook.Names.Add Name:="ColH", RefersToR1C1:="=" & Me.Name & "!R" & Target.Row & "C8"
End Sub

Open in new window


2) You would add formulas in cells A1 and A3 on Sheet2 to point to those two ranges
A1:  =ColH
A3:  =ColB

I've attached a sample workbook to demonstrate.

Regards,
Glenn
EE_Q_28962450.xlsm
0
 

Author Comment

by:RWayneH
ID: 41749746
I am aware of worksheet chg, and the =Sheet2()  pointing to a different sheet, however I am pulling values out of a row to generate a .xml file.  So in a specific row (active row) i need to define some values as variables.   The sheet2 is a xml template and the template section cannot have formulas in those cells.

The rows are specific test scenarios.  So what I need is actually how it is written in the question..  after cells are variables I can use them in the xml and in other places.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 41750561
If you need the values inserted in Sheet2 (rather than have formulas there as I previously suggested) then one only needs to update the macro to write the values in Sheet2, like so:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Check if more than one row selected or on header row or if below used cells
    If Target.Rows.Count > 1 Or Target.Row = 1 Or _
       Target.Row > Me.UsedRange.Rows.Count Then Exit Sub
    ActiveWorkbook.Names.Add Name:="ColB", RefersToR1C1:="=" & Me.Name & "!R" & Target.Row & "C2"
    ActiveWorkbook.Names.Add Name:="ColH", RefersToR1C1:="=" & Me.Name & "!R" & Target.Row & "C8"
    'Insert ColH and ColB values in Sheet2
    Sheets("Sheet2").Range("A1").Value = Range("ColH").Value
    Sheets("Sheet2").Range("A3").Value = Range("ColB").Value
End Sub

Open in new window


Revised workbook attached.

-Glenn
EE_Q_28962450.xlsm
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:RWayneH
ID: 41751297
I actually got the first example to work, and it works very well.  Both are good options.  Thanks for the help.
0
 

Author Closing Comment

by:RWayneH
ID: 41751298
EXCELlent!!  and thanks!!
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 41751401
You're welcome.  Glad I could help.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question