Use of Array in Macro

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, I have a Macro which works great, but I need to add the letters "S" and "L". I'm pretty sure it will require an array. Looked for possible example and could not come accross one.

The current macro works greaat moving the A, but I need it to move an S and L too if found in Column R. These letters will be by themselves in Column R.

VBA Code:
Public Sub MoveLetters()
    Dim cell As Range
        Application.ScreenUpdating = False
        For Each cell In Range("R1:R" & Cells(Rows.Count, "R").End(xlUp).Row)
        If InStr(cell.Text, "A") > 0 Then
            cell.Offset(0, 1) = cell.Text
            cell.ClearContents
        End If
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub


Thank you,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
how about
VBA Code:
For Each cell In Range("R1:R" & Cells(Rows.Count, "R").End(xlUp).Row)
    Select Case cell.Text
       Case "R","S","L"
         cell.Offset(0, 1) = cell.Text
         cell.ClearContents
        Case Else ' what to do if not R,S or L?
          'If nothing, can eliminate these two lines
       End Select
 Next cell
I'm assuming your cell contains the single letter. If not, then you're using Instr to find "A" in Apple?
 
Upvote 0
Solution
Using an array, which would eliminate going back and forth to the worksheet within your loop, you could try this...

VBA Code:
Public Sub MoveLetters()
    Dim arr, i As Long
        Application.ScreenUpdating = False
        arr = Range("R1:S" & Cells(Rows.Count, "R").End(xlUp).Row)
        For i = 1 To UBound(arr)
            If InStr(arr(i, 1), "A") > 0 Then arr(i, 2) = "A"
            If InStr(arr(i, 1), "S") > 0 Then arr(i, 2) = "S"
            If InStr(arr(i, 1), "L") > 0 Then arr(i, 2) = "L"
            If Not arr(i, 2) = "" Then arr(i, 1) = ""
        Next
        Range("R1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Oops I used "A" instead of "R", so a correction and using a Case Statement which would be faster than using a For/Next loop should be the fastest...

VBA Code:
Public Sub MoveLetters()
    Dim arr, i As Long
    arr = Range("R1:S" & Cells(Rows.Count, "R").End(xlUp).Row)
    For i = 1 To UBound(arr)
        Select Case arr(i, 1)
            Case "R", "S", "L"
                arr(i, 2) = arr(i, 1)
                arr(i, 1) = ""
        End Select
    Next
    Range("R1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End Sub
 
Upvote 0
Nothing good ever happens when you rush...

VBA Code:
Public Sub MoveLetters()
    Dim arr, i As Long
    arr = Range("R1:S" & Cells(Rows.Count, "R").End(xlUp).Row)
    For i = 1 To UBound(arr)
        Select Case arr(i, 1)
            Case "A", "S", "L"
                arr(i, 2) = arr(i, 1)
                arr(i, 1) = ""
        End Select
    Next
    Range("R1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End Sub
 
Upvote 0
Won't that miss first array member because arrays are 0 based?
No it wont because when you load an array direct from a sheet it will be 1 based, regardless of settings.
 
Upvote 0
how about
VBA Code:
For Each cell In Range("R1:R" & Cells(Rows.Count, "R").End(xlUp).Row)
    Select Case cell.Text
       Case "R","S","L"
         cell.Offset(0, 1) = cell.Text
         cell.ClearContents
        Case Else ' what to do if not R,S or L?
          'If nothing, can eliminate these two lines
       End Select
 Next cell
I'm assuming your cell contains the single letter. If not, then you're using Instr to find "A" in Apple?
So many choices in the end I elected to go with yours. I have the others too if something goes wrong in the future. Many thanks!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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