我正在创建一个快速脚本来选择和复制特定的行,但遇到了问题。我一直收到错误" Type Mismatch"参考这一行:
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。
答案 0 :(得分: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