我一直在寻找答案:
使用下面的代码我想在每组唯一值的末尾输入一个空行。踢球者是,我希望它有一个提示,允许用户键入列范围的字母。我已尝试了其中一些,不能用查询答案替换“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
任何建议?
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分:0)
尝试使用Range("B" & Cells.Rows.Count)
而不是Cells(Cells.Rows.Count, "B")
,其余部分也是如此。
答案 3 :(得分: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
再次感谢您的帮助!