Solved

Variable naming accross a row

Posted on 2016-08-09
6
35 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now