从TextBox \ Combobox到Range的值

时间:2011-09-25 19:02:58

标签: excel vba

UserForm1.TextBox1.Value = "G4"

Set CodeRange = Range(UserForm1.TextBox1.Value, Cells(Rows.Count, "G").End(xlUp))

给予:methog range of object _global failed

请帮助我们如何将值从表单转换为范围。

2 个答案:

答案 0 :(得分:2)

你的代码对我来说没问题,虽然有点不正统。您是否将CodeRange标注为范围?

如果activehseet是图表工作表,您将收到该错误。

您还应该使用工作表或活动表完全限定您的参考资料。

With WorkSheeets("Sheet1")
    Set CodeRange = .Range(UserForm1.TextBox1.Value, .Cells(Rows.Count, "G").End(xlUp))
End With

如果您的代码驻留在Userform模块中,那么您可以使用Me关键字而不是Userfrom1。

我更喜欢使用像这样的输入框来获取用户的范围:

Sub HTH()

    Dim rRange As Range

    On Error Resume Next
    rRange = Application.InputBox(Prompt:= _
            "Please select a range with your Mouse to be bolded.", _
            Title:="SPECIFY RANGE", Type:=8)
    On Error GoTo 0

    If Not rRange Is Nothing Then
        '// Your code here
    End If


End Sub

答案 1 :(得分:0)

Decimal sphvalue = Decimal.Parse(cmbo_sph.SelectedValue.ToString());

        txt_sph.Text = sphvalue.ToString();
        Decimal cylvalue = Decimal.Parse(cmbo_cyl.SelectedValue.ToString());
        txt_cyl.Text = cylvalue.ToString();
        try
        {
        SqlConnection searchconnnection = new SqlConnection("DataSource=ANI\\SQLEXPRESS;Initial Catalog=divinei;Integrated Security=True");
            searchconnnection.Open();
            SqlCommand searchcommand = new SqlCommand("SELECT CL_sphValue,CL_CylValue FROM ConvertCharts where SP_sphValue=" + Decimal.Parse(txt_sph.Text) + " and Sp_CylValue=" + Decimal.Parse(txt_cyl.Text) + " ", searchconnnection);
            searchcommand.ExecuteNonQuery();
            SqlDataReader convertreader = searchcommand.ExecuteReader();
            while (convertreader.Read())
            {
                sphtxt.Text = convertreader[0].ToString();
                cyltxt.Text = convertreader[1].ToString();
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }`enter code here`