0

毎回同じ正確な数の行をコピーするように見える最後の行コードへのコピーを書いています。

コードは、新しいワークブックを開き、新しいワークブックから特定のシートを取得し、利用可能なすべてのデータをコピーして、元のワークブックに貼り付けることです。私のコードは以下です

Dim wkb As Workbook
Dim wkbFrom As Workbook
Dim wks As Worksheet
Dim path As String, FilePart As String
Dim TheFile
Dim loc As String
Dim Lastrow As Long
Dim lr As Long
Dim lr2 As Long

FilePart = Trim(shPivot.Range("E17").Value)
TheFile = Dir(path & "*" & FilePart & ".xls")
Set wkbFrom = Workbooks.Open(loc & path & TheFile & FilePart)
Set wks = wkbFrom.Sheets(shPivot.Range("E15").Value)
Lastrow = ActiveSheet.UsedRange.Rows.Count
lr = shCurrentWeek.Range("A" & Rows.Count).End(xlUp).Row
lr2 = wks.Range("A" & Rows.Count).End(xlUp).Row
Debug.Print lr
Debug.Print lr2

'Copies range from report generated to share drive and pastes into the current week tab of open order report
wks.Range("A2:N" & lr2).Copy wkb.Sheets("Current Week").Range("A4")

'Closes the workbook in the shared drive
wkbFrom.Close False

「新しく開いたワークブック」に167行しかない場合でも、常に187行のデータを取得しています。データは列「A」から507行までいっぱいです。

4

1 に答える 1

1

一部のワークシートからデータの最後の行を自分で取得するのに問題がありました。これが私が現在使用しているコードで、私にとって最も一貫していると思われます。

objExcelWks.Range("A1").Offset(objExcelWks.Rows.Count - 1, 0).End(xlUp).Row

多分それはうまくいくでしょうか?

于 2012-11-20T19:47:08.110 に答える