[英]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).
我有填充数据的代码,但是我想创建一个按钮来调用userform以便用户填写范围,以便代码在用户提供的范围内工作(因为数据由用户自己调整)。 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. 我希望用户在userform中输入范围(E4:E15)。 How to do the userform if its dependent only range?
如果只依赖于范围,如何执行userform? Thanks stackoverflow.com.
谢谢stackoverflow.com。
Put a text box in userform and name it txtRng
. 在userform中放置一个文本框并将其命名为
txtRng
。 Then declare a variable named MyRng
as String
. 然后将名为
MyRng
的变量声明为String
。 Assign that text box value to MyRng
variable and use this variable as argument of range like... 将该文本框值分配给
MyRng
变量,并将此变量用作范围的参数,如...
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. 我还建议你不要使用
range
, value
作为变量,因为其中一些保留关键字。 It may cause misbehave of output. 它可能会导致输出错误。
You could use the InputBox()
method and have the user select a range: 您可以使用
InputBox()
方法并让用户选择范围:
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 或者您可以将
RefEdit
控件添加到您的用户窗体并进行处理
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) 在这种情况下,由于用户可以输入无效范围,您可能需要添加验证函数(在以下示例中名为
GetRange
)
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)
要向用户询问范围,可以使用
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 如果选择了整个列(A:A)或多列,则会调整为UsedRange中的总行数和选择中的第一列
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 注意:建议不要使用特殊的VBA关键字命名变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.