Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 6th, 2007, 10:37 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Aprile
Default 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
Reply With Quote
  #2 (permalink)  
Old September 6th, 2007, 11:42 PM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

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)
Reply With Quote
  #3 (permalink)  
Old September 7th, 2007, 06:16 AM
Registered User
 
Join Date: Sep 2007
Location: Mablethorpe, Lincolnshire, United Kingdom.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transferring files sachtaridis Visual C++ 0 November 22nd, 2007 07:31 AM
Transferring Dlls from one server to another Pallavs20 General .NET 0 October 13th, 2005 02:13 AM
Transferring a variable value losmon HTML Code Clinic 4 July 19th, 2005 04:03 AM
Transferring data between functions Fightfish C# 2 July 6th, 2005 12:21 PM
Transferring Import Specifications timmaher Access 4 September 1st, 2003 10:58 AM



All times are GMT -4. The time now is 08:51 PM.


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