0

I have a code in VB:

srcClm2 = Sheet45.Range("B2:B" & lastRowSrc)

But I wanna choose sheet by name. I try the different ways, for example:

Sheets("Cycle").Range("A2:A" & lastRowSrc)

But I have got error: "Type mismatch."

EDIT: This is fragment of my code:

Dim srcClm1(), srcClm2()
Dim lastRowSrc As Long

lastRowSrc = Sheets("Line_Cycle_working_sheet").Cells(Rows.Count, 1).End(xlUp).Row


srcClm1 = Sheets("Line_Cycle_working_sheet").Range("A2:A" & lastRowSrc)

srcClm2 = Sheets("Line_Cycle_working_sheet").Range("B2:B" & lastRowSrc)
8
  • 1
    Ok, post the whole code. Or let me confirm, Is this line shown error. I don't think so. Because Sheets("Cycle").Range("A2:A" & lastRowSrc) is right.
    – R.Katnaan
    Commented Aug 12, 2015 at 11:00
  • 1
    Probably wrong name ("Cycle")
    – Amit
    Commented Aug 12, 2015 at 11:01
  • 1
    As Nicolas stated, Sheets("Cycle").Range("A2:A" & lastRowSrc) is correct as long as 1. your active workbook contains a sheet "Cycle", and 2. lastRowSrc is a number >0. Also, don't forget Set in Set srcClm2 = ... Commented Aug 12, 2015 at 11:02
  • 1
    If you want to get an array of Values, your code is O.K. If you want to define a range, then you need Set Commented Aug 12, 2015 at 11:02
  • @Robert, rather then posting code in comment, please improve your question.
    – Maciej Los
    Commented Aug 12, 2015 at 11:07

1 Answer 1

2

You need to declare variables srcClm1, srcClm2 as Variant.

Dim srcClm1 As Variant, srcClm2 As Variant
Dim lastRowSrc As Long

lastRowSrc = Sheets("Line_Cycle_working_sheet").Cells(Rows.Count, 1).End(xlUp).row
srcClm1 = Sheets("Line_Cycle_working_sheet").Range("A2:A" & lastRowSrc)
srcClm2 = Sheets("Line_Cycle_working_sheet").Range("B2:B" & lastRowSrc)

Actually you could skip As Variant and declare it just like this:

Dim srcClm1, srcClm2

since Variant type is default. However, it is good practice to add As Variant to make it clear that you declare it as Variant by purpose and not by mistake.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.