Excel 公共函数vba语言

Excel 公共函数vba语言,excel,vba,Excel,Vba,我必须滚动一列数据,当我从privious单元格(n-1)中找到不同的值时,我必须添加一行 我试图设置使用公共功能,因为我需要所有工作表,但不起作用: Public Sub f() For i = 1 To FinalRow If Cells(row, 4) <> Cells(row - 1, 4) Then cell(i, 4).Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAboven.

我必须滚动一列数据,当我从privious单元格(n-1)中找到不同的值时,我必须添加一行

我试图设置使用公共功能,因为我需要所有工作表,但不起作用:

Public Sub f()

For i = 1 To FinalRow

  If Cells(row, 4) <> Cells(row - 1, 4) Then
    cell(i, 4).Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAboven.Delete
    Count = Count + 1
  End If

Next i

End Sub
Public Sub f()
对于i=1到最后一行
如果单元格(第4行)单元格(第1行,第4行),则
单元格(i,4).Selection.Insert Shift:=xlDown,CopyOrigin:=xlFormatFromLeftOrAboven.Delete
计数=计数+1
如果结束
接下来我
端接头
怎么了? 有人能帮我吗?

试试这个:

Public Sub f()

    Dim totalRows As Long
    Dim currentRow As Long

    currentRow = 1 'Start parsing in row 1
    totalRows = 9 'Replace with your total

    Do While currentRow <= totalRows
        'Compare it to the next row
        If Worksheets("Sheet1").Range("A" & currentRow).Value = Worksheets("Sheet1").Range("A" & (currentRow + 1)).Value Then
            'They are the same -> go to the next row
            currentRow = currentRow + 1
        Else
            'They are different -> insert a row (and add it to the total)
            Worksheets("Sheet1").Range("A" & currentRow).Activate
            ActiveCell.Offset(1).EntireRow.Insert
            currentRow = currentRow + 2 'You want to parse the next row but skip the one you inserted
            totalRows = totalRows + 1
        End If
    Loop

End Sub
Public Sub f()
所有行的长度相同
与当前行一样长
currentRow=1'在第1行中开始分析
totalRows=9'替换为您的总计
当当前行转到下一行时执行此操作
currentRow=currentRow+1
其他的
'它们不同->插入一行(并将其添加到总数中)
工作表(“Sheet1”).范围(“A”和currentRow).激活
ActiveCell.Offset(1).EntireRow.Insert
currentRow=currentRow+2'您想分析下一行,但跳过插入的那一行
totalRows=totalRows+1
如果结束
环
端接头

您能解释一下为什么或什么不适用于您的函数吗?FinalRow变量来自哪里(它似乎不包含值)?为什么你需要count=count+1?我必须在marco的txt中插入这个txt。我有一个城市名称的堆栈,当宏滚动时,该列应添加一行,当城市名称与之前不同时,您可以进一步澄清您想要实现的目标。你似乎使用了很多没有实际使用的变量。也许可以说明一个简单的示例数据?示例:伦敦伦敦伦敦纽约纽约西雅图西雅图西雅图setalle我的宏结果应该是:伦敦伦敦伦敦伦敦纽约纽约纽约西雅图西雅图西雅图setalle我必须插入一个新行,当一个previous@user3619429你从哪里得到这个密码的?你知道你写的代码到目前为止做了什么吗?你写“…但不起作用”,所以我们想知道到底什么不起作用:它什么也不做/它给你一个特定的错误(哪个错误和哪行代码)/它只起作用一次/。。。