根据用户输入(文本框值)插入行

时间:2019-07-09 05:27:18

标签: excel vba

我想使用用户输入(文本框)以及公式和递增的行序列号在表中插入行

我尝试了下面提到的代码

Thisworkbook.sheets("Sheet1").visible = true
Thisworkbook.sheets("Sheet1").select
Sheet1.activate
Sheets("Sheet1").Rows(9+1).Entirerow.insertshift := xldown
Sheets("Sheet1").Rows(9).resize(Cint(textbox1.value)).filldown

每次单击按钮时,它都会插入单行而不是用户输入

Screenshot

1 个答案:

答案 0 :(得分:0)

虽然我不清楚您要做什么,但是我认为下面的代码会有所帮助。

请根据您的需要进行调整。

Sub add_some_fancy_rows()
Dim Resizer As Integer
Dim a As Variant
a = InputBox("Please enter the number of rows you want to autofill", "Let me do it for you") 'First we ask for user input

On Error GoTo notvalid  'We add an error handler, so if the user would enter text like "seven", the sub will exit with a message
Resizer = CInt(a)       'we store the input in a variable which has to be an integer, if the user enters text it will couse an error so we jump to the end
If Resizer < 2 Then GoTo notvalid 'We also check if the number is higher than 1, othervise it couses error, or copies the 8th row to the 9th
On Error GoTo 0 'We reset the error handling so we can see if something else goes wrong.

ThisWorkbook.Sheets("Sheet1").Visible = True
ThisWorkbook.Sheets("Sheet1").Select
ThisWorkbook.Sheets("Sheet1").Rows(9 + 1).EntireRow.Insert shift:=xlDown 'add a new row under the 9th row/above the 10th row
ThisWorkbook.Sheets("Sheet1").Rows(9).Resize(Resizer).FillDown
  Exit Sub    'We exit the sub before the error message.
notvalid: 'in case of error we jump here
    MsgBox "Please enter a number which is 2 or higher"
End Sub

我希望对您有帮助

相关问题