预先感谢大家的帮助。我根本不是一个高级编码器,但除了一个非常基本的东西外,还是以某种方式设法使以下代码工作。我正在努力解决一个非常基本的问题,即每次条件不得到时实际删除1行实现。并且行号应从FOR循环中获取。 FOR循环当前所在的坐标应该由S.Row反映出来,但是正如我在下面所示,我一直在尝试多种删除它的方法,并且总是遇到“运行时错误'1004':定义或对象定义的错误”,这让我发疯,请帮助。一次多谢大家:
Public Sub Optionfilter()
Dim StrikeD As Date
Dim RefD As Date
Dim StrikeP As Integer
Dim S As Range
Dim R As Range
Dim XVAR As Integer
Dim Intervall As String
Dim Number As Integer
Dim TotalRow As Integer
Dim L As Integer
XVAR = 5
Intervall = ThisWorkbook.Worksheets("Data").Range("I2").Value
Debug.Print Intervall
Number = ThisWorkbook.Worksheets("Data").Range("G2").Value
Debug.Print Number
Debug.Print ThisWorkbook.Worksheets("Data").Range("G2").Address
Debug.Print "-----------------------------------------"
XP:
For Each S In ThisWorkbook.Worksheets("Data").Range("J6:J" & ThisWorkbook.Worksheets("Data").UsedRange.SpecialCells(xlCellTypeLastCell).Row)
Debug.Print S.Address & " in the Calc Loop"
Debug.Print ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column - 1).Value
Debug.Print ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column - 1).Address
StrikeD = ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column - 1).Value
Debug.Print StrikeD
Debug.Print "-----------------------------------------"
RefD = ThisWorkbook.Worksheets("Data").Range("E2").Value
Debug.Print RefD
RefD = DateAdd(Intervall, Number, RefD)
Debug.Print RefD
DIFFRAW = Abs(StrikeD - RefD)
Debug.Print DIFFRAW
ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column + 1).Value = DIFFRAW
If ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column + 1).Value > XVAR Then
Debug.Print S.Row
L = S.Row
'This code below is not deleting anything for some reason it's just getting executed but no row disappear/delete
Rows(S.Row).EntireRow.Delete
'This code below is also not deleting anything for some reason it's just getting executed but no row disappear/delete
Range("J" & S.Row).EntireRow.Delete
'This code below is giving me the mentioned ERROR
'ThisWorkbook.Worksheets("Data").Rows(L).Delete
'OR Run-time error 438
ThisWorkbook.Worksheets("Data").S.EntireRow.Delete
'OR
Range("A" & S.Row).EntireRow.Delete
'OR
ThisWorkbook.Worksheets("Data").Rows("S.Row").Delete
'OR
ThisWorkbook.Worksheets("Data").Range(S, 1).EntireRow.Delete
Else
End If
Next S
TotalRow = ActiveSheet.UsedRange.Rows.Count
Debug.Print TotalRow
Debug.Print ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column).Address
Debug.Print ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column).Value
Sheets.Add After:=Worksheets("Data") 'After we deleted the old datasheet, we now insert a new (empty) one
Sheets(3).Name = "TEMP" 'and rename it instead of the defaultname to
If TotalRow <= 10 Then ' And ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row, S.Column).Value > 0 Then
For Each R In ThisWorkbook.Worksheets("Data").Range("J6:J" & ThisWorkbook.Worksheets("Data").UsedRange.SpecialCells(xlCellTypeLastCell).Row)
ThisWorkbook.Worksheets("Data").UsedRange.Cells(S.Row).Copy
ThisWorkbook.Worksheets("Temp").Cells(j, 2).PasteSpecial xlPasteValues
j = j + 1 'This is a controlvariabel to write the copied cell everytime in a new row
Next R
Else
XVAR = XVAR - 1
GoTo XP
End If
End Sub
'S eine erste Position zu ordnen
'zusätzlichen Goto einfpügen falls 0 resultate sind und schleife mit Prioritisierung der ergebnisse -->besser davor als danahc usw.
' Debug.Print ActiveSheet.Cells(S.Row, S.Column + 7).Value
's.Value = Replace(s.Value, ",", "") 'delete the ","
' If InStr(1, S, "S") > 0 Then 'The command InStr(1, s, "S") respond the place (position) where it found "S" in the cellstring
' '(s), for example: 1/5/3... basically it looks if in this cell the letter appears
' S.Value = Replace(S.Value, " ", "") 'If it has found an "S" in the cell value (for example in the portgolionumber
' '011 1044 S02) then it replaces all " " by "" and write the new value (0111044S02) in the cell
' End If 'Ends the if condition
答案 0 :(得分:1)
我无法跟踪您在此处发布的内容,但这解决了您最初的问题的核心:
Dim i As Long, ws As Worksheet, XVAR As Long
Set ws = ThisWorkbook.Worksheets("Data")
'XVAR = '...something
For i = ws.Cells(Rows.Count, 10).End(xlUp).Row To 6 Step -1
If ws.Cells(i, 11).Value > XVAR Then
ws.Rows(i).Delete
End If
Next i
答案 1 :(得分:0)
这可能与您遇到的问题有关,也可能与您遇到的问题无关,但我写这封信是为了帮助您解决似乎存在的误解。
您的变量S
是一个范围。因此,当您设置该范围时,excel就知道有关该范围的所有信息。它知道它的价值,行和列,包含它的工作表以及包含该工作表的工作簿。限定S
变量或仅使用该行的Long
值来引用同一行的所有代码...最好是多余的,并且会导致更糟的错误。
例如:
Rows(S.Row).EntireRow.Delete
您实际上是在说:“获取代表此单元格所在行的数字,并在当前处于活动状态的任何工作表中(谁知道那是什么??),删除与该数字相对应的整行”。 / p>
相反:
S.EntireRow.Delete
现在它说:“删除变量S
中保存的单元格所在的整行”。
关于您的错误:
ThisWorkbook.Worksheets("Data").S.EntireRow.Delete
这是说“变量S
中的单元格,我们已经通过保留在该变量中的范围对象的性质知道它驻留在哪个工作表和工作簿中,(尽管如此,我还是告诉您)在ThisWorkbook中,并且名为Data
的工作表将其全部删除。”
Excel并没有因此而失败,因为您无法通过工作表和工作簿来限定这样的范围对象。它已经设置并且不可更改。在工作表“数据”和S
中,范围ThisWorkbook
已100%不变。您试图再次告诉excel这些信息只是让excel生气。
相反:
S.EntireRow.Delete
看起来很熟悉。
答案 2 :(得分:0)
非常感谢您的阐述和花费的时间。非常感谢,谢谢。这是有道理的,但是将您的代码(S.EntireRow.Delete)复制到我的语法中后,我总是收到此处所述的错误:https://qph.fs.quoracdn.net/main-qimg-0ea61a60a22c8fbbbf3e95c1b463b242