If Range does not include necessary header

VTHokie11

New Member
Joined
Oct 26, 2011
Messages
19
I'd like to do two different things depending on whether or not a selected range starts with row 1 (row containing headers).

More specifically I'd like to copy and past the selected range to a "new sheet" if it does not include the first row to a new sheet starting in A2, then go back to the data source sheet and grab the header from whichever column the range came from and place it in A1 on the "new sheet."

This would allow me to run an automated pivot with the correct header no matter whether or not the user had selected the header with their range or not...feel free to recommend better ways of getting the proper header. I'm VERY rusty on VBA, thank you for any help/advice!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Perhaps something like this which grabs all the data in the selected columns (including any headers) and copies it to a different sheet.
Code:
With Selection.EntireColumn
    With Range(.Cells(1, .ColumnsCount), .Cells(.Rows.Count, 1).End(xlup))
        .Copy destination:=ThisWorkbook.Sheets("Sheet2").Range("A1").Resize(.Rows.Count, .Columns.Count)
    End With
End With
 
Upvote 0
I tried the bit of code you so kindly provided, unfortunately I got the following error:

Run-time error '438':
Object doesn't support this property or method.

But good news is I was thinking the same thing to get around this little issue...if the selected portion of a column does not include row 1 (the row with the headers) paste the selection to a new sheet starting in row 2 (leaving row one empty for the header). Then go back and grab the corresponding header to that selection and paste above then make the selection again now with the header. Now to code that... Sorry for late response to your response and thank you for your help!
 
Upvote 0
You also might want to look at the .CurrentRegion property.

About the error, when you run the code, are cells selected or is (possibly) a control selected?
 
Upvote 0
When I ran that code I had the cells of interest selected. I must not be explaining the problem I'm encountering well enough let me try and explain it a little more clearly. Sheet has a bunch of columns describing various aspects of an incident qualitatively:

Date | Collision Cause | Header 3 | Header 4| Header n|
1/24/2012 Drunk Driving
1/12/2012 Cell Phone Use
1/2/2012 Tired Driving
1/2/2012 Wet Conditions
12/27/2011 Wet Conditions
12/20/2011 Drunk Driving
12/15/2011 Tired Driver
12/15/2011 Aggressive Driving

Unfortunately my frequency table which is created via a pivot table is off unless the time span of interest includes the most recent incident row (1/24/2012). This is because it interprets the first cell within the selected single column range (user will always select a single column range) as the header and begins the frequency count with the next cell within the selected single column range.

What I need to figure out how to code is something that will perhaps grab the header and stitch it together with the rest of the selected range in these situations where the header row (and most recently recorded incident for that matter) are not included.

I was thinking that perhaps the easiest way to do that is if the header row is included run my frequency table creation macro normally, but when it's not included copy the selected range to a new work sheet starting in B2, then go back to the section sheet and pull the appropriate corresponding header. Then I would have the header with the data of interest and could run the frequency table macro. Thank you so much for reading this and thank you in advance for any help / advice anyone may provide.
 
Upvote 0
Perhaps something like this. (In practice it would include a check to see if Selection.Row =1)
Code:
With Selection.Offset(-1,0)
    .Cells(1,1).Value = "Date" Then .Select Replacing:=False
End With

I'm doing this from memory and I might have gotten the action of the Replacing argument of Select wrong way 'round.
 
Upvote 0
Mike, Thanks for your response. Let me know if my understanding of the code snippet you provided is correct.

Code:
//Select one cell above the current selection
With Selection.Offset(-1,0)

//This second line I'm not sure if I completely understand but from looking up //(http://www.tushar-mehta.com/excel/vba/multiple_sheets.htm) .Select //Replace and setting it to false allows you to add to the current selection?

    .Cells(1,1).Value = "Date" Then .Select Replacing:=False
End With

I think you may be on to something here. If I can figure out how to get this command to work along with the check to see if the selection row = 1 (in which case it could just operate without .Select Replace:= False), perhaps I can select the header along with the desired data at the same time!

This might even make it possible without having to create a new sheet to "stitch" them together as I had previously imagined (paste the data starting in A2 and then go back to the data sheet, get the header and paste in A1 then re-select data now along with the corresponding header).

Was I understanding you correctly Mike? Thanks again for your help! This will be awesome if I can figure this out with y'alls help!
 
Upvote 0
My response was to the sitution where the user had incorrecetly selected the range they wanted to be acted on. Code that accounts for user's sloppy habits is nessesary in every user interaction.

If I am mistaken and the identification of the range is being done by you, via code, the easiest approach is for you to know where the headers are in your project and handle them appropriatly.
 
Upvote 0
Ah my fault, the situation where the user does not select a range including the header row is not actually a mistake. The workbook contains a lot of incident information which the user may want to summarize. They may want to summarize some dates that are contained in the "middle" so to speak of the rows (i.e. not including the most recently recorded incident and therefore not including the header row).

I actually am utilizing a application input box to prompt the user to select the range of interest. Here is the code below I'm trying to fix this problem with (probably should have posted this code earlier but thought it may have been scaring away any help, hence the cross posts sorry again about that!).

Code:
Sub DoAll()
    
    Dim CloudData As Range
    
    On Error Resume Next
    
    'Asks user to specify which column of data they wish to summarize
    Set CloudData = Application.InputBox("Please select a range with the incident information you wish to summarize.", _
                                              "Specify Incident Information", Selection.Address, , , , , 8)


'Sub MakeTable()
Dim Pt As PivotTable
Dim strField As String
    
    'Pass heading to a String variable
    '*Need to make edit here most likely with some sort of if to use the first row heading
    'if selected data does not include this. i.e. if selected range starts with second row
    'or after.*
    
    'strField = Selection.Cells(1, 1).Text
    strField = CloudData.Cells(1, 1).Text
    
    'Name the list range not using the xlDown because there exists
    'the possibility of blanks in the column data.
    
    'Range(Selection, Selection.End(xlDown)).Name = "Items"
    CloudData.Name = "Items"
       
    'Create the Pivot Table based off our named list range.
    'TableDestination:="" will force it onto a new sheet
    
     ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
         SourceData:="=Items").CreatePivotTable TableDestination:="", _
             TableName:="ItemList"
                
    'Set a Pivot Table variable to our new Pivot Table
    Set Pt = ActiveSheet.PivotTables("ItemList")
    
    'Place the Pivot Table to start from A1 on the new sheet
    ActiveSheet.PivotTableWizard TableDestination:=Cells(1, 1)
        
    'Move the list heading to the Row Field
    Pt.AddFields RowFields:=strField
    
    'Move the list heading to the Data Field
    Pt.PivotFields(strField).Orientation = xlDataField

ActiveSheet.Name = "FreqTable"

''Attempting to sort resulting table descending (not working so commented
''out until I figure out the header issue
'    With Range("A1", Cells(Rows.Count, 2).End(xlUp)).Resize(, 2)
'        .Sort .Cells(1, 2), xlDescending
'    End With

'Call CreateCloud

End Sub
 
Upvote 0
I started a new thread here http://www.excelforum.com/excel-pro...itle-count-of-____.html?p=2691676#post2691676 because I thought up another possible solution to this question and couldn't post pictures or tables here.

Basically since the pivot creation code still works and is just a "count" off, perhaps instead of trying to get the range pieced with the right header before the frequency table (pivot table) gets created it would be best to just let the table get created and then fix it.

However one added complexity that is not apparent in the tables in that thread is that if the first cell in the selected range is not repeated it will only be in the table in the first two rows and when looking for its match within the pivot table (frequency table) to increment its count it won't find a match and would have to input it and make its count 1. I think that can be dealt with however and that this is still a preferable way to go than the previously discussed possibilities. Thanks again for all your help!
 
Upvote 0

Forum statistics

Threads
1,215,963
Messages
6,127,948
Members
449,412
Latest member
montand

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top