Вставить строки на основе пользовательского ввода (значение текстового поля) - PullRequest
0 голосов
/ 09 июля 2019

Я хочу вставить строки в таблицу, используя пользовательский ввод (текстовое поле) вместе с формулой и порядковыми номерами последовательных строк

Я попробовал ниже упомянутый код

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

каждый раз, когда я нажимаю кнопку, он вставляет одну строку вместо ввода пользователя

Скриншот

1 Ответ

0 голосов
/ 09 июля 2019

Хотя я не совсем понимаю, что вы хотите сделать, я думаю, что приведенный ниже код поможет.

Пожалуйста, настройте его в соответствии с вашими потребностями.

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

Надеюсь, это поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...