0
Sub Delete_Columns()
    Dim Last_Row As Integer
    Dim rnge As Range
    Dim celladdres As Variant
    Dim v As Integer

    Last_Row = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row

    Cells(Last_Row, [13]).Value = "Sampling"
    Range("C3").Copy Range("C" & Last_Row)
    Range("B3").Copy Range("B" & Last_Row)
    Range("A3").Copy Range("A" & Last_Row)

    Set rnge = Range("G3:G1000").Find(what:="Description")
    rnge.Find what:="Description"

    celladdres = rnge.Offset(-1, 30).Address
    Range("a2", [celladdress]).Delete
End Sub

嗨,我试图在描述时偏移列和行,如果发现要从先前的测试中删除上面的所有数据,但是当我偏移它时,行正在向上移动,但列没有偏移它假设的 30。它没有任何错误。我是否需要将字母设置为值才能使其正常工作?谢谢最大

4

2 回答 2

2
  1. 确保您用于Option Explicit检测可变错别字。

  2. 确保每个 RangeCells和对象都有一个像 一样引用的工作表Rows,否则它将占用任何处于活动状态的工作表(并且可以通过用户单击轻松更改)。Columsws.Range

  3. 正如您在Range.Find 方法的文档中所见,您绝对需要指定以下 4 个参数,否则会得到随机结果:

    每次使用此方法时都会保存LookInLookAtSearchOrder和的设置。MatchByte如果下次调用该方法时没有为这些参数指定值,则使用保存的值。设置这些参数会更改“查找”对话框中的设置,而更改“查找”对话框中的设置会更改在省略参数时使用的保存值。为避免出现问题,请在每次使用此方法时显式设置这些参数。

    如果不定义这些参数,它可能现在可以工作并在下次停止工作。

  4. 使用该Find()方法后,请确保您测试是否找到了某些东西,If Not FoundAt Is Nothing Then否则它会出错。

  5. [ ]您使用的 in与符号[celladdress]无关,[A1]并且不像您假设的那样工作。他们需要被删除!

  6. 声明你的变量尽可能接近它们的第一次使用而不是最顶部。否则你很容易得到类似的东西Dim v As Integer并且永远不会v在整个代码中使用。

  7. 最后使用正确的代码格式和正确的缩进。代码越容易阅读,错误就越少,调试也就越容易。不要像“我稍后会修复格式”那样工作。这会减慢您编写好代码的速度,并且您可能永远无法修复它。

所以你最终会得到类似的东西:

Option Explicit

Public Sub Example()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1") 'define your sheet
     
    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1, 0).Row

    ws.Cells(LastRow, 13).Value = "Sampling"
    ws.Range("C3").Copy ws.Range("C" & LastRow)
    ws.Range("B3").Copy ws.Range("B" & LastRow)
    ws.Range("A3").Copy ws.Range("A" & LastRow)
     
    Dim FoundAt As Range 'define ALL these parameters below to prevent random/wrong results
    Set FoundAt = ws.Range("G3:G1000").Find(What:="Description", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchByte:=False)
    
    If Not FoundAt Is Nothing Then 'test if something was found you cannot delete if you found nothing
        Dim CellAddress As String
        CellAddress = FoundAt.Offset(-1, 30).Address
        ws.Range("A2", CellAddress).Delete
    Else
        MsgBox "'Description' was not found.", vbCritical
    End If
End Sub
于 2020-09-04T11:22:58.427 回答
1

采样

  • 有了这么少的信息,我只能想出这个。如果有什么被误解,这些评论应该可以帮助你改变。

编码

Option Explicit

Sub Sampling()
    
    ' Define worksheet.
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    ' Calculate 'NewRow', the row below last non-blank cell in column "A".
    Dim NewRow As Long
    NewRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1, 0).Row
    
    ' Write "Sampling" in column "M" of 'NewRow'.
    ws.Range("M" & NewRow).Value = "Sampling"
    ' Copy range "A3:C3" to the same columns in 'NewRow'.
    ws.Range("A3:C3").Copy ws.Range("A" & NewRow, "C" & NewRow)

    ' Find the first occurrence of "Description" in column "G"
    ' starting from the 3rd row and ending above 'NewRow'.
    Dim rng As Range
    Set rng = ws.Range("G3", "G" & NewRow - 1) _
                .Find(What:="Description", After:=ws.Range("G" & NewRow - 1), _
                      Lookin:= xlValues, LookAt:=xlWhole)
    ' Check if "Description" was not found.
    If rng Is Nothing Then Exit Sub
        
    ' Delete range 'A2:AK2' resized to the row above of
    ' where "Description" was found.
    ' First test with 'Select'.
    ' When tested, replace 'Select' with 'Delete'.
    ws.Range("A2", "AK" & rng.Row - 1).Select
              
End Sub
于 2020-09-04T12:15:29.487 回答