我设置了一个宏,用于从另一个工作表导入数据,然后将其放在当前选项卡中。这些数据的顺序有时会有所不同,因此我设置了一个基于单元格值查找特定值的搜索功能。然后选择该列数据并将其放置在应有的位置。这就是我一直在使用的。
Sub SelectData ()
'For Selecting Depth Column Data on Meter Data Tab During Import Meter Data
Dim x As String
x = Cells(9, 14)
Dim lColumn As Long
Dim iCntr As Long
lColumn = 150 'colums from A to check during delete
For iCntr = lColumn To 1 Step -1
If Cells(25, iCntr) = x Then 'You can change this text
Cells(24, iCntr).Select
End If
Next
'For selecting the whole data
Range(ActiveCell.Offset(105000, 0), ActiveCell.Offset(0, 0)).Select
'Paste the data in the apropriate column
Selection.Copy
Range("B2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
今天我现在添加了ElseIf
,因为在用于选择粘贴数据的单元格中的值不存在于数据中。这会提示一个消息框说没有这样的价值。当我添加Elseif
时,它现在总是放错误框。
Sub SelectData ()
'For Selecting Depth Column Data on Meter Data Tab During Import Meter Data
Dim x As String
x = Cells(9, 14)
Dim lColumn As Long
Dim iCntr As Long
lColumn = 150 'colums from A to check during delete
For iCntr = lColumn To 1 Step -1
If Cells(25, iCntr) = x Then 'You can change this text
Cells(24, iCntr).Select
ElseIf Cells(25, iCntr) <> x Then 'You can change this text
MsgBox "Your value in cell N8 does not match the value in Row 2 inside the Meter Data Spreadsheet. Make sure they match and try again." & vbNewLine & "No Data for DEPTH was Imported."
Exit Sub
End If
Next
'For selecting the whole data
Range(ActiveCell.Offset(105000, 0), ActiveCell.Offset(0, 0)).Select
'Paste the data in the apropriate column
Selection.Copy
Range("B2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
答案 0 :(得分:0)
您可能已经解决了它,但这是Find方法的一个示例。您还应该避免使用Selects和ActiveCell。您可能必须指定一些Find参数(建议)。
{{1}}