简体   繁体   中英

Excel-VBA: Inputting range in userform call

I have growing data files and in a certain column is not fill out. I have the code to fill the data, though I want to make a button to call userform for user to fill in the range so that the code works within the range provided by the user(since data is adjusted by the user itself). The code to fill the blank cell is;

Sub fillblank()
Dim range As Range
Dim cell As Range
Dim value As String
Set range = Sheets("Sheet1").Range("E4:E15")
For Each cell In range
 If Trim(cell.Value) <> "" Then
   value = cell.Value
 Else
   cell.Value = value
 End if
Next cell
End Sub

I would like the user to enter the range (E4:E15) in userform. How to do the userform if its dependent only range? Thanks stackoverflow.com.

Put a text box in userform and name it txtRng . Then declare a variable named MyRng as String . Assign that text box value to MyRng variable and use this variable as argument of range like...

Set range = Sheets("Sheet1").Range(MyRng)

So, full code will be like as below

Sub fillblank()
Dim range As range
Dim cell As range
Dim value As String
Dim MyRng As String

    MyRng = UserForm1.txtRng 'Use your form name here
    Set range = Sheets("Sheet1").range(MyRng)

    For Each cell In range
     If Trim(cell.value) <> "" Then
       value = cell.value
     Else
       cell.value = value
     End If
    Next cell

End Sub

I also suggest you to not use range , value as variable because some of these reserve keyword. It may cause misbehave of output.

You could use the InputBox() method and have the user select a range:

Sub fillblank()
    Dim myRange As Range
    Dim cell As Range
    Dim myValue As String

    Set myRange = Application.InputBox( prompt:="Select a range", Type:=8) 
    For Each cell In myRange
        If Trim(cell.Value) <> "" Then
            myValue = cell.Value
        Else
            cell.Value = myValue
        End if
    Next
End Sub

or you could add a RefEdit control to your userform and process it

Sub fillblank()
    Dim cell As range
    Dim myValue As String
    For Each cell In range(Me.RefEdit1.Text)
        If Trim(cell.value) <> "" Then
            myValue = cell.value
        Else
            cell.value = myValue
        End If
    Next cell
End Sub

In this case, since the user could input an invalid range, you may want to add a validation function (named GetRange in my following example)

Sub fillblank()
    Dim myRange As range
    If Not GetRange(Me.RefEdit1.Text, myRange) Then
        MsgBox "Select a valid range "
        Me.RefEdit1.SetFocus
        Exit Sub
    End If

    Dim cell As range
    Dim myValue As String
    For Each cell In myRange
        If Trim(cell.value) <> "" Then
            myValue = cell.value
        Else
            cell.value = myValue
        End If
    Next cell
End Sub

Function GetRange(RefEditText As String, myRange As range) As Boolean
    On Error Resume Next
    Set myRange = range(RefEditText)
    On Error GoTo 0
    GetRange = Not myRange Is Nothing
End Function

finally, here is an alternative method (no loops) to fill blank cells as you're wanting to do:

Sub fillblank()
    With range(Me.RefEdit1.Text).SpecialCells(xlCellTypeBlanks)
        .FormulaR1C1 = "=R[-1]C"
        .value = .value
    End With
End Sub

To ask the user for a range you could use InputBox(Type:=8)

The code bellow will accept only one column

If an entire column is selected (A:A) or multiple columns it adjusts to the total rows in UsedRange and first column in selection


Option Explicit

Public Sub FillBlanks()
  Dim selectedCol As Range, itm As Range, lr As Long

  On Error Resume Next
    Set selectedCol = Application.InputBox(Prompt:="Select column:", Type:=8)
  On Error GoTo 0

  If selectedCol Is Nothing Then Exit Sub   'User cancelled selection

  With selectedCol
   .Parent.Activate
   lr = .Parent.UsedRange.Rows.Count
   If .Columns.Count > 1 Then Set selectedCol = .Resize(.Rows.Count, 1)
   If .Rows.Count < 2 Or .Rows.Count > lr Then Set selectedCol = .Resize(lr - .Row + 1, 1)
   selectedCol.Select
  End With

  For Each itm In selectedCol
    If Len(Trim$(itm)) = 0 And itm.Row > 1 Then itm.Value2 = itm.Offset(-1).Value2
  Next
End Sub

Note: It's not recommended to name your variables with special VBA keywords

  • Dim range As Range - Range is the most important object in Excel
  • Dim value As String - Value is the most important property of the Range object

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