在Excel表格中,使用多个数组来相乘不同的范围。

huangapple go评论100阅读模式
英文:

Taking multiple arrays to multiply different ranges in excel sheet

问题

以下是您要翻译的代码部分:

  1. I am trying to multiply different ranges in an excel sheet and running them when the multiplier value changes.
  2. Sheet 1 has :
  3. | A | B | C | D |
  4. |---------|---------|---------|---------|
  5. | ABC | 10 | 20 | 2.5 |
  6. | DED | 5 | 15 | 1.7 |
  7. Sheet 2 has:
  8. Column A2: "5%" - Percentage value
  9. Column A4: "10%" - Percentage value
  10. I want to increase or decrease the value of B and C columns with 5% and D with 10%. Further, which will change and refresh the whole table in Sheet 1 when the values in Sheet 2 are changed.
  11. Here is the code I am using and it is working for B and C rows but not for the second array:
  12. With Sheet1
  13. Dim LastRow As Long
  14. LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  15. Dim arr() As Variant
  16. Dim arr2() As Variant
  17. arr = .Range("B2:C" & LastRow).Value
  18. arr2 = .Range("D2:D" & LastRow).Value
  19. End With
  20. GA1 = 1 + Sheet2.Range("A4").Value
  21. If Sheet2.Range("A4").Value <= 0 Then GA1 = 1
  22. GA2 = 1 + Sheet2.Range("A2").Value
  23. If Sheet2.Range("A2").Value <= 0 Then GA2 = 1
  24. Dim i As Long
  25. Dim m As Long
  26. For m = LBound(arr2, 1) To UBound(arr2, 1)
  27. arr2(m, 1) = arr2(m, 1) * GA1
  28. Next
  29. For i = LBound(arr, 1) To UBound(arr, 1)
  30. arr(i, 1) = arr(i, 1) * GA2
  31. arr(i, 2) = arr(i, 2) * GA2
  32. Next
  33. Sheet1.Range("B2").Resize(UBound(arr, 1), 2).Value = arr
  34. Sheet1.Range("D2").Resize(UBound(arr2)).Value = arr2
  35. String method:
  36. Sub IncreaseRangeTEST()
  37. Const SRC_FIRST_CELL As String = "A2"
  38. Const SRC_COLS_LIST As String = "B:C,D"
  39. Const LKP_CELLS_LIST As String = "A2,A4"
  40. Dim Sh1 As Worksheet
  41. Dim Sh2 As Worksheet
  42. Set wb1 = ActiveWorkbook
  43. Set Sh1 = wb1.Sheets("Sheet1")
  44. Set Sh2 = wb1.Sheets("Sheet2")
  45. Dim srg As Range, srCount As Long
  46. With Sht1.Range(SRC_FIRST_CELL)
  47. srCount = .Worksheet.Cells( _
  48. .Worksheet.Rows.Count, .Column).End(xlUp).Row - .Row + 1
  49. If srCount < 1 Then Exit Sub ' no data
  50. Set srg = .Resize(srCount)
  51. End With
  52. Dim sCols() As String: sCols = Split(SRC_COLS_LIST, ",")
  53. Dim lCells() As String: lCells = Split(LKP_CELLS_LIST, ",")
  54. Dim rg As Range, Percentage, n As Long
  55. For n = 0 To UBound(sCols)
  56. Percentage = Sht2.Range(lCells(n)).Value
  57. If VarType(Percentage) = vbDouble Then
  58. If Percentage > 0 Then
  59. Set rg = srg.EntireRow.Columns(sCols(n))
  60. IncreaseRange rg, Percentage
  61. End If
  62. End If
  63. Next n
  64. End Sub
  65. Sub IncreaseRange( _
  66. ByVal rg As Range, _
  67. ByVal Percentage As Double)
  68. Dim rCount As Long: rCount = rg.Rows.Count
  69. Dim cCount As Long: cCount = rg.Columns.Count
  70. Dim Data()
  71. If rCount * cCount = 1 Then
  72. ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rg.Value
  73. Else
  74. Data = rg.Value
  75. End If
  76. Dim Factor As Double: Factor = 1 + Percentage
  77. Dim Value, r As Long, c As Long
  78. For r = 1 To rCount
  79. For c = 1 To cCount
  80. Value = Data(r, c)
  81. If VarType(Value) = vbDouble Then
  82. Data(r, c) = Data(r, c) * Factor
  83. End If
  84. Next c
  85. Next r
  86. rg.Value = Data
  87. End Sub
英文:

I am trying to multiply different ranges in an excel sheet and running them when the multiplier value changes.
Sheet 1 has :

A B C D
ABC 10 20 2.5
DED 5 15 1.7

Sheet 2 has:

Column A2 : "5%" - Percentage value
Column A4 : "10%" - Percentage value

I want to increase or decrease the value of B and C columns with 5% and D with 10%. Further, which will change and refresh the whole table in Sheet 1 when the values in Sheet 2 are changed.

Here is the code I am using and it is working for B and C rows but not for the second array:

  1. With Sheet1
  2. Dim LastRow As Long
  3. LastRow = .Cells(.Rows.Count, &quot;A&quot;).End(xlUp).Row
  4. Dim arr() As Variant
  5. Dim arr2() As Variant
  6. arr = .Range(&quot;B2:C&quot; &amp; LastRow).Value
  7. arr2 = .Range(&quot;D2:D&quot; &amp; LastRow).Value
  8. End With
  9. GA1 = 1 + Sheet2.Range(&quot;A4&quot;).Value
  10. If Sheet2.Range(&quot;A4&quot;).Value &lt;= 0 Then GA1 = 1
  11. GA2 = 1 + Sheet2.Range(&quot;A2&quot;).Value
  12. If Sheet2.Range(&quot;A2&quot;).Value &lt;= 0 Then GA2 = 1
  13. Dim i As Long
  14. Dim m As Long
  15. For m = LBound(arr2, 1) To UBound(arr2, 1)
  16. arr2(m, 1) = arr2(m, 1) * GA1
  17. Next
  18. For i = LBound(arr, 1) To UBound(arr, 1)
  19. arr(i, 1) = arr(i, 1) * GA2
  20. arr(i, 2) = arr(i, 2) * GA2
  21. Next
  22. Sheet1.Range(&quot;B2&quot;).Resize(UBound(arr, 1), 2).Value = arr
  23. Sheet1.Range(&quot;D2&quot;).Resize(UBound(arr2)).Value = arr2

String method:

  1. Sub IncreaseRangeTEST()
  2. Const SRC_FIRST_CELL As String = &quot;A2&quot;
  3. Const SRC_COLS_LIST As String = &quot;B:C,D&quot;
  4. Const LKP_CELLS_LIST As String = &quot;A2,A4&quot;
  5. Dim Sh1 As Worksheet
  6. Dim Sh2 As Worksheet
  7. Set wb1 = ActiveWorkbook
  8. Set Sh1 = wb1.Sheets(&quot;Sheet1&quot;)
  9. Set Sh2 = wb1.Sheets(&quot;Sheet2&quot;)
  10. Dim srg As Range, srCount As Long
  11. With Sht1.Range(SRC_FIRST_CELL)
  12. srCount = .Worksheet.Cells( _
  13. .Worksheet.Rows.Count, .Column).End(xlUp).Row - .Row + 1
  14. If srCount &lt; 1 Then Exit Sub &#39; no data
  15. Set srg = .Resize(srCount)
  16. End With
  17. Dim sCols() As String: sCols = Split(SRC_COLS_LIST, &quot;,&quot;)
  18. Dim lCells() As String: lCells = Split(LKP_CELLS_LIST, &quot;,&quot;)
  19. Dim rg As Range, Percentage, n As Long
  20. For n = 0 To UBound(sCols)
  21. Percentage = Sht2.Range(lCells(n)).Value
  22. If VarType(Percentage) = vbDouble Then
  23. If Percentage &gt; 0 Then
  24. Set rg = srg.EntireRow.Columns(sCols(n))
  25. IncreaseRange rg, Percentage
  26. End If
  27. End If
  28. Next n
  29. End Sub
  30. Sub IncreaseRange( _
  31. ByVal rg As Range, _
  32. ByVal Percentage As Double)
  33. Dim rCount As Long: rCount = rg.Rows.Count
  34. Dim cCount As Long: cCount = rg.Columns.Count
  35. Dim Data()
  36. If rCount * cCount = 1 Then
  37. ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rg.Value
  38. Else
  39. Data = rg.Value
  40. End If
  41. Dim Factor As Double: Factor = 1 + Percentage
  42. Dim Value, r As Long, c As Long
  43. For r = 1 To rCount
  44. For c = 1 To cCount
  45. Value = Data(r, c)
  46. If VarType(Value) = vbDouble Then
  47. Data(r, c) = Data(r, c) * Factor
  48. End If
  49. Next c
  50. Next r
  51. rg.Value = Data
  52. End Sub

答案1

得分: 1

以下是翻译好的代码部分:

  1. Sub IncreaseRangeTEST()
  2. Const SRC_FIRST_CELL As String = "A2"
  3. Const SRC_COLS_LIST As String = "B:C,D"
  4. Const LKP_CELLS_LIST As String = "A2,A4"
  5. Dim srg As Range, srCount As Long
  6. With Sheet1.Range(SRC_FIRST_CELL)
  7. srCount = .Worksheet.Cells( _
  8. .Worksheet.Rows.Count, .Column).End(xlUp).Row - .Row + 1
  9. If srCount < 1 Then Exit Sub ' no data
  10. Set srg = .Resize(srCount)
  11. End With
  12. Dim sCols() As String: sCols = Split(SRC_COLS_LIST, ",")
  13. Dim lCells() As String: lCells = Split(LKP_CELLS_LIST, ",")
  14. Dim rg As Range, Percentage, n As Long
  15. For n = 0 To UBound(sCols)
  16. Percentage = Sheet2.Range(lCells(n)).Value
  17. If VarType(Percentage) = vbDouble Then
  18. If Percentage > 0 Then
  19. Set rg = srg.EntireRow.Columns(sCols(n))
  20. IncreaseRange rg, Percentage
  21. End If
  22. End If
  23. Next n
  24. End Sub
  25. Sub IncreaseRange( _
  26. ByVal rg As Range, _
  27. ByVal Percentage As Double)
  28. Dim rCount As Long: rCount = rg.Rows.Count
  29. Dim cCount As Long: cCount = rg.Columns.Count
  30. Dim Data()
  31. If rCount * cCount = 1 Then
  32. ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rg.Value
  33. Else
  34. Data = rg.Value
  35. End If
  36. Dim Factor As Double: Factor = 1 + Percentage
  37. Dim Value, r As Long, c As Long
  38. For r = 1 To rCount
  39. For c = 1 To cCount
  40. Value = Data(r, c)
  41. If VarType(Value) = vbDouble Then
  42. Data(r, c) = Data(r, c) * Factor
  43. End If
  44. Next c
  45. Next r
  46. rg.Value = Data
  47. End Sub

请注意,我只提供了代码的翻译部分,没有包括问题的回答。

英文:

Increase by Percentage

在Excel表格中,使用多个数组来相乘不同的范围。

The Example

<!-- language: lang-vb -->

  1. Sub IncreaseRangeTEST()
  2. Const SRC_FIRST_CELL As String = &quot;A2&quot;
  3. Const SRC_COLS_LIST As String = &quot;B:C,D&quot;
  4. Const LKP_CELLS_LIST As String = &quot;A2,A4&quot;
  5. Dim srg As Range, srCount As Long
  6. With Sheet1.Range(SRC_FIRST_CELL)
  7. srCount = .Worksheet.Cells( _
  8. .Worksheet.Rows.Count, .Column).End(xlUp).Row - .Row + 1
  9. If srCount &lt; 1 Then Exit Sub &#39; no data
  10. Set srg = .Resize(srCount)
  11. End With
  12. Dim sCols() As String: sCols = Split(SRC_COLS_LIST, &quot;,&quot;)
  13. Dim lCells() As String: lCells = Split(LKP_CELLS_LIST, &quot;,&quot;)
  14. Dim rg As Range, Percentage, n As Long
  15. For n = 0 To UBound(sCols)
  16. Percentage = Sheet2.Range(lCells(n)).Value
  17. If VarType(Percentage) = vbDouble Then
  18. If Percentage &gt; 0 Then
  19. Set rg = srg.EntireRow.Columns(sCols(n))
  20. IncreaseRange rg, Percentage
  21. End If
  22. End If
  23. Next n
  24. End Sub

The Method

<!-- language: lang-vb -->

  1. Sub IncreaseRange( _
  2. ByVal rg As Range, _
  3. ByVal Percentage As Double)
  4. Dim rCount As Long: rCount = rg.Rows.Count
  5. Dim cCount As Long: cCount = rg.Columns.Count
  6. Dim Data()
  7. If rCount * cCount = 1 Then
  8. ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rg.Value
  9. Else
  10. Data = rg.Value
  11. End If
  12. Dim Factor As Double: Factor = 1 + Percentage
  13. Dim Value, r As Long, c As Long
  14. For r = 1 To rCount
  15. For c = 1 To cCount
  16. Value = Data(r, c)
  17. If VarType(Value) = vbDouble Then
  18. Data(r, c) = Data(r, c) * Factor
  19. End If
  20. Next c
  21. Next r
  22. rg.Value = Data
  23. End Sub

答案2

得分: 0

将这部分代码放入Sheet2工作表中。它将在单元格A2和A4的更改时触发。如果您需要使用数组而不是范围,在选择情况下,我们可以将范围更改为数组并将其传递给IncreaseRange过程,将for each循环更改为多层循环以扫描2D数组。

  1. Option Explicit
  2. Private Sub IncreaseRange(ByVal oMultiplier As Double, ByRef oTarget As Range)
  3. Dim oValue As Variant
  4. For Each oValue In oTarget
  5. oValue.Value = oValue.Value * (1 + oMultiplier)
  6. Next
  7. End Sub
  8. Private Sub Worksheet_Change(ByVal Target As Range)
  9. Dim LastRow As Long
  10. LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
  11. Select Case Target.Address
  12. Case "$A$2"
  13. Call IncreaseRange(Target.Value, Sheet1.Range("B2:C" & LastRow))
  14. Case "$A$4"
  15. Call IncreaseRange(Target.Value, Sheet1.Range("D2:D" & LastRow))
  16. End Select
  17. End Sub
英文:

Toss this in your Sheet2 worksheet. It will trigger on the change of cells A2 & A4. If you need to use arrays instead of the range, in the select case we can change the range to an array & pass it to the IncreaseRange procedure and the for each loop to a multi-layered loop to scan a 2d array.

  1. Option Explicit
  2. Private Sub IncreaseRange(ByVal oMultiplier As Double, ByRef oTarget As Range)
  3. Dim oValue As Variant
  4. For Each oValue In oTarget
  5. oValue.Value = oValue.Value * (1 + oMultiplier)
  6. Next
  7. End Sub
  8. Private Sub Worksheet_Change(ByVal Target As Range)
  9. Dim LastRow As Long
  10. LastRow = Sheet1.Cells(Sheet1.Rows.Count, &quot;A&quot;).End(xlUp).Row
  11. Select Case Target.Address
  12. Case &quot;$A$2&quot;
  13. Call IncreaseRange(Target.Value, Sheet1.Range(&quot;B2:C&quot; &amp; LastRow))
  14. Case &quot;$A$4&quot;
  15. Call IncreaseRange(Target.Value, Sheet1.Range(&quot;D2:D&quot; &amp; LastRow))
  16. End Select
  17. End Sub

huangapple
  • 本文由 发表于 2023年6月5日 13:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定