I have a worksheet with 50 tabs for each state and recurring data in the similar format in all the tabs.
data1 data2
Month1 Temperature Humidity
Month2 Temperature Humidity
Month3 Temperature Humidity
Total Temperature HUmidity
Month1 Temperature Humidity
Month2 Temperature Humidity
Month3 Temperature Humidity
Total Temperature HUmidity
Now I need to do 2 things:
First, I want to copy and paste all the data from each state tab from first workbook to corresponding tabs in another workbook.
Second, I need to inser t 2 blank rows and a heading row after each total in the copied workbook.
My code is below:
Public sub Statedata()
Dim book1 as workbook : Set book1 =workbooks(State_data_source)
Dim book2 as workbook : Set book2 =workbooks(State_data_dest)
Dim state as string
Dim j as integer
Dim rngtotal as range
Dim c as range
Dim I as integer
Dim count as integer
Dim numrows as long
********************************************************************************
‘code to copy data from each state tab from first worksheet to another worksheet in the corresponding tab
**********************************************************************************
For j= 1 to book1.sheets.count
State=book(1).sheets(j).name
Book(1).sheets(j).copy
Book(2).Sheets(state).Paste
Next j
********************************************************************************
‘code to insert 2 blank row and a heading row between each total and Month1 in the destination workbook
**********************************************************************************
Count =application.sheets.count
Numrows =2
Book(2).sheets(1).select
For i=1 to 50
Set rndtotal=activesheet.usedrange
For each c in rngtotal
If right(c.value,5)=”Total” and c.offset(1,0).value=”Month1” then
Rows(“1:1”).copy
c.offset(1).entirerow.insert
c.offset(1,0).entirero0w.resize(numrows).insert shift: = xldown
else
end if
nextc
activesheet.next.select
next i
set rngtotal = nothing
set book1 = nothing
set book2 =nothing
The problem with the above code is whenever I run it, it is creating a new worksheet every time and copying data instead of copying data in each state tab in destination workbook and I cannot figure out why it is happening.
Any suggestion would be much appreciated.
Thanks,
Blyzzard
Bookmarks