Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel aba variable range help

Status
Not open for further replies.

DJ2018

Technical User
May 14, 2018
13
GB
Hi Guys,

I need to remove blanks from a variable range e4:g - variable ive managed to replace the blanks but only on a fixed range.
here is what I have.
Code:
Private Sub cmdClean_Click()
    Const sBad      As String = "`!@#$;^()_-=+{}&[]\|;:'"",.<>/?"
    Dim cell        As Range
    Dim s           As String
    Dim i           As Long

    Application.ScreenUpdating = False
    For Each cell In Range("E4:G50")
        If VarType(cell.Value) = vbString Then
            s = cell.Value

            For i = 1 To Len(sBad)
                s = Replace(s, Mid(sBad, i, 1), " ")
            Next i
            
            cell.Value = Application.Trim(s)
        End If
    Next cell
    
    
    For Each c In Worksheets("Retso").Range("e4:G15")
        If c.Value = "" Then
            c.Value = "TEST"
        End If
    Next c
              
    Application.ScreenUpdating = True
End Sub
 
You can refer via Cells and use row/column variables or build range string:
Code:
Dim rng As Range, strRng
Set rng = Range(Cells(2, 2), Cells(5, 5))
MsgBox rng.Address
' or
strRng = "b2:e" & 5
Set rng = Range(strRng)
MsgBox rng.Address
Refering to worksheet too, using cells:
Code:
Dim rng As Range
With Worksheets("Retso")
    Set rng = Range(.Cells(2, 2), .Cells(5, 5))
End With

combo
 
Hi,
Code:
    Dim rng As Range
    
    With ActiveSheet
        Set rng = Range(.Cells(4, 5), .Cells(.UsedRange.Rows.Count + .UsedRange.Row - 1, 7))
    
        For Each cell In rng
'...
        Next Cell
    End With

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top