1

我一直在寻找这个问题的答案:

使用下面的代码,我想在每组唯一值的末尾输入一个空行。最重要的是,我希望它有一个提示,允许用户键入列范围的字母。我已经尝试了其中的一些,无法用查询答案替换“B”。

Dim lRow As Long
For lRow = Cells(Cells.Rows.Count, "B").End(xlUp).Row To 2 Step -1
If Cells(lRow, "B") <> Cells(lRow - 1, "B") Then Rows(lRow).EntireRow.Insert
Next lRow
End Sub

有什么建议么?

4

4 回答 4

1

尝试这个

Sub Demo()
    Dim lRow As Long
    Dim sCol As String
    sCol = InputBox("Enter Column", sCol)
    For lRow = Cells(Cells.Rows.Count, sCol).End(xlUp).Row To 2 Step -1
        If Cells(lRow, sCol) <> Cells(lRow - 1, sCol) Then
            Rows(lRow).Insert
        End If
    Next lRow
End Sub
于 2013-02-06T01:15:27.793 回答
1

我想您的问题是,您希望用户能够输入“B”、“AA”、“C”作为列?

部分复制@Chris 的代码

Sub Demo()
    Dim lRow As Long
    Dim sCol As String
    Dim colNum as string
    sCol = InputBox("Enter Column", sCol)
    colNum  = columns(sCol).column
    For lRow = Cells(Cells.Rows.Count, colNum  ).End(xlUp).Row To 2 Step -1
        If Cells(lRow, colNum  ) <> Cells(lRow - 1, colNum  ) Then
            Rows(lRow).Insert
        End If
    Next lRow
End Sub
于 2013-02-06T06:01:11.107 回答
0

尝试Range("B" & Cells.Rows.Count)代替,Cells(Cells.Rows.Count, "B")其余的也类似。

于 2013-02-05T19:58:55.150 回答
0

包括打开提示,这就是我最终使用的。
将打开提示连接到一个按钮,以使非技术人员更容易使用:

Sub InsertRowAtChangeInValue()

    Dim customerBook As Workbook
    Dim filter As String
    Dim caption As String
    Dim customerFilename As String
    Dim customerWorkbook As Workbook
    Dim targetWorkbook As Workbook

    Set targetWorkbook = Application.ActiveWorkbook

    filter = "Excel 2007 files (*.xlsx),*.xlsx, Excel 97-03 files (*.xls),*xls, All files (*.*),*.*"
    caption = "Please select an input file."
    customerFilename = Application.GetOpenFilename(filter, , caption)

    Set customerWorkbook = Application.Workbooks.Open(customerFilename)

    Dim targetSheet As Worksheet
    Set targetSheet = targetWorkbook.Worksheets(1)
    Dim sourceSheet As Worksheet
    Set sourceSheet = customerWorkbook.Worksheets(1)

    targetSheet.Range("A1", "AR5000").Value = sourceSheet.Range("A1", "AR5000").Value

    Dim lRow As Long
    Dim sCol As String
    Dim colNum As String
    sCol = InputBox("Enter Column", sCol)
    colNum = Columns(sCol).Column
    For lRow = Cells(Cells.Rows.Count, sCol).End(xlUp).Row To 2 Step -1
        If Cells(lRow, sCol) <> Cells(lRow - 1, sCol) Then
            Rows(lRow).Insert
        End If
    Next lRow
End Sub

再次感谢您的帮助!

于 2013-02-06T18:43:04.530 回答