Results 1 to 3 of 3

Thread: converting and copying columns in excel

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    converting and copying columns in excel

    I have a problem with my code, I am trying first to copy columns from worksheet1 to worksheet2 inside the same workbook and second i want to convert column 2 in worksheet1 which contains string into an integer column
    by connecting to a database and retrieving the ID corresponding to each cell and copy the ID in the same cell but in worksheet2.Everything's ok but my problem is that I obtain the following message:
    "A file named "index30052006.xls" already exists in this location.Do you want to replace it?".Can anyone help me to find what's wrong?


    VB Code:
    1. Private Sub cmdmerge_Click()
    2. 'Open the Workbook
    3. Set wkbSource = Workbooks.Open("C:\index30052006.xls")
    4.  
    5.  
    6. 'Set wksSource = wkbSource.Worksheets(1)
    7. 'Set wksTarget = wkbSource.Worksheets(2)
    8.  
    9. 'LastRow = wksSource.UsedRange.End(xlDown).Row
    10.  
    11. Worksheets(1).Columns(1).Copy Worksheets(2).Columns(10)
    12. Worksheets(1).Columns(4).Copy Worksheets(2).Columns(6)
    13. Worksheets(1).Columns(5).Copy Worksheets(2).Columns(8)
    14. Worksheets(1).Columns(8).Copy Worksheets(2).Columns(7)
    15. Worksheets(1).Columns(9).Copy Worksheets(2).Columns(12)
    16. Worksheets(1).Columns(11).Copy Worksheets(2).Columns(9)
    17.  
    18. nextstep
    19. End Sub
    20.  
    21.  
    22. Private Sub Matching()
    23. Rs1.Open "Select * from Category where DescriptionCategory like '" & scat & "'", cnn, adOpenStatic, adLockOptimistic
    24. If Rs1.RecordCount = 0 Then
    25. Worksheets(2).Cells(i, 2).Value = "No Value"
    26. Else
    27. Worksheets(2).Cells(i, 2).Value = Rs1!CategoryID
    28. End If
    29. Rs1.Close
    30. End Sub
    31.  
    32. Private Sub nextstep()
    33. Dim lastRow As Integer
    34.  
    35. lastRow = Worksheets(1).UsedRange.End(xlDown).Row
    36. For i = 2 To lastRow
    37. scat = Worksheets(1).Cells(i, 2)
    38. Matching
    39. Next i
    40. wkbSource.Save
    41. wkbSource.Close False
    42.   Set wkbSource = Nothing
    43. End Sub

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: converting and copying columns in excel

    well u are saving the worksheet

    wkbSource.Save

    but u arent using SaveAs so im not sure why its prompting u??? odd....

    but do this

    VB Code:
    1. Application.DisplayAlerts False
    2.     wkbSource.Close True
    3. Application.DisplayAlerts True

    I took out the SAVE line since u are closing it right after u can just call SAVE on the close by using Close True
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: converting and copying columns in excel

    engnouna ...

    You may find a lot of your problems if you put "Option Explicit" as the very top line of your module. It will show you a lot of your errors. What is happening is that you are treating "wkbSource" like a Global variable, but it is only defined in "Sub cmdmerge_Click" and is ONLY local to that subroutine where it is also opened. You need to learn about the "Scope" of variables (DKenny said the same thing in response to your Thursday post). You are using the same variable NAME "wkbSource" in "Sub nextstep", but I'm pretty sure VBA assumes that this is the ActiveWorkbook and not necessarily the same workbook it opened in "Sub cmdmerge_Click". Even though the name is the same in both subroutines, VBA is treating them like entirely different Workbooks. "Option Explicit" prevents VBA from making this type of assumption ... you have to explictly and properly define all variables yourself, so it will complain that you have not properly defined "wkbSource" in "Sub nextstep", and you'll have to add a DIM line to fix it. That will still result in two different variables.

    To make "wkbSource" a "Global" handle for the Workbook, put it above the start of the first Subroutine/Function in the module. I generally prefix my Globals with a "g" or a "G", so it would look like "G_wkbSource". Now, the same book is referenced whenever you use that name.

    The other option is to pass the handle for the open workbook as a parameter to the "Sub nextstep", making it something like "Sub nextstep(byref aBook as Workbook)". Now, the call to "nextstep" becomes "nextstep(wkbSource)". If you really want to, you can use "wkbSource" instead of "aBook" in "Sub nextstep" ... pick whatever name is the easiest for you to follow.
    Last edited by Webtest; Jun 3rd, 2006 at 09:27 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

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



Click Here to Expand Forum to Full Width