VBA - inseritng a table

Barrakooda

Board Regular
Joined
Feb 3, 2012
Messages
75
Hi all

I am using the following VBA to select data on a sheet, does anyone know what to put after .Select to insert a table?

I have tried recording a macro, but seems to limit itself to the range selected while recording. The range will vary.

Sub PickedActualUsedRange()
Range("A1").Resize(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column).Select
End Sub

Thanks in advance
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Are you talking about inerting a Pivot table ?
You don't need to Select any data, simply make sure your activecell is withing the data range, the recorder will select ALL data
 
Upvote 0
Thanks Michael

No it's not for a pivot table, just a simple table. When I recorded macro previously it put in Table 4 I think from memory. I simply clicked on Table in Insert menu.

I know this can be done manually butt this will be part of a grander macro to automate quite a few actions.
 
Upvote 0
Ok, this will select the Usedrange of a worksheet.....but try to avoid using Select whenever possible.....it's generally not required !!
Once you've selected the entire range what do you want to do with it ???

Code:
Sub MM1()
ActiveSheet.UsedRange.Select
End Sub
 
Upvote 0
Darn, I timed out....this will add the table
Code:
Sub MM1()
    With ActiveSheet
       .UsedRange
       .ListObjects.Add(xlSrcRange, ActiveSheet.UsedRange, , xlYes).Name = "Table2"
    End With
End Sub
 
Upvote 0
I want to insert a basic table with headers, different coloured rows etc over the data selected. Since when I dump all my data on to different sheets they will all have varying ranges. More the rows than anything else.

What's the issue with using .Select in VBA?
 
Upvote 0
See my previous post......I didn't use Select
You say you are creating a grander macro !
If you use Select.Selection throughout you will find the process could be slowed considerably, especially if there are many sheet changes, range changes, etc.
By avoiding this, your code will be quicker, cleaner, and easier to debug.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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