Excel VBA Tables

radonwilson

New Member
Joined
Jun 23, 2021
Messages
49
Office Version
  1. 2019
Platform
  1. Windows
I have written my questions on screenshot, please have a look.

VBA Code:
Sub cleanup()

Dim tbl As Range
Dim ws As Worksheet
Set tbl = Range("a1").CurrentRegion
Set ws = ActiveSheet

ws.Name = "Transactions"
Rows("1:10").Delete


Range("D:D").Select
Selection.Cut
Range("A:A").Select
Selection.Insert

Range("C:E").Select
Selection.Insert

Range("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth

Range("C:E").Delete

    Cells.Replace "Electronic Transactions (Credit Card/Net Banking/GC)", "ET"
    Cells.Replace "Cash On Delivery Transactions and Non-Transactional Fees", "COD"
   
tbl.Select


Selection.WrapText = False
ws.ListObjects.Add(xlSrcRange, tbl, , xlYes).Name = "trans"
ws.ListObjects("trans").TableStyle = ""
ws.ListObjects("trans").HeaderRowRange.Font.Bold = 1

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Please ask your question in your post & not via an image on a share site. Thanks.
 
Upvote 0
1. I have created a variable tbl as Range and set it to - tbl=Range("a1").current region. Given VBA code is not including Column A in table trans. Is it happening because I had cut/insert a Column D before Column A? I know this can be fixed by writing tbl.currnetregion, while creating a table. But I want to know how its not working.



VBA Code:
VBA Code:
Sub cleanup()

Dim tbl As Range
Dim ws As Worksheet
Set tbl = Range("a1").CurrentRegion
Set ws = ActiveSheet

ws.Name = "Transactions"
Rows("1:10").Delete


Range("D:D").Select
Selection.Cut
Range("A:A").Select
Selection.Insert

Range("C:E").Select
Selection.Insert

Range("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth

Range("C:E").Delete

Cells.Replace "Electronic Transactions (Credit Card/Net Banking/GC)", "ET"
Cells.Replace "Cash On Delivery Transactions and Non-Transactional Fees", "COD"
  
tbl.Select


Selection.WrapText = False
ws.ListObjects.Add(xlSrcRange, tbl, , xlYes).Name = "trans"
ws.ListObjects("trans").TableStyle = ""
ws.ListObjects("trans").HeaderRowRange.Font.Bold = 1

End Sub


2. I am unable to create a table this way, do I have to give table style separately?

Code:
ws.ListObjects.Add(xlSrcRange, tbl, , xlYes,,"").Name = "trans"
 
Upvote 0
Given VBA code is not including Column A in table trans. Is it happening because I had cut/insert a Column D before Column A? I know this can be fixed by writing tbl.currnetregion, while creating a table. But I want to know how its not working.
You are correct, Set tbl initially starts at column "A" but when you insert a column before "A" the range of tbl moves to the right.

If you add some debug lines to your code you will see this in the immediate window for tbl.address
Rich (BB code):
Before Inserting Column A
$A$1:$E$20
After Inserting Column A
$B$1:$E$20

I added this debug.print as follows:-
VBA Code:
    Range("D:D").Select
    Selection.Cut
  
        Debug.Print "Before Inserting Column A"
        Debug.Print tbl.Address
    Range("A:A").Select
    Selection.Insert
        Debug.Print "After Inserting Column A"
        Debug.Print tbl.Address

Since you not using the tbl object until right at the end, perhaps move your Set tbl = Range("a1").CurrentRegion closer to the end.

2. I am unable to create a table this way, do I have to give table style separately?

I can't find a way of using the None ("") style in the same line as the .Add command.
You might want to consider doing it this way.
VBA Code:
    With ws.ListObjects.Add(xlSrcRange, tbl, , xlYes)
        .Name = "trans"
        .TableStyle = ""
        .HeaderRowRange.Font.Bold = 1
    End With
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

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