Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Several worksheets mess up

Posted on 2011-03-17
4
Medium Priority
?
182 Views
Last Modified: 2012-05-11
Hi,
I have a button click event in file "file1". When I click it, a file dialog is open and I select another excel file "file2".
Now my logic is that to compare the sheets between the files "file1" and "file2" than doing something.
Private Sub CommandButton1_Click()
    Dim FullFileName As String
  FullFileName = Application.GetOpenFilename("Excel files (*.xl*),*.xl*", _
    1, "Custom Dialog Title", , False)
    
    Dim m As Integer
    m = Sheet1.xlCellTypeLastRow
    
    Workbooks.Open FullFileName
    Dim wbOpen As Workbook
    Set wbOpen = Workbooks.Open(FullFileName)
  
    With wbOpen
        Dim X As Long
        X = xlCellTypeLastRow
        Dim i As Integer
        Dim j As Integer
        Dim a As Integer
        Dim b As Integer
        For i = 1 To X
              wbOpen.Sheets(2).Cells(i, 1) = wbOpen.Sheets(1).Cells(i, 1)
              wbOpen.Sheets(2).Cells(i, 2) = wbOpen.Sheets(1).Cells(i, 2)
              wbOpen.Sheets(2).Cells(i, 3) = wbOpen.Sheets(1).Cells(i, 3)
              For a = 1 To m
                    If (Sheet1.Cells(a, 3) = wbOpen.Sheets(1).Cells(i, 1) And Sheet1.Cells(a, 4) = wbOpen.Sheets(1).Cells(i, 2) And Sheet1.Cells(a, 5) = wbOpen.Sheets(1).Cells(i, 3)) Then
                        b = a
                    Exit For
                    End If
              Next a
              wbOpen.Sheets(2).Cells(i, 4) = Sheet1.Cells(b, 1) 'Here Sheet is supposed to be in file1
              wbOpen.Sheets(2).Cells(i, 5) = Sheet1.Cells(b, 2)
        Next i
    End With
    

Open in new window

Please notice Sheet1 in the code is supposed to be in file1 but I found it is in file2. How to modify the code?
Thanks
0
Comment
Question by:zhshqzyc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
StephenJR earned 1000 total points
ID: 35160545
Try adding a workbook reference, e.g.

wbOpen.Sheets(2).Cells(i, 4) = ThisWorkbook.Sheet1.Cells(b, 1)

or

wbOpen.Sheets(2).Cells(i, 4) = ThisWorkbook.Sheets(1).Cells(b, 1)
0
 

Author Comment

by:zhshqzyc
ID: 35160576
Still wrong. Because I opened two files at the same time. Both of them have the same name ThisWorkbook. I checked the value in the immediate window. It is the value in file2.
0
 

Author Closing Comment

by:zhshqzyc
ID: 35160618
My fault. You are rigjht.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35160637
ThisWorkbook refers specifically to the workbook containing the code.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question