VBA - Convert range into table

TopLearner

Board Regular
Joined
Feb 25, 2022
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

I hope you are all well.

I have a spreadsheet with a tab called "summary" and a range D18:G18 with data on it.
I am trying to convert it into a table by writing a code but it does not work. I have tried two different approaches but they do not work.

VBA Code:
Sub ConvertirRangoenTabla()


Dim Rango As Range
Dim Hoja As Worksheet


Set Rango = Worksheets("Summary").UsedRange
Set Hoja = ActiveSheet

Hoja.ListObjects.Add(SourceType:=XlRangoRange, Source:=Rango, xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Summary"

End Sub


This is giving me this error: "Run-time error '5: Invalid procedure call or argument


Then, I try another one


VBA Code:
Sub ConvertirRangoenTablaII()


Dim Rango As Range
Dim Hoja As Worksheet


Set Rango = Range("D3").CurrentRegion
Set Hoja = ActiveSheet


Hoja.ListObjects.Add(SourceType:=XlRangoRange, Source:=Rango, xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Summary"


End Sub

And, I get the same error
"Run-time error '5: Invalid procedure call or argument


Can you please help?

Thanks
Sanchez
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try changing
VBA Code:
Hoja.ListObjects.Add(SourceType:=XlRangoRange, Source:=Rango, xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Summary"


to
Rich (BB code):
Hoja.ListObjects.Add(SourceType:=xlSrcRange, Source:=Rango, xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Summary"
 
Upvote 0
Try changing
VBA Code:
Hoja.ListObjects.Add(SourceType:=XlRangoRange, Source:=Rango, xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Summary"


to
Rich (BB code):
Hoja.ListObjects.Add(SourceType:=xlSrcRange, Source:=Rango, xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Summary"
Thanks Mark,

My question here is, why xlSrcRange if my variable is called Rango. Is Src something that must be always used? Any reasons for that?

Does Src stand for "Source" and that's why we add Source:=Rango to the code?

Regards
Sanchez
 
Upvote 0
It is not a variable, it is a source type. It tells Excel where it is getting the data from.
The source types are


xlSrcExternal
xlSrcModel
xlSrcQuery
xlSrcRange
xlSrcXml
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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