Solved

Naming a range based on uniqueness of another column

Posted on 2016-08-10
11
36 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
[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
  • 5
  • 4
  • 2
11 Comments
 
LVL 46

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 31

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
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.

 
LVL 31

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
 
LVL 31

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 46

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 31

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
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.
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…

630 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