Solved

Variable naming accross a row

Posted on 2016-08-09
6
55 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Industry Leaders: 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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

737 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