从TextBox \ Combobox到范围的值

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

给出: methog range of object _global failed

请帮助如何将值从forms转换为范围。

你的代码对我来说看起来不错,虽然有点不正规。 您是否将CodeRange设置为范围?

如果activehseet是一个图表,你会得到这个错误。

你也应该完全限定你的参考工作表或activesheet。

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

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

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

 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 

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`