• 0

Help with EXCEL VBA


Question

I'm trying to do a simple thing but with me not touching VB in last 10 years I cant figure out how :(

 

What i'm trying to do is if column B = "plane" and column A numbers are from 1 to 100, then it would spit out in column C = "yes" and if numbers from 100 to 300, then it would spit out "no" in each cell.  There will be bunch of more other data above and below  but i need just to make sure everything that has "plane" in B and number in A next to it will get converted.  Feel stupid to ask because it supposed to be a simple thing for me but can't figure it out anymore. 

 

 

zzzzz	zzzzz
zzzzz	zzzzz
zzzzz	zzzzz
zzzzz	zzzzz
216	plane
217	plane
12	plane
12	plane
221	plane
20	plane
226	plane
223	plane
3	plane
8	plane
25	plane
zzzzz	zzzzz
zzzzz	zzzzz
zzzzz	zzzzz
zzzzz	zzzzz
zzzzz	zzzzz
zzzzz	zzzzz

 

Link to comment
https://www.neowin.net/forum/topic/1282272-help-with-excel-vba/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

 

This should work...

Sub isPlane()

Dim colA As Integer
Dim colB As String
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Integer

Set ws = ThisWorkbook.Worksheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A1:A" & lastRow)

For Each cell In rng
	'assuming colA will always be an integer value
    colA = cell.Value
    colB = ws.Cells(cell.Row, 2)
    
    If colB = "plane" And (colA <= 100 And colA >= 1) Then
        ws.Cells(cell.Row, 3) = "yes"
    ElseIf colB = "plane" And (colA <= 300 And colA >= 100) Then
        ws.Cells(cell.Row, 3) = "no"
    End If
Next

End Sub

edit

Just read through the spec again - column a can be string or numbers. Will need to add a check to see if you can convert cell into integer.

  • 0
Sub isPlane()

Dim colA As Long
Dim colB As String
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Integer

Set ws = ThisWorkbook.Worksheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A1:A" & lastRow)

For Each cell In rng
    If IsNumeric(cell.Value) Then
        colA = cell.Value
    Else
        GoTo nextIteration
    End If
    colB = ws.Cells(cell.Row, 2)
    
    If colB = "plane" And (colA <= 100 And colA >= 1) Then
        ws.Cells(cell.Row, 3) = "yes"
    ElseIf colB = "plane" And (colA <= 300 And colA >= 100) Then
        ws.Cells(cell.Row, 3) = "no"
    End If
nextIteration:
Next

End Sub

 

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.