排序时卡在 运行 模式
Stuck in running mode while sorting
我正在尝试按字符串 sby 中的文本对 sheet 2
到 sheet 9
上的数据进行排序。
我认为从下面的代码来看逻辑很简单。
但是下面的代码给出了
runtime-error 1004; Select Method of Range Class Failed for the line wk.Rows(j).EntireRow.Select
更新 1:
I don't get the error now but it's stuck in [running] mode for a long time I had to break the code execution. I am trying to implement the I/O logic below. I think my code should work but it's either taking very long or stuck in an infinite loop. Can you help
输入:
col B
Commercial
Tech
Operation
Commercial
Commercial
.
.
.
.
输出:
col B
Commercial
Commercial
Commercial
Tech
Operation
.
.
.
代码
Option Explicit
Sub SortByCommercial()
Dim wk As Worksheet
Dim sby, FinalRow, i, j, p
Dim WsCount As Integer
WsCount = ActiveWorkbook.Worksheets.Count
Dim wb As Workbook
Set wb = ActiveWorkbook
sby = "Commercial"
For i = 2 To WsCount
Set wk = Sheets(i)
FinalRow = wk.Range("A900000").End(xlUp).Row
p = FinalRow + 1
For j = 2 To FinalRow
If Trim(wk.Range("B" & j).Text) <> sby Then
wk.Rows(j).EntireRow.Select
Selection.Cut
wk.Range("A" & p).Select
wk.Paste
Rows(j).EntireRow.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Application.CutCopyMode = False
j = j - 1
Else: End If
Next j
Next i
End Sub
Sub SortByCommercial()
Dim wk As Worksheet
Dim sby, FinalRow, i, j, p
Dim WsCount As Integer
WsCount = ActiveWorkbook.Worksheets.Count
Dim wb As Workbook
Dim lngRowTemp as Long
Set wb = ActiveWorkbook
sby = "Commercial"
For i = 2 To WsCount
Set wk = Sheets(i)
Sheets(i).Activate
For j = 2 To FinalRow
FinalRow = wk.Range("A900000").End(xlUp).Row + 1
lngRowTemp = FinalRow -1
If Trim(wk.Range("B" & j).Text) <> sby Then
wk.Rows(j).EntireRow.Select
Selection.Cut
wk.Range("A" & FinalRow).Select
wk.Paste
Application.CutCopyMode = False
End If
Next j
for j = lngRowTemp to 2 step -1
if cells("B" & j).value = "" then
cells("B" & j).EntireRow.Delete
end if
next j
Next i
End Sub
我正在尝试按字符串 sby 中的文本对 sheet 2
到 sheet 9
上的数据进行排序。
我认为从下面的代码来看逻辑很简单。
但是下面的代码给出了
runtime-error 1004; Select Method of Range Class Failed for the line
wk.Rows(j).EntireRow.Select
更新 1:
I don't get the error now but it's stuck in [running] mode for a long time I had to break the code execution. I am trying to implement the I/O logic below. I think my code should work but it's either taking very long or stuck in an infinite loop. Can you help
输入:
col B
Commercial
Tech
Operation
Commercial
Commercial
.
.
.
.
输出:
col B
Commercial
Commercial
Commercial
Tech
Operation
.
.
.
代码
Option Explicit
Sub SortByCommercial()
Dim wk As Worksheet
Dim sby, FinalRow, i, j, p
Dim WsCount As Integer
WsCount = ActiveWorkbook.Worksheets.Count
Dim wb As Workbook
Set wb = ActiveWorkbook
sby = "Commercial"
For i = 2 To WsCount
Set wk = Sheets(i)
FinalRow = wk.Range("A900000").End(xlUp).Row
p = FinalRow + 1
For j = 2 To FinalRow
If Trim(wk.Range("B" & j).Text) <> sby Then
wk.Rows(j).EntireRow.Select
Selection.Cut
wk.Range("A" & p).Select
wk.Paste
Rows(j).EntireRow.Select
Selection.Delete Shift:=xlUp
Range("A1").Select
Application.CutCopyMode = False
j = j - 1
Else: End If
Next j
Next i
End Sub
Sub SortByCommercial()
Dim wk As Worksheet
Dim sby, FinalRow, i, j, p
Dim WsCount As Integer
WsCount = ActiveWorkbook.Worksheets.Count
Dim wb As Workbook
Dim lngRowTemp as Long
Set wb = ActiveWorkbook
sby = "Commercial"
For i = 2 To WsCount
Set wk = Sheets(i)
Sheets(i).Activate
For j = 2 To FinalRow
FinalRow = wk.Range("A900000").End(xlUp).Row + 1
lngRowTemp = FinalRow -1
If Trim(wk.Range("B" & j).Text) <> sby Then
wk.Rows(j).EntireRow.Select
Selection.Cut
wk.Range("A" & FinalRow).Select
wk.Paste
Application.CutCopyMode = False
End If
Next j
for j = lngRowTemp to 2 step -1
if cells("B" & j).value = "" then
cells("B" & j).EntireRow.Delete
end if
next j
Next i
End Sub