標題:
[發問]
將voucher的資料寫入journal出錯
[打印本頁]
作者:
Hero2013
時間:
2015-3-17 15:05
標題:
將voucher的資料寫入journal出錯
各位大大,
小弟寫了1個簡單的VBA自用, 不過有問題解決不了:'(
問題1:
如果將以下這行
my_journal.Cells(i, 7) = my_voucher.Cells(Voucher_First_line, 3) + " " + my_voucher.Cells(Voucher_First_line, 4)
改成
my_journal.Cells(i, 7) = my_voucher.Cells(Voucher_First_line, 3) + " " + my_voucher.Cells(Voucher_First_line, 5)
或
my_journal.Cells(i, 7) = my_voucher.Cells(Voucher_First_line, 3) + " " + my_voucher.Cells(Voucher_First_line, 4)+ " " + my_voucher.Cells(Voucher_First_line, 5)
就出錯
問題2:
如果在Print那張sheet的 (9,1), (9,2)裡填入 1~3, 怎樣寫個VBA由Journal那張sheet的找尋相關資料, 然後逐張列印出來?
謝了
原Code以下
Sub add_data()
Dim count_num As Integer, s, t As Integer
Dim LastRow As Long
Set my_voucher = Worksheets("Voucher")
Set my_journal = Worksheets("Journal")
With my_voucher
.Activate
s = .Cells(Rows.Count, 1).End(xlUp).Row 'Voucher第1個column 由下向上的最後一行
End With
With my_journal
.Activate
t = .Cells(Rows.Count, 1).End(xlUp).Row 'Journal第1個column 由下向上的最後一行
End With
Journal_First_line = t + 1 'Journal第1個column 最後一行的下一行
count_num = s - 9 '總筆數=第1個column 最後一行減標題列行
Voucher_First_line = 10 'Voucher第1行是第10行
For i = Journal_First_line To Journal_First_line + count_num - 1 'Journal的第1行空格 至 Journal的第1行空格+voucher總行數-1
my_journal.Cells(i, 1) = my_voucher.Cells(4, 7) 'Voucher No
my_journal.Cells(i, 2) = my_voucher.Cells(Voucher_First_line, 1) 'Account Code
my_journal.Cells(i, 3) = my_voucher.Cells(Voucher_First_line, 2) 'Account Name
my_journal.Cells(i, 4) = my_voucher.Cells(5, 7) 'Date
my_journal.Cells(i, 5) = my_voucher.Cells(6, 7) 'Cheque No
my_journal.Cells(i, 6) = my_voucher.Cells(7, 7) 'Payer Name
my_journal.Cells(i, 7) = my_voucher.Cells(Voucher_First_line, 3) + " " + my_voucher.Cells(Voucher_First_line, 4) '合併Particular
my_journal.Cells(i, 8) = my_voucher.Cells(Voucher_First_line, 6) 'Dr
my_journal.Cells(i, 9) = my_voucher.Cells(Voucher_First_line, 7) 'Cr
Voucher_First_line = Voucher_First_line + 1
Next i
With my_voucher
.Activate
Cells(s + 1, 5).Value = "總數"
Cells(s + 1, 6).Formula = "=SUM(F10:F" & s + 1 - 1 & ")" '最後1行向右第4空位
Cells(s + 1, 7).Formula = "=SUM(G10:G" & s + 1 - 1 & ")" '最後1行向右第5空位
End With
End Sub
作者:
GBKEE
時間:
2015-3-17 16:03
回復
1#
Hero2013
my_journal.Cells(i, 7) = my_voucher.Cells(Voucher_First_line, 3) + " " + my_voucher.Cells(Voucher_First_line, 4)
Debug.Print my_voucher.Cells(Voucher_First_line, 3), my_voucher.Cells(Voucher_First_line, 4), my_voucher.Cells(Voucher_First_line, 5)
複製代碼
字串
+
字串 是可以的
字串
&
數字 才正確
請相看VBA
運算子摘要
的說明
[attach]20449[/attach]
作者:
Hero2013
時間:
2015-3-17 16:35
回復 Hero2013 字串 + 字串 是可以的
字串 & 數字 才正確
請相看VBA 運算子摘要 的說明
GBKEE 發表於 2015-3-17 16:03
GBKEE大大, 謝謝你.
問題2:
如果在Print那張sheet的 (9,1), (9,2)裡填入 1~3, 怎樣寫個VBA由Journal那張sheet的找尋相關資料, 然後逐張列印出來? (這個問題也困擾了許久)
作者:
GBKEE
時間:
2015-3-17 17:50
回復
3#
Hero2013
試試看
Option Explicit
Sub Ex()
Dim Ar(), Ay(), Rng As Range, i As Integer, R As Integer
With Sheets("print")
For i = .Range("G9").Value To .Range("H9").Value
With Sheets("Journal")
Set Rng = .Range("A5", .Range("A5").End(xlDown)).Find(i, LOOKAT:=xlWhole)
End With
If Not Rng Is Nothing Then
Do While Rng = i
R = R + 1
With Rng
ReDim Preserve Ar(1 To R)
Ar(R) = Array(.Range("B1").Value, .Range("C1").Value, .Range("G1").Value, .Range("H1").Value, .Range("I1").Value)
End With
Set Rng = Rng.Offset(1)
Loop
Ay = Application.Transpose(Ar)
R = R + 1
ReDim Preserve Ar(1 To R)
Ar(R) = Array("", "", "總 數:", Application.Sum(Application.Index(Ay, 4)), Application.Sum(Application.Index(Ay, 5)))
'Dr借項 Application.Index(Ay, 4)
'Cr貸項 Application.Index(Ay, 5)
Ar = Application.Transpose(Application.Transpose(Ar))
.[a10:e10].Resize(R).Value = Ar
.PageSetup.PrintArea = "A3:" & .[a10:e10].Resize(R).Address '設定印列範圍
.PrintOut '印列
Erase Ar '重新初始化固定大小陣列的元素,並釋放動態陣列的儲存空間
.[a10:e10].Resize(R) = ""
R = 0
End If
Next
End With
End Sub
複製代碼
作者:
Hero2013
時間:
2015-3-17 18:23
回復
4#
GBKEE
謝謝GBKEE老大,你真的太強了,感恩:)
作者:
Hero2013
時間:
2015-3-17 22:12
GBKEE老大,
不好意思, 我試了一下, 發現
Set Rng = .Range("A5", .Range("A5").End(xlDown)).Find(i, LOOKAT:=xlWhole)
列印第1張時會少了第一行
將A5改成A4就可以(雖然我不明白)
另外就是 "Print"那個sheet的 E4至E7沒有從"Journal"那個sheet提取相關資料
可以改一下嗎? 謝謝你了
作者:
GBKEE
時間:
2015-3-18 06:50
回復
6#
Hero2013
修改一下 After(參數):從最後開始搜尋
Set Rng = .Range("A5", .Range("A5").End(xlDown)).Find(i, LOOKAT:=xlWhole, After:=.Range("A5").End(xlDown))
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)