?
Solved

Variable naming accross a row

Posted on 2016-08-09
6
Medium Priority
?
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 2000 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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