p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Excel VBA (http://p2p.wrox.com/forumdisplay.php?f=79)
-   -   transferring/copying from spreadsheet to another (http://p2p.wrox.com/showthread.php?t=61831)

Aprile September 6th, 2007 10:37 AM

transferring/copying from spreadsheet to another
 
I have this code that I would like to use to get info from one spreadsheet to add to another, but there's always this "Object variable or With block variable not set."

Any ideas as to how to remedy this? Here's the code:

Code:

Option Explicit
Sub mUpdate()
Dim vtest As Boolean, ctest As Boolean, return1 As Integer, rowcount As Integer, column As Integer, count As Integer
Dim compfile As Workbook, tcol As Integer, trow As Integer, filename As String

filename = "C:\Users\lthuynh\Documents\MH\Lab Data Collection_ final_aag2"
'Set compfile = Workbooks.Open(filename:=filename, Format:=5)


count = 0
rowcount = 3
column = 2
tcol = 1
trow = 1
ctest = True
vtest = True

return1 = MsgBox("Update the spreadsheet?", vbOKCancel, "Updating the spreadsheet")
If return1 = 1 Then
    While vtest
        If rowcount < 2106 Then

        While ctest

            If (sheet1.Columns.Cells(rowcount, column) = compfile.Worksheets("Lab Result-Order Code").Cells(trow, tcol).Value) Then

            sheet1.Columns.Cells(rowcount, 4) = compfile.Worksheets("Lab Result-Order Code").Cells(trow, 5).Value
            ctest = False
            ElseIf rowcount > 3093 Then
            ctest = False
            Else
            trow = trow + 1
            End If
        Wend
        rowcount = rowcount + 1
        Else
        vtest = False
        End If
    Wend
End If

End Sub

Thanks in advance.

Kind regards,



Aprile
www.accessdatabaserepair.com - Access Repair Tool

Shasur September 6th, 2007 11:42 PM

My guess would be

Replace
filename = "C:\Users\lthuynh\Documents\MH\Lab Data Collection_ final_aag2"
'Set compfile = Workbooks.Open(filename:=filename, Format:=5)

with

filename = "C:\Users\lthuynh\Documents\MH\Lab Data Collection_ final_aag2.xls"
Set compfile = Workbooks.Open(filename:=filename, Format:=5)

Try and let me know if it is still creating problem

Cheers
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)

royUK September 7th, 2007 06:16 AM

You give Filename a value, but you do not use it.

You declare compfile as a Workbook, but you don't give it a value.As a result the following line gives you the error:

            If (Sheet1.Columns.Cells(rowcount, column) = compfile.Worksheets("Lab Result-Order Code").Cells(trow, tcol).Value) Then

This is also wrong

If (Sheet1.Columns.Cells(rowcount, column)


Try

Option Explicit

Sub mUpdate()
    Dim vtest As Boolean, ctest As Boolean, return1 As Integer, rowcount As Integer, column As Integer, count As Integer
    Dim compfile As Workbook, tcol As Integer, trow As Integer, filename As String


    Set compfile = Workbooks.Open("C:\Users\lthuynh\Documents\MH\Lab Data Collection_ final_aag2.xls")


    count = 0
    rowcount = 3
    column = 2
    tcol = 1
    trow = 1
    ctest = True
    vtest = True

    return1 = MsgBox("Update the spreadsheet?", vbOKCancel, "Updating the spreadsheet")
    If return1 = 1 Then
        While vtest
            If rowcount < 2106 Then

                While ctest

                    If Sheet1.Cells(rowcount, column).Value = compfile.Worksheets("Lab Result-Order Code").Cells(trow, tcol).Value Then
                        Sheet1.Cells(rowcount, 4) = compfile.Worksheets("Lab Result-Order Code").Cells(trow, 5).Value
                        ctest = False
                    ElseIf rowcount > 3093 Then
                        ctest = False
                    Else
                        trow = trow + 1
                    End If
                Wend
                rowcount = rowcount + 1
            Else
                vtest = False
            End If
        Wend
    End If

End Sub













Hope that helps

Roy


All times are GMT -4. The time now is 09:16 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.