Проблема для компенсации последней - PullRequest
0 голосов
/ 03 апреля 2019

У меня есть проблема, чтобы сместить из предыдущей строки мою последнюю строку. На самом деле я пытаюсь запустить мой код ниже, у меня появляется сообщение об ошибке

ошибка компиляции: неверный квалификатор…

Проблема, очевидно, исходит от моей переменной lastRow4 = lastRow3.Offset(-1, 0)

Когда я удаляю эту строку моего кода и заменяю в своем коде VBA предпоследнюю строку Range("A3:F" & lastRow4).Select на Range("A3:F" & lastRow3).Select, мой код работает, но выбор выбирает мой диапазон до lastrow (в моем коде это lastRow3) и это не то, что я хочу.

Если кто-то знает решение, как решить проблему, чтобы моя переменная lastRow4 не возвращала мне никаких сообщений об ошибках, это было бы здорово.

Большое спасибо заранее. Хави

Sub gdfgdhgf()


Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer, lastRow3 As Integer, lastRow4 As Integer
Dim currentRowValue As String

sourceCol = 5 'column F has a value of 5
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
        lastRow3 = currentRow
    End If
Next
lastRow4 = lastRow3.Offset(-1, 0)
Range("A3:F" & lastRow4).Select

End Sub

Ответы [ 2 ]

3 голосов
/ 03 апреля 2019

Попробуйте приведенный ниже код, объяснение в комментариях к Кодексу:

Option Explicit

Sub gdfgdhgf()

Dim sourceCol As Long, rowCount As Long, currentRow As Long, lastRow3 As Long, lastRow4 As Long
Dim CurrentRng As Range
Dim currentRowValue As String
Dim Sht As Worksheet

Set Sht = ThisWorkbook.Sheets("Sheet1") '<-- modify "Sheet1" to your sheet's name

sourceCol = 5 'column F has a value of 5

With Sht
    rowCount = .Cells(.Rows.Count, sourceCol).End(xlUp).Row ' for every row, find the first blank cell and select it

    For currentRow = 1 To rowCount
        ' I think you are trying to set it as a range
        Set CurrentRng = .Cells(currentRow, sourceCol)

        If IsEmpty(CurrentRng) Or CurrentRng.Value2 = "" Then
            lastRow3 = CurrentRng.Row ' get the row of the CurrentRng.Row
        End If
    Next

    lastRow4 = lastRow3 - 1 ' just subtract 1, no need to use Offset
    .Range("A3:F" & lastRow4).Select ' <-- Not sure why you need to Select
End With

End Sub
3 голосов
/ 03 апреля 2019

Ниже не код.это пример.измените и попробуйте:

Option Explicit

Sub test()

    Dim LastRow As Long

    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Get the last row of column A sheet1

        'In order to refer to line before Lastrow you can use:

        .Range ("A" & LastRow - 1)

    End With

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