Excel 2013 won't format as a table after importing from a CSV using VB script

BellaxPalus

New Member
Joined
Apr 26, 2013
Messages
6
I have a sheet that I am importing data from a CSV file that I then unlink it from the data and format it as a table. This works perfectly when I do it manually but when I attempt to do this with a Macro I always get a "Run-time Error 1001" unless I save and reopen the file.



I delete the external data connections using this bit of code from elsewhere in this forum

Code:
[/FONT][/COLOR][COLOR=#333333][FONT=Verdana]sub delete_Connections()[/FONT][/COLOR]

[COLOR=#333333][FONT=Verdana]do while activeworkbook.connections.count > 0[/FONT][/COLOR]

[COLOR=#333333][FONT=Verdana]activeworkbook.connections.item(activeworkbook.connections.count).delete[/FONT][/COLOR]

[COLOR=#333333][FONT=Verdana]loop[/FONT][/COLOR]

[COLOR=#333333][FONT=Verdana]end sub[/FONT][/COLOR][COLOR=#222222][FONT=Verdana]

https://www.mrexcel.com/forum/excel...data-connections-post3343454.html#post3343454



I also recorded my format steps so if there is a better way to format as a table I am open to improving the code.

Code:
Range("A1:F17").Select    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$17"), , xlYes).Name = _[/FONT][/COLOR]

[COLOR=#222222][FONT=Verdana]        "Table1"[/FONT][/COLOR]

[COLOR=#222222][FONT=Verdana]    Range("Table1[#All]").Select[/FONT][/COLOR]

[COLOR=#222222][FONT=Verdana]    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium2"



This code does remove the connections if you go into the connections manager, but something seems to hold it in memory until you close and reopen the file.



Having to close and open the file after the <g class="gr_ gr_28 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="28" style='background-position: -1px calc(100% + 3px); color: rgb(34, 34, 34); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif; font-size: 13px; font-style: normal; font-weight: 400; word-spacing: 0px; border-bottom-color: transparent; border-bottom-width: 2px; border-bottom-style: solid; display: inline; white-space: normal; orphans: 2; widows: 2; background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPScxMDAlJyBoZWlnaHQ9JzEwMCUnPgogIDxsaW5lIG9wYWNpdHk9JzAuNzUnIHgxPSc0JyB5MT0nMTAwJScgeDI9JzEwMCUnIHkyPScxMDAlJyB0cmFuc2Zvcm09J3RyYW5zbGF0ZSgtMS41LCAtMi41KScgc3Ryb2tlLXdpZHRoPSczJyBzdHJva2UtbGluZWNhcD0ncm91bmQnIHN0cm9rZT0nI2ZjNTQ1NCcvPgo8L3N2Zz4K"); background-repeat: no-repeat; background-size: calc(100% + 1px) 100%; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' data-gr-id="28">import</g> kind of removes the automation I am going for. I have a workaround where I have to launch a second script after importing to finish the formatting but I am trying to make this a one-button job so I can give it to others in my shop that have to do the same work.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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