VBA: Как запустить 'emptyrow' из определенного столбца и ячейки? - PullRequest
0 голосов
/ 24 июня 2018

В основном я делаю пользовательскую форму и хотел бы, чтобы данные начинались со следующей пустой строки в столбце B, начиная с ячейки B4.

Вот код, полученный из шаблона пользовательской формы, найденного в Интернете:

Private Sub OKButton_Click()

Dim emptyRow As Long

'Make Sheet1 active
Sheet1.Activate

'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 2

'Transfer information
Cells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = PositionTextBox.Value
Cells(emptyRow, 3).Value = EmployeeIDTextBox.Value

Спасибо за помощь.

1 Ответ

0 голосов
/ 24 июня 2018
Private Sub OKButton_Click()


    ' Declare/Set variable for referencing workbook
    Dim wb As Workbook
    Set wb = ThisWorkbook

    ' Declare/Set variable for referencing worksheet
    Dim ws As Worksheet
    Set ws = wb.Worksheets("Sheet1")

    'Determine next empty Row
    Dim emptyRow As Long
    ' Code below works like this:
        ' ws = the worksheet
        ' .Range("b65536") is the last cell in column b
        ' .End(xlUp) means go up from cell b65536 until we hit a non-empty cell
        ' .Row is the row number of that non-empty cell
        ' + 1 to get to the empty row below
    emptyRow = ws.Range("b65536").End(xlUp).Row + 1

    ' Create/Set a variable for referencing the range we want to use
    ' The range will be set to start at column b of
    ' the empty row and go to column AF of the empty row
    Dim rng As Range
    Set rng = ws.Range("b" & emptyRow & ":AF" & emptyRow)


    'Transfer information
    ' If we didn't use "With rng", below, you'd have to write each of these lines like:
    ' rng.Cells(...
    ' Since the range is only 1 row, you can replace the .Cells(emptyrow, 1), etc.
    ' like you had and just do .Cells(1,1), etc.
    With rng
        .Cells(1, 1).Value = NameTextBox.Value
        .Cells(1, 2).Value = PositionTextBox.Value
        .Cells(1, 3).Value = EmployeeIDTextBox.Value
        .Cells(1, 4).Value = GenderComboBox.Value
        .Cells(1, 5).Value = NationalityTextBox.Value
        .Cells(1, 6).Value = DOBTextBox.Value
        .Cells(1, 7).Value = PassportTextBox.Value
        .Cells(1, 8).Value = PassportExpTextBox.Value
        .Cells(1, 9).Value = MedicalTextBox.Value
        .Cells(1, 10).Value = YFTextBox.Value
        .Cells(1, 11).Value = Lic1TextBox.Value
        .Cells(1, 12).Value = Lic1FlagTextBox.Value
        .Cells(1, 13).Value = Lic1ExpTextBox.Value
        .Cells(1, 14).Value = Lic2TextBox.Value
        .Cells(1, 15).Value = Lic2FlagTextBox.Value
        .Cells(1, 16).Value = Lic2ExpTextBox.Value
        .Cells(1, 17).Value = DPComboBox.Value
        .Cells(1, 18).Value = DPCertTextBox.Value
        .Cells(1, 19).Value = DPCertExpTextBox.Value
        .Cells(1, 20).Value = GMDSSTextBox.Value
        .Cells(1, 21).Value = GMDSSCertTextBox.Value
        .Cells(1, 22).Value = GMDSSExpTextBox.Value


        If RadarCheckBox.Value = True Then .Cells(1, 23).Value = "Yes"

        If ArpaCheckBox.Value = True Then .Cells(1, 24).Value = "Yes"

        If EcdisCheckBox.Value = True Then .Cells(1, 25).Value = "Yes"

        If BosietCheckBox.Value = True Then .Cells(1, 26).Value = "Yes"

        If HuetCheckBox.Value = True Then .Cells(1, 27).Value = "Yes"

        If HloCheckBox.Value = True Then .Cells(1, 28).Value = "Yes"

        If OrbCheckBox.Value = True Then .Cells(1, 29).Value = "Yes"

        If EACheckBox.Value = True Then .Cells(1, 30).Value = "Yes"

        If VsoOptionButton1.Value = True Then
            .Cells(1, 31).Value = "Yes"
        Else
            .Cells(1, 31).Value = "No"
        End If

    End With


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