简体   繁体   中英

Object Required: VBA Excel, Range selection

So I am getting an error in this line of code in this particular selection structure I am trying to formulate. I am trying to dynamically select a range based off of the selection of Demand and a user entry that gets read into UB, However I keep getting the object required error and am sure what is wrong (I know my code is a bit redundant at a certain part, I was getting an another error code about range and left some of the redundancies for now!)

   Dim y As Integer
   UB = TextBox1.Text
   y = UB + 4
   srange = "C" & 4 & ":" & "J" & y

   If ComboBox2.Text = "Demand" Then
    Worksheets("SensitivityDemand").Visible = True
    Worksheets("Welcome").Visible = False
    Worksheets("SensitivityDemand").Activate
    ActiveSheet.Range("A1").Select
    Sheets("SensitivityDemand").Select

    With ActiveSheets
        .Range("srange").Select          'Object required here 
        Application.CutCopyMode = False
    'Application.CutCopyMode = False
        Selection.Table RowInput:=Range("B3"), ColumnInput:=Range("B4")

Can you try this below code and let me know if there is any thing missing.

Dim y As Integer
UB = TextBox1.Text
y = UB + 4
srange = "C" & 4 & ":" & "J" & y

If ComboBox2.Text = "Demand" Then
Worksheets("SensitivityDemand").Visible = True
Worksheets("Welcome").Visible = False
Worksheets("SensitivityDemand").Activate
ActiveSheet.Range("A1").Select
'Sheets("SensitivityDemand").Select


    ActiveSheet.Range(srange).Select          'Object required here 
    Application.CutCopyMode = False
'Application.CutCopyMode = False
    Selection.Table RowInput:=Range("B3"), ColumnInput:=Range("B4")

Do no use the ActiveSheets Also srange do not put those in double qoutes as it has some values passed to it. So if you put srange in double qoutes it will treat as a string not a value.

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