簡體   English   中英

VBA將數據復制到列

[英]VBA to copy data into Columns

我目前有這個代碼

Sheets("Pivot_Table_Non_Closed_Area").Range("E7:L7").Copy
'Pastes the data from the sheet above in the next avaliable row.
Sheets("Tracking_Table_Non_Closed_Area").Cells(Rows.Count, "C").End(xlUp).Offset(1). _
PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Sheets("Tracking_Table_Non_Closed_Area").Select
n = Cells(Rows.Count, "C").End(xlUp).Row
Range("A" & n) = Date
Range("B" & n) = Time

這是我當前代碼的顯示方式: https : //www.dropbox.com/s/p99kh0y3x2vsbo2/Currently_Presents.JPG?dl=0

但我似乎無法弄清楚如何將其從復制數據行和粘貼行更改為從數據列復制並粘貼到列中

這就是我希望新代碼呈現數據的方式: https : //www.dropbox.com/s/krkdjlculdqpckn/Wish_for_it_to_Be_Presented.JPG?dl=0

希望這有意義

編輯:這是我當前的代碼現在在所有幫助后的樣子,但是仍然在日期和時間上掙扎

Sheets("Pivot_Table_002").Range("B10:B19").Copy
Sheets("Sheet1").Cells(7, Columns.Count).End(xlToLeft).Offset(0, 1). _
        PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=False
n = Cells(7, Columns.Count).End(xlToLeft).Column
Range("A" & n) = Date
Range("B" & n) = Time

謝謝

編輯- DateTime

您正在使用Range設置DateTime的目的地,但是現在n代表最后占用的列,您需要更改該邏輯。 讓我們使用Cells構造,在這種情況下,我認為它讀起來更好:

Sheets("Tracking_Table_Non_Closed_Area").Cells(7, n) = Date Sheets("Tracking_Table_Non_Closed_Area").Cells(8, n) = Time

.Cells的工作方式如下:

.Cells(row_identifier, column_identifier)

有了這些,您應該已經准備就緒!

編輯- DateTime

讓我們對DateTime與對列式數據相同的策略。 原始設計執行以下操作:

n = Cells(Rows.Count, "C").End(xlUp).Row

那里到底發生了什么? n是一個數字。 具體地說, n是列“ C”中最后一個占用單元的行號。 我們感興趣的是得到一排的最后占領代替-比方說,堅持使用下面的例子,我們在7行中的最后一欄占據:

n = Cells(7, Columns.Count).End(xlToLeft).Column

繁榮! 現在n保留了最后占用的列號,您可以應用最后兩行中的相同策略,根據提供的屏幕快照在DateTime寫入。

初步答案:

我認為剖析您已經存在的代碼將對您有所幫助,所以讓我們開始吧!

復制/粘貼操作發生在以下兩行:

'This line does the copying
Sheets("Pivot_Table_Non_Closed_Area").Range("E7:L7").Copy

'This line does the pasting
Sheets("Tracking_Table_Non_Closed_Area").Cells(Rows.Count, "C").End(xlUp).Offset(1). _
        PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=False

(為了清楚起見,我在縮進行上添加了縮進,因為_是多行指示器。)

讓我們來談談副本:

Sheets("Pivot_Table_Non_Closed_Area") '<~ this specifies the worksheet
Range("E7:L7")                        '<~ this specifies the range, which is a row-ish
                                      '   group of cells from E7 to L7
Copy                                  '<~ this is the copy method

因此,如果您想使用一組列式單元格,則可以調整Range 為了舉例說明,假設您對從E7到E11的5個單元格的列式組感興趣。 如果要復制該組,請輸入:

Sheets("Pivot_Table_Non_Closed_Area").Range("E7:E11").Copy

真好! 現在,讓我們深入研究粘貼:

Sheets("Tracking_Table_Non_Closed_Area")         '<~ this specifies the worksheet
Cells(Rows.Count, "C").End(xlUp).Offset(1)       '<~ this starts in the last cell in
                                                 '   column C (Rows.Count = the count
                                                 '   of all the rows, i.e. 1 million-
                                                 '   ish in Excel 2007+ or 56K-ish in
                                                 '   Excel 2003). Then, .End(xlUp)
                                                 '   simulates hitting Ctrl + Up on
                                                 '   the keyboard, bringing you to the
                                                 '   last occupied cell in column C.
                                                 '   Finally, .Offset(1) increments
                                                 '   that location by 1 row, bringing
                                                 '   you to the cell immediately below
                                                 '   the last occupied cell in
                                                 '   column C.
PasteSpecial Paste:=xlPasteValues (then options) '<~ this does the pasting, with
                                                 '   values-only (along with some
                                                 '   other options, which aren't that
                                                 '   important here.

酷吧? 找到最后一個被占用的行並在其下面立即寫信息是VBA的基石,因此,我建議閱讀該主題的殺手kill 那么,如果您想粘貼我們復制的列式區域,該區域在第7行中最后一個被占用列的右邊一列上方? 我們可以這樣寫:

Sheets("Tracking_Table_Non_Closed_Area").Cells(7, Columns.Count).End(xlToLeft).Offset(0, 1). _
            PasteSpecial Paste:=xlPasteValues, _
            Operation:=xlNone, SkipBlanks:=False, Transpose:=False

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM