Выбор Contigous Range из двух длинных типов данных - PullRequest
0 голосов
/ 14 июня 2019

Я пытаюсь получить приемлемый диапазон, используя два длинных типа данных, но я получаю ошибку 1004.

Private Sub SearchButton_Click()
'Finds the last non-blank cell in a single row or column

Dim lRow As Long
Dim lCol As Long

    'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    'Find the last non-blank cell in row 1
    lCol = Cells(1, "S").Column

    ***Range("rRow", "rCol").Select***

End Sub

Ответы [ 2 ]

1 голос
/ 14 июня 2019

Вы можете попробовать:

Код:

Option Explicit

Private Sub SearchButton_Click()

    Dim lRow As Long, lCol As Long

    'Refer to you worksheet
    With ThisWorkbook.Worksheets("Sheet1")

        'Find Last row of column A (Based on the example answer is 4)
        lRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Find last column of row 5 (Based on the example answer is 7)
        lCol = .Cells(5, .Columns.Count).End(xlToLeft).Column

        'Range select
       .Range(.Cells(1, 1), .Cells(lRow, lCol)).Select

    End With

End Sub

Изображения:

enter image description here

1 голос
/ 14 июня 2019

VBA не принимает переменные в кавычках, они используются для вставки строк.Переменные передаются без запятых.Как в коде ниже.

Используйте это:

Private Sub SearchButton_Click()
'Finds the last non-blank cell in a single row or column

Dim lRow As Long
Dim lCol As Long

    'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    'Find the last non-blank cell in row 1
    lCol = Cells(1, "S").Column

    Range(Cells(1, 1), Cells(lRow, lCol)).Select
End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...