Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'m starting to practice writing some VBA code in excel. I took a java c

ID: 3619413 • Letter: H

Question

Hello, I'm starting to practice writing some VBA code in excel.
I took a java class last year but have never used VBA. I created a column in excel and the cells contain a "S", "E", "N", or "W" and the code will change those to South, East, north and west in the next column. The code works, I am just trying to make it more concise. (Do I need to write the whole statemnt as I did to reference each cell?)

This is my code:

Sub Direction()

Dim m As Integer
m = 1

Do While Worksheets("Sheet1").Cells(m, 1) <> "" Or Worksheets("Sheet1").Cells(m, 1) <> 0

If Worksheets("Sheet1").Cells(m, 1) = "S" Then
Worksheets("Sheet1").Cells(m, 2) = "South"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "W" Then
Worksheets("Sheet1").Cells(m, 2) = "West"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "N" Then
Worksheets("Sheet1").Cells(m, 2) = "North"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "E" Then
Worksheets("Sheet1").Cells(m, 2) = "East"
End If

m = m + 1
Loop

End Sub


Thanks!
  • 20 hours ago
Additional Details I think I improved it a little.

My new code is:

Sub ExpandDirection()

Do While ActiveCell.Value <> "" Or ActiveCell.Value <> 0

ActiveCell.Offset(0, 1).Select

If ActiveCell.Previous = "S" Then
ActiveCell.Value = "South"
ElseIf ActiveCell.Previous = "W" Then
ActiveCell.Value = "West"
ElseIf ActiveCell.Previous = "N" Then
ActiveCell.Value = "North"
ElseIf ActiveCell.Previous = "E" Then
ActiveCell.Value = "East"
Else
ActiveCell.Value = "N/A"
End If

ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(0, -1).Select

Loop

End Sub Hello, I'm starting to practice writing some VBA code in excel.
I took a java class last year but have never used VBA. I created a column in excel and the cells contain a "S", "E", "N", or "W" and the code will change those to South, East, north and west in the next column. The code works, I am just trying to make it more concise. (Do I need to write the whole statemnt as I did to reference each cell?)

This is my code:

Sub Direction()

Dim m As Integer
m = 1

Do While Worksheets("Sheet1").Cells(m, 1) <> "" Or Worksheets("Sheet1").Cells(m, 1) <> 0

If Worksheets("Sheet1").Cells(m, 1) = "S" Then
Worksheets("Sheet1").Cells(m, 2) = "South"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "W" Then
Worksheets("Sheet1").Cells(m, 2) = "West"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "N" Then
Worksheets("Sheet1").Cells(m, 2) = "North"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "E" Then
Worksheets("Sheet1").Cells(m, 2) = "East"
End If

m = m + 1
Loop

End Sub


Thanks!
  • 20 hours ago
Additional Details I think I improved it a little.

My new code is:

Sub ExpandDirection()

Do While ActiveCell.Value <> "" Or ActiveCell.Value <> 0

ActiveCell.Offset(0, 1).Select

If ActiveCell.Previous = "S" Then
ActiveCell.Value = "South"
ElseIf ActiveCell.Previous = "W" Then
ActiveCell.Value = "West"
ElseIf ActiveCell.Previous = "N" Then
ActiveCell.Value = "North"
ElseIf ActiveCell.Previous = "E" Then
ActiveCell.Value = "East"
Else
ActiveCell.Value = "N/A"
End If

ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(0, -1).Select

Loop

End Sub Hello, I'm starting to practice writing some VBA code in excel.
I took a java class last year but have never used VBA. I created a column in excel and the cells contain a "S", "E", "N", or "W" and the code will change those to South, East, north and west in the next column. The code works, I am just trying to make it more concise. (Do I need to write the whole statemnt as I did to reference each cell?)

This is my code:

Sub Direction()

Dim m As Integer
m = 1

Do While Worksheets("Sheet1").Cells(m, 1) <> "" Or Worksheets("Sheet1").Cells(m, 1) <> 0

If Worksheets("Sheet1").Cells(m, 1) = "S" Then
Worksheets("Sheet1").Cells(m, 2) = "South"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "W" Then
Worksheets("Sheet1").Cells(m, 2) = "West"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "N" Then
Worksheets("Sheet1").Cells(m, 2) = "North"
ElseIf Worksheets("Sheet1").Cells(m, 1) = "E" Then
Worksheets("Sheet1").Cells(m, 2) = "East"
End If

m = m + 1
Loop

End Sub


Thanks! I think I improved it a little.

My new code is:

Sub ExpandDirection()

Do While ActiveCell.Value <> "" Or ActiveCell.Value <> 0

ActiveCell.Offset(0, 1).Select

If ActiveCell.Previous = "S" Then
ActiveCell.Value = "South"
ElseIf ActiveCell.Previous = "W" Then
ActiveCell.Value = "West"
ElseIf ActiveCell.Previous = "N" Then
ActiveCell.Value = "North"
ElseIf ActiveCell.Previous = "E" Then
ActiveCell.Value = "East"
Else
ActiveCell.Value = "N/A"
End If

ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(0, -1).Select

Loop

End Sub

Explanation / Answer

You could try a different kind of loop, like this:

Sub Expand_Direction()
Dim i, LastRow
LastRow = Range("M" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
Select Case UCase(Cells(i, "M"))
Case Is = "S"
Cells(i, "M").Offset(0, 1).Value = "South"
Case Is = "N"
Cells(i, "M").Offset(0, 1).Value = "North"
Case Is = "E"
Cells(i, "M").Offset(0, 1).Value = "East"
Case Is = "W"
Cells(i, "M").Offset(0, 1).Value = "West"
End Select
Next
End Sub
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote