Trim and compare text via VBA macro

lkrznchc

New Member
Joined
Feb 28, 2013
Messages
11
I am trying to write a macro that filters a list based on criteria in a particular column and transfers the information to another sheet.

There is a problem with the line
Code:
xmitter_type = Application.Right(Trim(Cells(j + 2, 5)), 1)

within the overall procedure of:
Code:
Dim cell As Range
    Dim num_xmitters As Integer
    Dim xmitter_type As String
    Dim TransmitterArray(1 To 4, 1 To 200) As Range
    
    num_xmitters = ThisWorkbook.Worksheets("Instr List").UsedRange.Rows.Count
    
    Dim j As Integer
    
    For j = 1 To num_xmitters
        xmitter_type = Application.Right(Trim(Cells(j + 2, 5)), 1)
        Select Case xmitter_type
            Case "A"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(1, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
                
            Case "I"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(2, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
            
            Case "O"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(3, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
            
            Case "F"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(4, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
        End Select
        Next j

Is there a better way to extract the last character of these cells and compare on a 4 case scenario?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to the board..

Remove the Application. from it..
VBA has it's own built in RIGHT function

so it should just be
xmitter_type = Right(Trim(Cells(j + 2, 5)), 1)
 
Upvote 0
I realized that I need the reference cells(j+2,5)),1) to reference a particular sheet. I am trying to use a dynamic array to accomplish this but is not working out as I expected.
Code:
 Dim cell As Range
    Dim num_xmitters As Integer
    Dim xmitter_type As String
    Dim TransmitterArray(1 To 4, 1 To 200) As Range
    Dim units() As Integer
    Dim j As Integer
    
    num_xmitters = ThisWorkbook.Worksheets("Instr List").UsedRange.Rows.Count
    units = ThisWorkbook.Worksheets.Range()
    
    For j = 1 To num_xmitters
        ReDim units(j + 2, 5)
        xmitter_type = Right(Trim(units()), 1)
        Select Case xmitter_type
            Case "A"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(1, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
                
            Case "I"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(2, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
            
            Case "O"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(3, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
            
            Case "F"
                ThisWorkbook.Worksheets("Instr Sort").TransmitterArray(4, j).Value = ThisWorkbook.Worksheets("Instr List").Cells(j + 2, 2).Value
        End Select
        Next j
 
Upvote 0
I am getting a mismatch error with the same line I had issues with before.
Code:
xmitter_type = Right(Trim(units()), 1)

I also deleted the definition line with unit()=thisworkbook.worksheets("instr list").range(" ")
how do i get that range to reference that particular column in the workbook?
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

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