Vba 查找数据后获取值
我正在寻找一种获取值的方法,在找到值后,每当我按下一个按钮,我都会使用单元格(rows.count,1).value等等,但仍然不起作用Vba 查找数据后获取值,vba,excel,Vba,Excel,我正在寻找一种获取值的方法,在找到值后,每当我按下一个按钮,我都会使用单元格(rows.count,1).value等等,但仍然不起作用 这是我的代码 Private Sub CopyNota_Click() On Error GoTo errorhandler: Application.ScreenUpdating = False Dim strpath As String Dim copysheet As Worksheet Dim pastesheet As Worksheet Se
这是我的代码
Private Sub CopyNota_Click()
On Error GoTo errorhandler:
Application.ScreenUpdating = False
Dim strpath As String
Dim copysheet As Worksheet
Dim pastesheet As Worksheet
Set copysheet = Worksheets("sheet3")
Set pastesheet = Worksheets("sheet5")
strpath = "E:\b\"
Filename = Dir(strpath & "b.xlsx")
If IsEmpty(Range("B2")) Then
Workbooks("b.xlsx").Worksheets("sheet3").Range("H2").Copy destination:=Range("B2")
Workbooks("b.xlsx").Worksheets("sheet3").Range("I2").Copy destination:=Range("C2")
Workbooks("b.xlsx").Worksheets("sheet3").Range("J2").Copy destination:=Range("D2")
Workbooks("b.xlsx").Save
Application.CutCopyMode = False
Application.ScreenUpdating = True
Else
Workbooks("b.xlsx").Worksheets("sheet3").Range("H2").Copy
Worksheets("sheet5").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Workbooks("b.xlsx").Worksheets("sheet3").Range("I2").Copy
Worksheets("sheet5").Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Workbooks("b.xlsx").Worksheets("sheet3").Range("J2").Copy
Worksheets("sheet5").Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Workbooks("b.xlsx").Worksheets("sheet3").Range("A2").Value = Worksheets("sheet5").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value
End If
errorhandler:
If Err.Number = "52" Then
MsgBox "Open The Workbooks First!!!"
Exit Sub
End If
End Sub
有人能帮我解决我的问题吗?
提前感谢试试这个:
Private Sub CopyNota_Click()
On Error GoTo errorhandler:
Application.ScreenUpdating = False
Dim copysheet As Worksheet, pastesheet As Worksheet
Dim wbk As Workbook
Dim bolDoNotOpen As Boolean
Filename = "E:\b\b.xlsx"
'check if any of the opened workbook name is equal to the "b.xlsx"
For Each wbk In Workbooks
If wbk.Name = "b.xlsx" Then
bolDoNotOpen = True
End If
Next wbk
'if none of the workbooks name = "b.xlsx" , then the "b.xlsx" is not open, so we can open it.
If bolDoNotOpen = False Then
Workbooks.Open Filename
End If
Set copysheet = Workbooks("b.xlsx").Worksheets("sheet3") 'added workbook reference
Set pastesheet = Workbooks("b.xlsx").Worksheets("sheet5") 'added workbook reference
If IsEmpty(pastesheet.Range("B2")) Then
pastesheet.Range("B2:D2").Value = copysheet.Range("H2:J2").Value
Workbooks("b.xlsx").Save
Else
'you can change this to do all the values at once. But only if you know, that their row will always be the same.
pastesheet.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = copysheet.Range("H2")
pastesheet.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = copysheet.Range("I2")
pastesheet.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = copysheet.Range("J2")
copysheet.Range("A2") = pastesheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
End If
Application.ScreenUpdating = True
Exit Sub
'obsolete now, we have checked or opened the workbook at the beginning
errorhandler:
If Err.Number = "52" Then
MsgBox "Open The Workbooks First!!!"
Exit Sub
End If
End Sub
单元格(Rows.Count,1)
在a列中查找最后一个值单元格的尝试看起来很糟糕。我建议改为。在您的例子中,它是Cells.Range(“A:A”).Find
@user381967Cells(Rows.Count,2)。End(xlUp)。偏移量(1,0)
实际上是您如何在column@JLILI阿门,。单元格(行,列)
不。单元格(列,行)
@uniks请解释什么不起作用,我不知道为什么你不能复制所有三个单元格并粘贴一次,而不是粘贴三次。OP,你可以而且应该提供更多信息,说明为什么不工作,错误在哪里,可能还会在代码中添加一些注释,这样就更清楚了,你想做什么。在这种情况下,这是相当容易的,但作为一般规则。