每当我按下按钮找到它时,我正在寻找一种获取价值的方法,我一直在使用单元格(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
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
有人会帮我解决我的问题吗?
提前致谢
答案 0 :(得分:0)
试试这个:
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