Solved

Naming a range based on uniqueness of another column

Posted on 2016-08-10
11
29 Views
Last Modified: 2016-08-11
The attached sheet has a key in column A, it defines how many are in a range.  Using ActiveCell.  (assuming the active cell is the first record in a bunch, and I click on it.)  So I would like to define a range in column B and column C.  So if I place my cursor in A2, it would identify B2 thru B7 and name it "Qtys"  and C2 thru C7 as "CatCode"

I would then like to goto sheet2 and be able it paste in  Qtys and Catcode.

Is there a way to just grab one row, 4 rows or 6 rows as ranges to use when pasting it into another sheet?
GroupingBasedOnAnotherCol.xlsx
0
Comment
Question by:RWayneH
  • 5
  • 4
  • 2
11 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 41751365
You can add a header line and do filtering, like I show in the attached version of your workbook
GroupingBasedOnAnotherCol.xlsx
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41751517
Please try this....


Sub CopyPasteAsPerSelectionInColumnA()
Dim sws As Worksheet, dws As Worksheet
Dim sRow As Long, eRow As Long

Application.ScreenUpdating = False
Set sws = Sheets("Sheet1")
Set dws = Sheets("Sheet2")

If Selection.Parent.Name <> sws.Name Then
    MsgBox "Please select the cell on " & sws.Name & " and then try again...", vbExclamation, "Wrong Sheet Selected!"
    Exit Sub
End If
If Selection.Column <> 1 Then
    MsgBox "Please select a cell in column A and then try again...", vbExclamation, "Wrong Selection!"
    Exit Sub
End If

sRow = sws.Range("A:A").Find(what:=ActiveCell.Value).Row
eRow = sws.Range("A:A").Find(what:=ActiveCell.Value, searchdirection:=xlPrevious).Row
dws.Cells.Clear
sws.Range("B" & sRow & ":C" & eRow).Copy dws.Range("A1")
dws.Columns.AutoFit
dws.Activate
Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:RWayneH
ID: 41751537
I don't believe the question mentions filtering in fact the object to this is not to filter.   Did not see in the attached named ranges of  Qty or CatCode? That's why I have named ranges in the question title.  Maybe you misunderstood the question or I am not following your reply?  The goal is, from the active cell, name the two ranges, and leave the active cell as it is.  Unless the macro filters than names then unfilters?  That is why I am even wondering if this can be done.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41751544
As per your description, I assumed that your ultimate goal is to paste the Qtys and CatCodes of the selected group onto the Sheet2 and to do that creating named ranges is not required. I am not sure why do you want to do that?

BTW try the following tweaked code, this will create two named ranges as per the selected group in col. A.

Is this what you are trying to achieve?
Sub CopyPasteAsPerSelectionInColumnA()
Dim sws As Worksheet, dws As Worksheet
Dim sRow As Long, eRow As Long

Application.ScreenUpdating = False
Set sws = Sheets("Sheet1")
Set dws = Sheets("Sheet2")

If Selection.Parent.Name <> sws.Name Then
    MsgBox "Please select the cell on " & sws.Name & " and then try again...", vbExclamation, "Wrong Sheet Selected!"
    Exit Sub
End If
If Selection.Column <> 1 Then
    MsgBox "Please select a cell in column A and then try again...", vbExclamation, "Wrong Selection!"
    Exit Sub
End If

sRow = sws.Range("A:A").Find(what:=ActiveCell.Value).Row
eRow = sws.Range("A:A").Find(what:=ActiveCell.Value, searchdirection:=xlPrevious).Row
dws.Cells.Clear
sws.Range("B" & sRow & ":B" & eRow).Name = "Qtys"
sws.Range("C" & sRow & ":C" & eRow).Name = "CatCode"
sws.Range("B" & sRow & ":C" & eRow).Copy dws.Range("A1")
dws.Columns.AutoFit
dws.Activate
Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:RWayneH
ID: 41751602
Neeraj
This is looking very promising.  Having issues with: Ln20  dws.Cells.Clear

Can we delete all rows under paste?  so if it puts in 6 rows of data, delete all rows under that last one.  I chg'd the paste destination to  G2 but that should not do much.  Is that possible to have a clean paste into the new sheet?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41751649
Line#20 clears the sheet2 before new data is pasted onto it.
Do you mean pasting onto a new sheet each time the code is executed?

I am having a hard time to understand your description. Can you upload a sample workbook mocking up the desired output mentioning the each step you want to automate?
0
 

Author Comment

by:RWayneH
ID: 41751978
I am going to try a different clear prior to pasting them in.. that may solve the issue.  Need to do a little more testing..  Will let you know later today.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41752464
There is another method, but it also requires the columns to have headers.  Shall I post that solution?
0
 

Author Comment

by:RWayneH
ID: 41752495
thanks aikimark.. but I believe the one posted is going to work still testing it.. but all is good so far
0
 

Author Closing Comment

by:RWayneH
ID: 41752680
Excellent!!!  Thanks for the help.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41752776
You're welcome. Thanks for the feedback.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
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…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

705 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

20 Experts available now in Live!

Get 1:1 Help Now