Re: [算表] VBA如何寫入他檔特定儲存格

看板Office作者 (溫柔殺手)時間7年前 (2018/09/08 14:44), 7年前編輯推噓0(009)
留言9則, 2人參與, 7年前最新討論串2/2 (看更多)
因為更新程式碼,用新文章比較好處理,如果傷眼,請見諒。 根據N大與S大的指導,我將程式碼更新如下。但是Excel還是不吃。 應該是有完成複製,但是沒有完成貼上。 Private Sub CommandButton2_Click() '=======================找尋絕對路徑與讀檔=========== Dim dpath As String Dim Fname As String Dim index_row As Integer Dim index_column As Integer Dim x As Integer Dim fileA As Workbook dpath = ThisWorkbook.Path MsgBox ThisWorkbook.Path Fname = Range("B1").Value Workbooks.Open Filename:=dpath & "\" & Fname & ".xlsx" Set fileA = Workbooks(Fname & ".xlsx") Application.Goto reference:=ActiveSheet.Range("A1") '儲存格起始座標指定 ActiveSheet.Range("A1").End(xlDown).Select '最後一筆資料定位 index_row = Selection.Row '最後一筆資料位置 index_column = Selection.Column '最後一筆資料位置 ThisWorkbook.Activate '回到A檔案 Range("B2").Copy With fileA '進到B檔案 Cells(index_row + 1, index_column).PasteSpecial End With '=================================================== End Sub ※ 引述《yimean (溫柔殺手)》之銘言: : 軟體:Excel : 版本:2013 : 小弟目前遇到兩個問題。 : 1.我要在A檔案執行VBA把A檔案的某特定儲存格寫入B檔案特定儲存格中。 : 2.我要在A檔案執行VBA把B檔案的某特定儲存格讀入A檔案特定儲存格中。 : 舉例: : A檔案worksheet1 : A B C : 1 我 是 誰 : 2 王 大 頭 : 3 知 道 嗎 : B檔案 : A B C D E F G H I : 1 我 是 誰 王 大 頭 知 道 嗎 : 取得檔案路徑我已經會了,可是我不知道應該如何寫入檔案。 : 我的核心問題是,我分不清楚此時我在A檔案還是在B檔案。 : 要如何在兩個檔案間切換。 : 下方是我的片段程式碼,煩請些助指導。 : Dim dpath As String : Dim Fname As String : Dim index_row As Integer : Dim index_column As Integer : Dim x As Integer : dpath = ThisWorkbook.Path : MsgBox ThisWorkbook.Path : Fname = Range("B1").Value : Workbooks.Open Filename:=dpath & "\" & Fname & ".xlsx" : Application.Goto reference:=ActiveSheet.Range("A1") : ActiveSheet.Range("A1").End(xlDown).Select '當我做這個動作時我應該是在B檔案了 : '那我如何切回A檔案把A檔案的資料複製起來貼到B檔案。 : 以上煩請協助,感謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.125.15.22 ※ 文章網址: https://www.ptt.cc/bbs/Office/M.1536389054.A.5FF.html ※ 編輯: yimean (59.125.15.22), 09/08/2018 14:49:21

09/08 14:52, 7年前 , 1F
cells(...).pastespecial改為 .activesheet.cells(...).
09/08 14:52, 1F

09/08 14:52, 7年前 , 2F
pastespecial
09/08 14:52, 2F

09/08 14:53, 7年前 , 3F
activesheet也可改為指定的worksheets或sheets的工作表名
09/08 14:53, 3F

09/08 14:53, 7年前 , 4F
09/08 14:53, 4F

09/08 14:59, 7年前 , 5F

09/08 15:09, 7年前 , 6F
感謝大大的指導,成功了。
09/08 15:09, 6F

09/08 15:24, 7年前 , 7F
不好意思,在請教一下,如果我只想貼上值,應該要怎麼做呢
09/08 15:24, 7F

09/08 15:27, 7年前 , 8F
range.pastespecial 後面參數為xlpastevalues
09/08 15:27, 8F

09/08 16:45, 7年前 , 9F
感恩
09/08 16:45, 9F
文章代碼(AID): #1Ras--N_ (Office)
討論串 (同標題文章)
文章代碼(AID): #1Ras--N_ (Office)