列不偏移
Column not offsetting
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 .它不会出现任何错误。我是否需要将字母设置为值才能使其正常工作?谢谢 max
确保使用 Option Explicit
来检测变量拼写错误。
确保每个Range
、Cells
、Rows
和Colums
对象都有一个作品sheet 像 ws.Range
一样被引用,否则它将采用任何 sheet 处于活动状态(并且可以通过用户单击轻松更改)。
正如您在 Range.Find method 的文档中看到的,您必须指定以下 4 个参数,否则您会得到随机结果:
The settings for LookIn
, LookAt
, SearchOrder
, and MatchByte
are saved each time you use this method. If you do not specify values
for these arguments the next time you call the method, the saved
values are used. Setting these arguments changes the settings in the
Find dialog box, and changing the settings in the Find dialog box
changes the saved values that are used if you omit the arguments. To
avoid problems, set these arguments explicitly each time you use this
method.
如果不定义这些参数,它可能现在可以工作,下次停止工作。
使用 Find()
方法后,确保测试是否找到了某些东西 If Not FoundAt Is Nothing Then
否则会出错。
您在 [celladdress]
中使用的 [ ]
与 [A1]
符号无关,并且不会像您假设的那样工作。他们需要被删除!
尽可能在第一次使用时声明您的变量,而不是在最前面。否则,您很容易会得到类似 Dim v As Integer
的结果,而不会在整个代码中使用 v
。
最后使用正确的代码格式和正确的缩进。代码越容易阅读,你犯的错误就越少,调试起来也就越容易。不要像 “我稍后会修复格式” 那样工作。这会减慢您编写优质代码的速度,而且您可能永远无法修复它。
所以你最终会得到这样的结果:
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
采样
- 根据这些信息,我只能想出这个。评论
如果有什么地方被误解了,应该可以帮助你改正。
代码
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
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 .它不会出现任何错误。我是否需要将字母设置为值才能使其正常工作?谢谢 max
确保使用
Option Explicit
来检测变量拼写错误。确保每个
Range
、Cells
、Rows
和Colums
对象都有一个作品sheet 像ws.Range
一样被引用,否则它将采用任何 sheet 处于活动状态(并且可以通过用户单击轻松更改)。正如您在 Range.Find method 的文档中看到的,您必须指定以下 4 个参数,否则您会得到随机结果:
The settings for
LookIn
,LookAt
,SearchOrder
, andMatchByte
are saved each time you use this method. If you do not specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method.如果不定义这些参数,它可能现在可以工作,下次停止工作。
使用
Find()
方法后,确保测试是否找到了某些东西If Not FoundAt Is Nothing Then
否则会出错。您在
[celladdress]
中使用的[ ]
与[A1]
符号无关,并且不会像您假设的那样工作。他们需要被删除!尽可能在第一次使用时声明您的变量,而不是在最前面。否则,您很容易会得到类似
Dim v As Integer
的结果,而不会在整个代码中使用v
。最后使用正确的代码格式和正确的缩进。代码越容易阅读,你犯的错误就越少,调试起来也就越容易。不要像 “我稍后会修复格式” 那样工作。这会减慢您编写优质代码的速度,而且您可能永远无法修复它。
所以你最终会得到这样的结果:
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
采样
- 根据这些信息,我只能想出这个。评论 如果有什么地方被误解了,应该可以帮助你改正。
代码
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