+ Reply to Thread
Results 1 to 3 of 3

what is wrong with the code

Hybrid View

  1. #1
    Registered User
    Join Date
    08-25-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    19

    what is wrong with the code

    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

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,644

    Re: what is wrong with the code

    I think it would help others to help you if you uploaded a sample workbook with some typical data so the code can be seen and tested in context.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: what is wrong with the code

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    PLEASE PM WHEN YOU HAVE DONE THIS AND I WILL DELETE THIS POST
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1