vba复制并粘贴变量行

时间:2018-09-27 19:45:18

标签: excel vba excel-vba

第一个帖子在这里。我没有使用VBA的经验,并且从该网站上的许多搜索中将以下内容结合在一起。这个社区很棒。

我有一个数据表,其中的行数可以根据制造表格上填写的内容而变化。我正在尝试复制范围并将其粘贴到第二张工作表中,但是我需要VBA足够智能,使其仅能到达实际包含结果数据的行-因为我有公式,如果数据行中的结果为“”表格上的内容未填写。

我的数据从I12开始,并将始终下降到Q:#。有人可以帮我了解如何正确使用我的Lastrow吗?下面的代码。

    Sub Post_P()
'
' Post_P Macro

'these are setting dimentions for the macro
Dim wb As Workbook
Dim wsData As Worksheet
Dim wsDest As Worksheet
Dim LastRow As Long
Dim LastCol As Long


'these are setting definitions for the sheets
Set wb = ActiveWorkbook
Set wsData = wb.Sheets("Production Sheet")
Set wsDest = wb.Sheets("Journalized Result")



'this next part is looking at the flexible range of data on the input tab and copy and pasting values to inventory log tab
With wsData
    LastRow = Cells.Find(What:="*", After:=Range("I12"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastCol = Cells.Find(What:="*", After:=Range("I12"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    wsData.Range("I12:Q" & LastRow).Copy


End With


With wsDest
    wsDest.Range("b" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End With


With wsDest 'this is autofilling the formula in A base on the number of rows in B
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    With .Range("A3:A" & LastRow)
        .Formula = "=a2+1"
    End With

End With

'this next part is returning the user to the initial page
With wsData.Range("b11").Select


MsgBox ("Post Completed")
End With

End Sub

非常感谢您的帮助。

谢谢

0 个答案:

没有答案