我想使用输入框,例如选择G:F或K:K ......看看我的代码:

时间:2017-05-05 12:24:45

标签: excel-vba vba excel

我想在LR来临时弹出一个输入框.. 查看代码:

LR = Range("G" & Rows.Count).End(xlUp).Row Range("G2:G" & LR).Select

Sub FixIt()

Dim LR As Long
    LR = Range("G" & Rows.Count).End(xlUp).Row
    Range("G2:G" & LR).Select

Selection.Replace What:=Chr(160), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Selection.NumberFormat = "0.00"
Selection.Style = "Comma"

End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用Application.InputBoxType:=8来指定一个输入框,该输入框的返回值为Range个对象。

以下是使用此类输入框获取所选单元格的编号的示例。

Dim inRange as Range
Set inRange = Application.InputBox("Please select a cell...", Type:=8)
If Not inRange Is Nothing Then
    LR = inRange.Row
Else
    'Probably you want to Exit Sub here or do some error-handling
End If

或者,使用相同的方法来获得整个选择范围:

Dim myRange as Range
Set myRange = Application.InputBox("Please select some range...", Type:=8)
If myRange Is Nothing Then
    'Probably you want to Exit Sub here or do some error-handling
End If
'proceed with the rest of your code...

With myRange
    .Replace What:=Chr(160), Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    .Replace What:=",", Replacement:=".", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    .NumberFormat = "0.00" Selection.Style = "Comma"
End With