Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

type mismatch in loop

Posted on 2013-01-20
11
Medium Priority
?
310 Views
Last Modified: 2013-01-20
Hi,
I get a type mismatch error and I have changed to variable types so many times I can't remember where I started! Hopefully someone can point me in the right direction.

The code in question is attached

I have I as String and K as Long. The code stops at line 11.
Maybe it's just bad syntax in the loop. I'm completely lost.
Any help as always is greatly appreciated.

ps
I should probably explain what I'm trying to do.
I want the code to look at the value of A1 in wsht1 ("I") and find that same value in column A of wsht3 ("K"), then paste the range values of wsht1 (C1:AA1) to the corresponding "K" row but offset two columns right (Column C in effect)

I hope I have explained it clear enough.

Set wbk1 = ActiveWorkbook
    Set wsht1 = wbk1.Sheets("Sheet1")
    Set wbk3 = Workbooks.Open(wbk3path) 'Interface workbook
    Set wsht3 = wbk3.Sheets(1)
    wsht3.Range("A1:AA2").Formula = wsht1.Range("A1:AA2").Value
    wsht1.Range("A1").EntireRow.Delete
    wsht1.Range("A1").EntireRow.Delete
Do
    I = wsht1.Range("A1")
    wsht1.Range("C1:AA1").Copy
    K = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
    wsht3.Range(K).Offset(0, 2).PasteSpecial xlPasteValues
    wsht1.Range("A1").EntireRow.Delete
Loop Until I = ""

Open in new window

0
Comment
Question by:Stephen Byrom
[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
  • 7
  • 2
  • 2
11 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 38798639
> K = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
the Find returns a Range object, normally, so if that returns nothing, your error is normal.

try to do this:
Dim findK as object
findK = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
if findK is not Nothing then
  K = findK.Value
end if 

Open in new window

0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 38798659
thanks for the input.
I get an error "Invalid use of object" when I run it though, and the "Nothing" is highlighted
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38798715
sorry:

if not ( findK is Nothing ) then
0
Independent Software Vendors: 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!

 
LVL 1

Author Comment

by:Stephen Byrom
ID: 38798833
Thanks for persevering.
I now get the error "Object variable or With block variable not set"
The code is now
Do
    I = wsht1.Range("A1")
    wsht1.Range("C1:AA1").Copy
    Dim findK As Object
    findK = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
        If Not (findK Is Nothing) Then
            K = findK.Value
            Range(K).Offset(0, 2).PasteSpecial xlPasteValues
            wsht1.Range("A1").EntireRow.Delete
        End If
Loop Until I = ""
0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 1000 total points
ID: 38798844
set findK = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 38798867
Thanks for the input ssaqibh, but i still get the error and I've changed the code to this;
Do
    I = wsht1.Range("A1")
    wsht1.Range("C1:AA1").Copy
    Dim findK As Object
    Set findK = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
        If Not (findK Is Nothing) Then
            K = findK
            K.Offset(0, 2).PasteSpecial xlPasteValues
            wsht1.Range("A1").EntireRow.Delete
        End If
Loop Until I = ""
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 38798909
Tried this also;
Do
    I = wsht1.Range("A1")
    wsht1.Range("C1:AA1").Copy
    Dim findK As Object
    Set findK = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
        If Not (findK Is Nothing) Then
            K = findK
            wsht3.Range(K).Offset(0, 2).PasteSpecial xlPasteValues
            wsht1.Range("A1").EntireRow.Delete
        End If
Loop Until I = ""

Still get the "Object variable or With block variable not set" error
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38798944
Ok Try this

Do
    I = wsht1.Range("A1")
    wsht1.Range("C1:AA1").Copy
    Dim findK As Object
    Set findK = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
        If Not (findK Is Nothing) Then
            K = findK
            wsht3.Range(findK.Address).Offset(0, 2).PasteSpecial xlPasteValues
            wsht1.Range("A1").EntireRow.Delete
        End If
Loop Until I = ""
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 38798967
managed it!!
new code is this;
Do
    I = wsht1.Range("A1")
    wsht1.Range("C1:AA1").Copy
    Dim findK As Object
    Set findK = wsht3.Columns("A:A").Find(What:=I, lookat:=xlWhole)
        If Not (findK Is Nothing) Then
            findK.Offset(0, 2).PasteSpecial xlPasteValues
        End If
    wsht1.Range("A1").EntireRow.Delete
Loop Until I = ""
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 38798974
I've requested that this question be closed as follows:

Accepted answer: 0 points for olorin57's comment #a38798967
Assisted answer: 250 points for angelIII's comment #a38798639
Assisted answer: 250 points for ssaqibh's comment #a38798844

for the following reason:

Had to split the points. Thanks to you both for your advice and pointers.
0
 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 38798975
Had to split the points
Thanks to you both
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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

705 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