[下面更新] 我正在掌握VBA的基础知识并自动化了一些东西,但是,我认为我可以提高自己的水平,因此我有一个问题。
我有一本Excel工作簿,其中每天我都会使用功能查询在一张纸上(“源”)更新许多银行余额。在第二张纸(“ ThisWeek”)上,我将整周的所有银行余额合并在一起。
数据如下所示: 来源:
|---------------------|------------------|--------------------|------------------|
| Account Code | Entity Code | Balance Yesterday | Balance Today |
|---------------------|------------------|--------------------|------------------|
| Account 1 | Account Holder | Value 1 | Value 2 |
|---------------------|------------------|--------------------|------------------|
ThisWeek
| Monday |
|---------------------|------------------|--------------------|------------------|
| Account Code | Entity Code | Date Yesterday | Date Today |
|---------------------|------------------|--------------------|------------------|
| Account 1 | Account name |*Insert value 1 here| and Value 2 here |
|---------------------|------------------|--------------------|------------------|
|Tuesday |
|---------------------|------------------|--------------------|------------------|
| Account Code | Entity Code | Date Yesterday | Date Today |
|---------------------|------------------|--------------------|------------------|
| Account 1 | Account name |*Insert value 1 here| and Value 2 here |
|---------------------|------------------|--------------------|------------------|
最后,我希望宏从源工作表复制每日余额,并将其粘贴到我的每周概览中的正确日期。 我还没有考虑在正确的日期进行粘贴(即“ ThisWeek”中从星期三到星期三的每日余额,稍后会开始使用
两张纸上的共同点是“帐户1”,“帐户2”等。
我现在为每个帐户提供以下代码行,效果不错,但与银行帐户一样,我拥有的代码块也很多。同样不确定我是否必须每次都激活工作表,或者是否也可以简化它。 一些注意事项: LastRow =“来源”表中具有银行余额的最后一行。此外,我现在为每个银行帐户创建了多个变量(即变量=帐户1)。 “余额”变量捕获“值1”和“值2”(每个循环中特定帐户的银行余额)。
此刻的“ j”只是看在我周表的第一天,因为我还没有弄清楚如何使其引用今天的日期并将其与“ ThisWeek”中的相关部分进行比较
Source.Activate
For i = 2 To LastRow
If Cells(i, 1).Value = Account 1 Then
Set Balance = Range("C" & i, "D" & i)
ThisWeek.Activate
For j = 4 To 26
If Cells(j, 1).Value = Account 1 Then
Range("C" & j, "D" & j) = Balance.Value
End If
Next j
End If
Next i
我当时想我可以在ThisWeek中设置一个范围,该范围将捕获所有相关帐户(“来源”包含比我感兴趣的更多余额)并执行以下操作。 这样做的好处是,我不必再声明所有个人帐户了,并且可以减少代码块。
Set Accounts = ThisWeek.Range("A2:A26")
For Each Cell In Accounts
For i = 2 To LastRow
If Cell.Value = Source.Cells(i, 1) Then
Set Balance = Source.Range("C" & i, "D" & i)
ThisWeek.Activate
If Cell.Value = Source.Cells(i, 1) Then
ThisWeek.Range("C" & i, "D" & i) = Balance.Value
End If
End If
Next i
Next
这确实将一些余额复制到我的“ ThisWeek”工作表中,但是,并没有将它们粘贴到正确的帐户之后。 (即,我获得的“帐户1”具有“来源”表中帐户7的值)。
有关如何将权利余额与正确的帐户对齐的任何帮助吗?
很抱歉,很长的帖子。
预先感谢, 托马斯
[更新] 似乎几乎已找到它,请参见下文。 唯一的问题是我的变量“余额”应包含2个值(即“来源”的昨天+今天的余额)。但是,当偏移活动单元格时,它仅粘贴1个值? 如何抵消单元格的“范围”?那可能吗?还是应该创建另一个变量和“偏移”两次?
Sub ImportBalances()
Dim wb As Workbook
Dim Source As Worksheet
Dim ThisWeek As Worksheet
Dim LastRow As Integer
Dim i As Integer
Dim j As Integer
Dim Balance As Range
Dim Accounts As Range
Dim Account As String
Dim Today As Date
Dim Monday As Date
Dim Tuesday As Date
Dim Wednesday As Date
Dim Thursday As Date
Dim Friday As Date
'Set workbook + worksheet
Set wb = ActiveWorkbook
Set Source = Sheets("Balances from Buitend.htm")
Set ThisWeek = Sheets("ThisWeek")
'Count number of rows with data in Source
LastRow = Source.Cells(Rows.Count, "A").End(xlUp).Row
'Define days of the week
With ThisWeek
' Today = Date
Today = DateValue("Jul 10, 2020") 'This is for testing
Monday = Range("A2")
Tuesday = Range("A30")
Wednesday = Range("A58")
Thursday = Range("A86")
Friday = Range("A114")
End With
'Copy balances
'Monday
If Today = Monday Then
Set Accounts = ThisWeek.Range("A4:A26") 'Refers to accounts listed in Column A under 'Monday' on 'ThisWeek'
For Each Cell In Accounts
Account = Cell.Value
For i = 2 To LastRow
If Source.Cells(i, 1).Value = Account Then
Set Balance = Source.Range("C" & i, "D" & i)
Cell.Offset(0, 2) = Balance.Value
End If
Next i
Next
'Tuesday
ElseIf Today = Tuesday Then
Set Accounts = ThisWeek.Range("A32:A54") 'Refers to accounts listed in Column A under 'Tuesday' on 'ThisWeek'
For Each Cell In Accounts
Account = Cell.Value
For i = 2 To LastRow
If Source.Cells(i, 1).Value = Account Then
Set Balance = Source.Range("C" & i, "D" & i)
Cell.Offset(0, 2) = Balance.Value
End If
Next i
Next
'Wednesday
ElseIf Today = Wednesday Then
Set Accounts = ThisWeek.Range("A60:A82") 'Refers to accounts listed in Column A under 'Wednesday' on 'ThisWeek'
For Each Cell In Accounts
Account = Cell.Value
For i = 2 To LastRow
If Source.Cells(i, 1).Value = Account Then
Set Balance = Source.Range("C" & i, "D" & i)
Cell.Offset(0, 2) = Balance.Value
End If
Next i
Next
'Thursday
ElseIf Today = Thursday Then
Set Accounts = ThisWeek.Range("A88:A110") 'Refers to accounts listed in Column A under 'Thursday' on 'ThisWeek'
For Each Cell In Accounts
Account = Cell.Value
For i = 2 To LastRow
If Source.Cells(i, 1).Value = Account Then
Set Balance = Source.Range("C" & i, "D" & i)
Cell.Offset(0, 2) = Balance.Value
End If
Next i
Next
ElseIf Today = Friday Then
Set Accounts = ThisWeek.Range("A116:A138") 'Refers to accounts listed in Column A under 'Friday' on 'ThisWeek'
For Each Cell In Accounts
Account = Cell.Value
For i = 2 To LastRow
If Source.Cells(i, 1).Value = Account Then
Set Balance = Source.Range("C" & i, "D" & i)
Cell.Offset(0, 2) = Balance.Value
End If
Next i
Next
End If
End Sub