Solved

Variable naming accross a row

Posted on 2016-08-09
6
59 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

726 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