Re: [算表] VBA如何寫入他檔特定儲存格
因為更新程式碼,用新文章比較好處理,如果傷眼,請見諒。
根據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
09/08 14:52, 1F
→
09/08 14:52,
7年前
, 2F
09/08 14:52, 2F
→
09/08 14:53,
7年前
, 3F
09/08 14:53, 3F
→
09/08 14:53,
7年前
, 4F
09/08 14:53, 4F
→
09/08 14:59,
7年前
, 5F
09/08 14:59, 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
09/08 15:27, 8F
→
09/08 16:45,
7年前
, 9F
09/08 16:45, 9F
討論串 (同標題文章)
Office 近期熱門文章
PTT數位生活區 即時熱門文章