我有一直在使用的这段代码(不是我的)。它对我来说效果很好,因为我知道我可以将sh.Rows ("x")
中的值更改为所需的任何行,并且它将获取我需要的所有内容。我想让我的一位同事更容易使用它,这样他们就不必进入Visual Basics来对其进行编辑。是否有一种简单的方法可以使它从每个工作表中提取单元格B2中的任何行并将其粘贴到母版工作表中?
Sub CopytoMaster()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
If SheetExists("Master") = True Then
MsgBox "The sheet Master already exist"
Exit Sub
End If
Application.ScreenUpdating = False
Set DestSh = Worksheets.Add
DestSh.Name = "Master"
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
If sh.UsedRange.Count > 1 Then
Last = LastRow(DestSh)
sh.Rows("7").Copy DestSh.Cells(Last + 1, 1)
End If
End If
Next
Application.ScreenUpdating = True
End Sub
Sub CheckMaster()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
If SheetExists("Master") = True Then
MsgBox "The sheet Master already exist"
Exit Sub
End If
Application.ScreenUpdating = False
Set DestSh = Worksheets.Add
DestSh.Name = "Master"
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
If sh.UsedRange.Count > 1 Then
Last = LastRow(DestSh)
With sh.Rows("7")
DestSh.Cells(Last + 1, 1).Resize(.Rows.Count, _
.Columns.Count).Value = .Value
End With
End If
End If
Next
Application.ScreenUpdating = True
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function Lastcol(sh As Worksheet)
On Error Resume Next
Lastcol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(Sheets(SName).Name))
End Function
答案 0 :(得分:0)
您可以简单地使用<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="26dp"
android:background="null"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:srcCompat="@mipmap/ic_launcher_round" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:background="null"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:srcCompat="@mipmap/ic_launcher" />
</RelativeLayout>
方法来获取B2的值。将其放在Range.Value
方法中。换句话说,您只需要将.Row()
更改为sh.Rows("7")
。
sh.Rows(ws.range("B2").value)
与第二步相同:
Sub CopytoMaster()
Dim sh As Worksheet, ws As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
If SheetExists("Master") = True Then
MsgBox "The sheet Master already exist"
Exit Sub
End If
Application.ScreenUpdating = False
Set DestSh = Worksheets.Add
DestSh.Name = "Master"
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
If sh.UsedRange.Count > 1 Then
Last = LastRow(DestSh)
sh.Rows(ws.Range("B2").Value).Copy DestSh.Cells(Last + 1, 1)
End If
End If
Next
Application.ScreenUpdating = True
End Sub
其中Sub CheckMaster()
Dim ws As Worksheet
...
With sh.Rows(ws.Range("B2").Value)
DestSh.Cells(Last + 1, 1).Resize(.Rows.Count, _
.Columns.Count).Value = .Value
End With
是包含相关值的工作表对象。您尚不清楚此工作表是否与ws
相同,因此,可以将sh
更改为ws
-否则,您需要设置 sh
到包含值的工作表中。
答案 1 :(得分:0)
这就是我现在拥有的,并且它正在按我想要的方式工作。
Sub CopytoMaster2()
Dim wb As Workbook
Dim sh As Worksheet
Dim ws As Worksheet
Dim DestSh As Worksheet
Dim mainSh As Worksheet
Dim Last As Long
If SheetExists("Master") = True Then
MsgBox "The sheet Master already exist"
Exit Sub
End If
Application.ScreenUpdating = False
Set DestSh = Worksheets.Add
DestSh.Name = "Master"
Set wb = ActiveWorkbook
Set mainSh = wb.Sheets("Main")
For Each sh In ThisWorkbook.Worksheets
If sh.Name <> mainSh.Name And sh.Name <> DestSh.Name Then
If sh.UsedRange.Count > 1 Then
Last = LastRow(DestSh)
sh.Rows(mainSh.Range("E7").Value).Copy DestSh.Cells(Last + 1, 1)
End If
End If
Next
Application.ScreenUpdating = True
结束子
Sub CheckMaster2() 昏暗的wb作为工作簿 昏暗工作表 昏暗的工作表 Dim DestSh作为工作表 昏暗的mainSh作为工作表 最后的昏暗 如果SheetExists(“ Master”)= True,则 MsgBox“工作表母版已经存在” 退出子 万一 Application.ScreenUpdating = False 设置DestSh = Worksheets.Add DestSh.Name =“大师” 设置wb = ActiveWorkbook 设置mainSh = wb.Sheets(“ Main”)
For Each sh In ThisWorkbook.Worksheets
If mainSh.Name <> sh.Name And sh.Name <> DestSh.Name Then
If sh.UsedRange.Count > 1 Then
Last = LastRow(DestSh)
With sh.Rows(mainSh.Range("E7").Value)
DestSh.Cells(Last + 1, 1).Resize(.Rows.Count, _
.Columns.Count).Value = .Value
End With
End If
End If
Next
Application.ScreenUpdating = True
结束子
函数LastRow2(sh作为工作表) 关于错误继续 LastRow = sh.Cells.Find(What:=“ *”,_ 之后:= sh.Range(“ A1”),_ 查找:= xlPart,_ LookIn:= xlFormulas,_ SearchOrder:= xlByRows,_ SearchDirection:= xl上一个_ MatchCase:= False)。行 出错时转到0 结束功能
函数Lastcol2(sh作为工作表) 关于错误继续 Lastcol = sh.Cells.Find(What:=“ *”,_ 之后:= sh.Range(“ A1”),_ 查找:= xlPart,_ LookIn:= xlFormulas,_ SearchOrder:= xlByColumns,_ SearchDirection:= xl上一个_ MatchCase:= False).Column 出错时转到0 结束功能 Function SheetExists2(SName As String,_ 可选的ByVal wb作为工作簿)为布尔值 关于错误继续 如果wb什么都没有,则设置wb = ThisWorkbook SheetExists = CBool(Len(Sheets(SName).Name)) 结束功能