Looking to paste various cells into relevant table

Falcons88

New Member
Joined
Jun 10, 2021
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hi,

I managed to get some help with a code a few weeks back from someone who was very helpful indeed and the VBA he gave me worked a treat, my work book has since progressed and I'm needing the below code to do exactly what its been doing but pasting the highlighted cells in the order shown, I thought by changing what was originally in the code ("A6:D6") to what's in now would work but it hasn't:

Dim ary As Variant, n As Variant

'Names of accounts from 1 to 19
ary = Array("Fixed Assets", "Long Term & Current Assets", "Liabilities", "Income", "Trading Expenses", "Selling & Distribution Expenses", _
"Financial Expenses", "Premises Expenses", "Administration Expenses", "General Expenses")

n = Application.Match(Sheets("Input Sheet").Range("E6").Value, ary, 0)
If Not IsError(n) Then
With Sheets("Chart of Accounts").ListObjects("Table" & n).ListRows.Add.Range 'add a new row and get its Range
.Cells(1).Resize(1, 4).Value = Sheets("Input Sheet").Range("A6,B6,F6,D6,J6").Value
End With
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I have Excel 2010, which doesn't have the TEXTJOIN function, so I can't actually test this but maybe
build a string using the worksheet TEXTJOIN function
then SPLIT the string into an array for the new table row.

Something like this...
VBA Code:
If Not IsError(n) Then
    
    With Sheets("Input Sheet")
        Str = Application.textjoin(",", True, A6, B6, F6, D6, J6)
    End With
    
    With Sheets("Chart of Accounts").ListObjects("Table" & n).ListRows.Add 'add a new row
        .Cells(1).Resize(1, 5).Value = Split(Str, ",")
    End With
    
End If
 
Upvote 0
I have Excel 2010, which doesn't have the TEXTJOIN function, so I can't actually test this but maybe
build a string using the worksheet TEXTJOIN function
then SPLIT the string into an array for the new table row.

Something like this...
VBA Code:
If Not IsError(n) Then
   
    With Sheets("Input Sheet")
        Str = Application.textjoin(",", True, A6, B6, F6, D6, J6)
    End With
   
    With Sheets("Chart of Accounts").ListObjects("Table" & n).ListRows.Add 'add a new row
        .Cells(1).Resize(1, 5).Value = Split(Str, ",")
    End With
   
End If
Thanks for replying @NoSparks , unfortunately it isn't working, as to why is beyond my understanding, I did read the textjoin article and I understand how it works but don't see why it doesn't like the code. Below is a screen shot of what it's saying. I understand how certain code works but its how to combine it all together I struggle to grasp.
1624606492053.png
 
Upvote 0
In sorting this out I've discovered str is a VBA key word so shouldn't be used as a variable name.
I haven't had any issues with that before, likely because I use Option Explicit and declare all variables.
Try
Rich (BB code):
Sub testing()

Dim ary As Variant, n As Variant
Dim sStr As String

'Names of accounts from 1 to 19
ary = Array("What", "Ever", "Your", "Headers", "Are")   '<--- change to actual headers

n = Application.Match(Sheets("Input Sheet").Range("E6").Value, ary, 0)
If Not IsError(n) Then
    
    With Sheets("Input Sheet")
        sStr = Application.textjoin(",", True, A6, B6, F6, D6, J6)
    End With
    
    With Sheets("Chart of Accounts").ListObjects("Table" & n).ListRows.Add 'add a new row
        .Cells(1).Resize(1, 5).Value = Split(sStr, ",")
    End With
    
End If
End Sub
 
Upvote 0
Another option
VBA Code:
With Sheets("Chart of Accounts").ListObjects("Table" & n).ListRows.Add.Range 'add a new row and get its Range
.Cells(1).Resize(1, 5).Value = Application.Index(Sheets("Input Sheet").Range("A6:J6").Value, 1, Array(1, 2, 6, 4, 10))
End With
 
Upvote 0
Solution
Another option
VBA Code:
With Sheets("Chart of Accounts").ListObjects("Table" & n).ListRows.Add.Range 'add a new row and get its Range
.Cells(1).Resize(1, 5).Value = Application.Index(Sheets("Input Sheet").Range("A6:J6").Value, 1, Array(1, 2, 6, 4, 10))
End With
Works an absolute treat, thank you very much for this @Fluff , you've put me in good spirits to see Lions take apart Japan tomorrow?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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