Using .Left with specific worksheet

l_eonandr

New Member
Joined
Feb 18, 2016
Messages
25
Please help.
I know this should be easy but having trouble getting this line to work;

P1 = Worksheets("Past-here").Range("B" & i).Left(cell("B" & i), 2)

I get a "type mismatch" error.

I need to check the data in a worksheet for several types of data, such as 100, 109, 400, 300 etc

I need to copy all the 100's to a sheet and all the 400's to a different sheet etc

I am using the .left to do that group selection. (note the data may be alphanumeric such as 40F)

Please help me with the correct syntax of this line of code.
I have tried several variations.
What am I missing here??

Much appreciate any help.

This is a larger picture of my part of my code;

Sub Bcopy() 'hot key is cntl + e


SrcLR = Application.CountA(Sheets("Past-here").Range("A:N"))
DestLR1 = Application.CountA(Sheets("Oil").Range("A:A"), -1)
DestLR2 = Application.CountA(Sheets("200").Range("A:A"), -1)
DestLR3 = Application.CountA(Sheets("300").Range("A:A"), -1)
DestLR4 = Application.CountA(Sheets("400").Range("A:A"), -1)



For i = 1 To SrcLR

P1 = Worksheets("Past-here").Range("B" & i).Left(cell("B" & i), 2)
'P = .Left(P1, 2)

If P = "10" Then
DestLR1 = DestLR1 + 1
Sheets("Past-here").Cells(i, 1).Cells.Copy Sheets("Oil").Cells(DestLR1, 1)
Sheets("Past-here").Cells(i, 2).Cells.Copy Sheets("Oil").Cells(DestLR1, 2)
Sheets("Past-here").Cells(i, 10).Cells.Copy Sheets("Oil").Cells(DestLR1, 3)
ElseIf P = "20" Then
DestLR1 = DestLR1 + 1
Sheets("Past-here").Cells(i, 1).Cells.Copy Sheets("200").Cells(DestLR2, 1)
Sheets("Past-here").Cells(i, 2).Cells.Copy Sheets("200").Cells(DestLR2, 2)
Sheets("Past-here").Cells(i, 10).Cells.Copy Sheets("200").Cells(DestLR2, 3)
ElseIf P = "30" Then
DestLR1 = DestLR1 + 1
Sheets("Past-here").Cells(i, 1).Cells.Copy Sheets("300").Cells(DestLR3, 1)
Sheets("Past-here").Cells(i, 2).Cells.Copy Sheets("300").Cells(DestLR3, 2)
Sheets("Past-here").Cells(i, 10).Cells.Copy Sheets("300").Cells(DestLR3, 3)
ElseIf P = "40" Then
DestLR1 = DestLR1 + 1
Sheets("Past-here").Cells(i, 1).Cells.Copy Sheets("400").Cells(DestLR4, 1)
Sheets("Past-here").Cells(i, 2).Cells.Copy Sheets("400").Cells(DestLR4, 2)
Sheets("Past-here").Cells(i, 10).Cells.Copy Sheets("400").Cells(DestLR4, 3)



End If
Next i
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Are you trying to read left 2 characters of the cells. If so then you need to use Left function of VBScript, not Range.Left property. Range.Left is a property which returns distance in points from left of column A to left of the Range. It does not give you leftmost characters.

Code:
P1 = Left(Worksheets("Past-here").Range("B" & i).value, 2)
 
Upvote 0
Thanks, that works, I thought I had tried that way back in the beginning but got errors, must have been something else wrong back then and I got confused about where I was at in the versions of the code.
Much appreciate your reply.
And to answer your question, yes I am trying to read the first 2 characters of the cells to determine where the data should go.
I still have other problems as well with my code but now that this line is working I can continue to troubleshoot the remainder of the application.
again, much thanks...
 
Upvote 0

Forum statistics

Threads
1,215,655
Messages
6,126,050
Members
449,283
Latest member
GeisonGDC

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top