简体   繁体   中英

Looking for the last row and have it fill the other column

I am newbie in here in Stackoverflow and in the VBA Field. Actually I need some help with my code.

I have created a macro and it seems there is something missing in my code.

Scenario :

  1. If A1 = 0.5 then in column B1 should answered FLAT
  2. If A2 = 2 then in column B2 should answered as PER
  3. If A3 = 5 then in column B3 should answered as PER
  4. If A4 = 0.1 then in column B4 should answered FLAT
  5. If A5 = 0.2 then in column B5 should answered FLAT

The code that I have only looking for one cell. What I want is in column B my code will apply until the last row of column "A" that has data

Sub exe()
    Dim number As Integer, result As String

    number = Range("a1").Value

    If number <= 1 Then
        result = "Flat"
    Else
        result = “Per”
    End If

    'enter code here
    Range("b1").Value = result
End Sub

I would recommned using a formula

=IF(TRIM(A1)="","",IF(OR(A1=0.1,A1=0.2,A1=0.5),"FLAT",IF(OR(A1=2,A1=5),"PER","")))

But since you mentioned in comments

so if this is <= 1 then the answer is FLAT so if this is greater than 1 then the answer is PER – Kate Sayson 16 mins ago

So the above will change to

=IF(TRIM(A1)="","",IF(A1<1,"FLAT",IF(A1>=1,"PER","")))

Other alternative is to use the worksheet change event. This will only act when a value in Column A changes and will only update Column B. So your rest of the worksheet is safe.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim aCell As Range

    On Error GoTo Whoa

    Application.EnableEvents = False

    If Not Intersect(Target, Columns(1)) Is Nothing Then
        For Each aCell In Target
            If IsNumeric(aCell.Value2) And _
               Len(Trim(aCell.Value2)) <> 0 And _
               aCell.Column = 1 Then
                Select Case aCell.Value2
                    Case Is < 1: Range("B" & aCell.Row).Value = "FLAT"
                    Case Else: Range("B" & aCell.Row).Value = "PER"
                End Select
            End If
        Next
    End If

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

If you still want a macro then try this

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim i As Long

    '~~> Change this to the relevant sheet
    Set ws = Sheet1

    With ws
        '~~> Find last row in Col A
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Loop through relevant cells in Col A
        For i = 1 To lRow
            If IsNumeric(.Range("A" & i).Value2) And _
               Len(Trim(.Range("A" & i).Value2)) <> 0 Then
                Select Case .Range("A" & i).Value2
                    Case Is < 1: Range("B" & i).Value = "FLAT"
                    Case Else: Range("B" & i).Value = "PER"
                End Select
            End If
        Next i
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM