How to select different tables (Table1, Table2, Table3... using VBA

Chiwidan

New Member
Joined
Apr 19, 2020
Messages
15
Office Version
  1. 2010
Platform
  1. Windows
Hi guys,

I hope you are having a good week so far. Greetings from New Zealand.

I am stuck but I will say this will be quite a basic question:

In a worksheet called "calculations" I have different 10 tables (From Table2 to Table11)

I am trying to pull the information from Table"i+1" but I am unsure how to code it.

Below is what I have but it gets stuck in the first line (on yellow)

VBA Code:
Sub Invoice_Making()


Dim shMacros As Worksheet, shCalculations As Worksheet, shInvTemplate As Worksheet
Set shMacros = Worksheets("Macros")
Set shCalculations = Worksheets("Calculations")
Set shInvTemplate = Worksheets("Invoice Template")

'Inserts new sheets, and names them a name according to form
 
Sheets("Macros").Select

Dim sheet_count As Integer

sheet_count = Range("E2")

Dim sheet_name As String
Dim i As Integer



For i = 1 To sheet_count


'copy NHI from Calculations to Invoice Template


    [COLOR=rgb(247, 218, 100)]shCalculations.Range("Table" & "(i+1)[NHI]").Copy shInvTemplate.Range("X3")[/COLOR]
 
    shCalculations.Range("Table&i+1[NHI2]").Copy shInvTemplate.Range("X19")
 
    shCalculations.Range("Table&i+1[NHI3]").Copy shInvTemplate.Range("X35")
shCalculations.Range("Table&i+1[NHI4]").Copy shInvTemplate.Range("X51")
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
What are the names of your tables?
 
Upvote 0
What are the names of your tables?
Well, the page will have 10 tables from Table2 to Table11, but not allways the 10 tables will be used...

The idea will be something like

for i = 1 to n
Take info from table(i+1) and copy it to another worksheet...
... other stuff.....
next i
 
Upvote 0
In that case use
VBA Code:
shCalculations.Range("Table" & i + 1).Copy shInvTemplate.Range("X3")
but you are going to be copying the data to the same cells
 
Upvote 0
In that case use
VBA Code:
shCalculations.Range("Table" & i + 1).Copy shInvTemplate.Range("X3")
but you are going to be copying the data to the same cells
Hi Fluff,

well, the idea is that the for i = 1 it will take the data from Table2 and copy to the InvTemplate worksheet... do something ... delete the info in InvTemplate and then the loop will go to i = 2, to take the date of Table3 and so on...
 
Upvote 0
Did the change I suggested do what you needed?
 
Upvote 0
Did the change I suggested do what you needed?
Hi Fluff... it did!!!! yay!!!! thanks heaps... It copied the formula that was in the Table... what would be the code for values only?

Thanks again...

Dan
 
Upvote 0
How about
VBA Code:
With shCalculations.Range("Table" & i + 1)
   shInvTemplate.Range("X3").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
 
Upvote 0
How about
VBA Code:
With shCalculations.Range("Table" & i + 1)
   shInvTemplate.Range("X3").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
VBA Code:
'copy NHI from Calculations to Invoice Template


    shCalculations.Range("Table" & i + 1 & "[NHI]").Copy shInvTemplate.Range("X3")
    
With shCalculations.Range("Table" & i + 1 & "[NHI]")

    shInvTemplate.Range("X3").Resize(.Rows.Count, .Columns.Count).Value = .Value


It did the trick!!!!! :) Genius!!
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

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