类型不匹配Excel VBA - 如果继续

问题描述 投票:0回答:1

我正在创建一个快速脚本来选择和复制特定的行,但遇到了问题。我一直收到错误“类型不匹配”参考该行:

If rng = "" Then

我是新手,我想我正在抓取一个对象,这是寻找字符串,但我的后退是PHP。我找不到一种方法来var_dump来获取值以进行仔细检查。我不确定转换为我尝试过的字符串是否有效,但似乎无法正确应用。有什么想法可以在这里发生吗?

    Sub duplicateRows()
'
' duplicateRows Macro
'
Dim lastCellOnRow As String
Dim lastCellOnRow2 As String
'Dim rng As String
Dim rangeHere As String
Dim lColDuplicate As Long
Dim lRowDuplicate As Long
Dim theActiveCell As String
'//////////////////////////////////////////////////////////
' Build the variables for selection
'//////////////////////////////////////////////////////////
    'Find the last non-blank cell in column A(1)
    lRowDuplicate = Cells(Rows.Count, 3).End(xlUp).Row

    'Find the last non-blank cell in row 1
    lColDuplicate = Cells(3, Columns.Count).End(xlToLeft).Column
    theActiveCell = "B3:" & Col_Letter(lColDuplicate) & "3"
    theActiveCell = theActiveCell

'//////////////////////////////////////////////////////////
'Make selection and create variable for how many are selected
'//////////////////////////////////////////////////////////
    'ActiveSheet.Range(theActiveCell).Select

    'selectionCount = Selection.Cells.Count
    'MsgBox selectionCount

Dim n As Integer, rng As Range
'n = InputBox("type the value of n")
Set rng = Range(theActiveCell)
rng.Select
line2:
'n = InputBox("type no. of times you want to be repeated minus 1 for e.g if you wnat to be repeated 3 times type 2")
Range(rng.Offset(1, 0), rng.Offset(2, 0)).EntireRow.Insert
Range(rng, rng.End(xlToRight)).Copy
Range(rng, rng.Offset(2, 0)).PasteSpecial
Set rng = rng.Offset(2 + 1, 0)
'CStr(rng)
'RangeToString (rng)
'MsgBox rng
If rng = "" Then
GoTo line1
Else
GoTo line2
End If
line1:
Application.CutCopyMode = False
Range("a1").Select
MsgBox "macro over"


End Sub

编辑:我只想到了关于if的事情。我想知道如果超出范围而不是字符串(“”)是否更有意义。这可能是思考VBA的错误方式。我再次尝试使用PHP。

excel excel-vba vba
1个回答
1
投票

如果您要确定rng对象中的任何单元格是否为空,则可以替换

If rng = "" Then

If rng.Columns.Count <> Application.CountA(rng) Then

FWIW,包含我们在聊天中提到的其他修改的重构代码可以是:

Sub duplicateRows()
    Dim lColDuplicate As Long
    Dim lRowDuplicate As Long
    Dim r As Long
    Dim c As Long

    'Find the last non-blank cell in column C
    lRowDuplicate = Cells(Rows.Count, 3).End(xlUp).Row

    'Find the last non-blank cell in row 3
    lColDuplicate = Cells(3, Columns.Count).End(xlToLeft).Column

    For r = lRowDuplicate To 3 Step -1
        Rows((r + 1) & ":" & (r + 2)).Insert
        For c = 2 To lColDuplicate
            'duplicate data as [xyz]
            Cells(r + 1, c).Value = "[" & Cells(r, c).Value & "]"
            'duplicate data as "xyz"
            Cells(r + 2, c).Value = """" & Cells(r, c).Value & """"
        Next
    Next
    MsgBox "macro over"
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.