VBA 避免枚举 copy/paste
VBA enumeration to avoid copy/paste
我不确定如何命名它,所以如果你能让它更具描述性,请随时更新。
这就是我想要做的。我有一个非常重复的代码,它具有非常可预测的变量模式,这会使每个实例都不同。
'Make sure decimal character is ","
Call SetLocalSetting(LOCALE_SDECIMAL, ",")
'_01 -------------------------------------------------------------
qty_01 = Val(ActiveDocument.Tables(5).Cell(2, 2).Range.Text)
'qty_01 is now ready to be used for calculations. Next is price_01.
price_01 = ActiveDocument.Tables(5).Cell(2, 3).Range.Text
price_01 = Replace(price_01, ",", ".")
price_01 = Format(Val(price_01), "##,##0.00")
'price_01 is now ready to be used for calculations. Next are price_01_cro and price_01_eng.
price_01_cro = price_01
price_01_eng = price_01
price_01_eng = Replace(price_01_eng, ",", "decSym")
price_01_eng = Replace(price_01_eng, ".", "thoSym")
price_01_eng = Replace(price_01_eng, "decSym", ".")
price_01_eng = Replace(price_01_eng, "thoSym", ",")
'price_01_cro and price_01_eng are now ready to be used for presentation.
'_02 -------------------------------------------------------------
qty_02 = Val(ActiveDocument.Tables(5).Cell(3, 2).Range.Text)
'qty_02 is now ready to be used for calculations. Next is price_02.
price_02 = ActiveDocument.Tables(5).Cell(3, 3).Range.Text
price_02 = Replace(price_02, ",", ".")
price_02 = Format(Val(price_02), "##,##0.00")
'price_02 is now ready to be used for calculations. Next are price_02_cro and price_02_eng.
price_02_cro = price_02
price_02_eng = price_02
price_02_eng = Replace(price_02_eng, ",", "decSym")
price_02_eng = Replace(price_02_eng, ".", "thoSym")
price_02_eng = Replace(price_02_eng, "decSym", ".")
price_02_eng = Replace(price_02_eng, "thoSym", ",")
'price_02_cro and price_02_eng are now ready to be used for presentation.
它持续了 11 次。有没有比一遍又一遍地复制相同的东西并更改数字更方便的方法?
顺便说一句,这是我构建发票生成器的持续努力。这就是为什么我不得不使用一些 hack 来应对 Word 在处理小数字符时的奇怪之处。
谢谢!
如果对您正在处理的数据没有更好的了解,我无法对此进行全面测试,但是如何将您的变量设置为数组并进行迭代呢?它看起来像这样:
Call SetLocalSetting(LOCALE_SDECIMAL, ",")
Dim qty(0 To 10) As Variant
Dim price(0 To 10) As Variant
Dim price_cro(0 To 10) As Variant
Dim price_eng(0 To 10) As Variant
For inum = 0 To 10
qty(inum) = Val(ActiveDocument.Tables(5).Cell(inum + 1, 2).Range.Text)
price(inum) = ActiveDocument.Tables(5).Cell(inum + 1, 3).Range.Text
price(inum) = Replace(price(inum), ",", ".")
price(inum) = Format(Val(price(inum)), "##,##0.00")
price_cro(inum) = price(inum)
price_eng(inum) = price(inum)
price_eng(inum) = Replace(price_eng(inum), ",", "decSym")
price_eng(inum) = Replace(price_eng(inum), ".", "thoSym")
price_eng(inum) = Replace(price_eng(inum), "decSym", ".")
price_eng(inum) = Replace(price_eng(inum), "thoSym", ",")
Next
我不确定如何命名它,所以如果你能让它更具描述性,请随时更新。
这就是我想要做的。我有一个非常重复的代码,它具有非常可预测的变量模式,这会使每个实例都不同。
'Make sure decimal character is ","
Call SetLocalSetting(LOCALE_SDECIMAL, ",")
'_01 -------------------------------------------------------------
qty_01 = Val(ActiveDocument.Tables(5).Cell(2, 2).Range.Text)
'qty_01 is now ready to be used for calculations. Next is price_01.
price_01 = ActiveDocument.Tables(5).Cell(2, 3).Range.Text
price_01 = Replace(price_01, ",", ".")
price_01 = Format(Val(price_01), "##,##0.00")
'price_01 is now ready to be used for calculations. Next are price_01_cro and price_01_eng.
price_01_cro = price_01
price_01_eng = price_01
price_01_eng = Replace(price_01_eng, ",", "decSym")
price_01_eng = Replace(price_01_eng, ".", "thoSym")
price_01_eng = Replace(price_01_eng, "decSym", ".")
price_01_eng = Replace(price_01_eng, "thoSym", ",")
'price_01_cro and price_01_eng are now ready to be used for presentation.
'_02 -------------------------------------------------------------
qty_02 = Val(ActiveDocument.Tables(5).Cell(3, 2).Range.Text)
'qty_02 is now ready to be used for calculations. Next is price_02.
price_02 = ActiveDocument.Tables(5).Cell(3, 3).Range.Text
price_02 = Replace(price_02, ",", ".")
price_02 = Format(Val(price_02), "##,##0.00")
'price_02 is now ready to be used for calculations. Next are price_02_cro and price_02_eng.
price_02_cro = price_02
price_02_eng = price_02
price_02_eng = Replace(price_02_eng, ",", "decSym")
price_02_eng = Replace(price_02_eng, ".", "thoSym")
price_02_eng = Replace(price_02_eng, "decSym", ".")
price_02_eng = Replace(price_02_eng, "thoSym", ",")
'price_02_cro and price_02_eng are now ready to be used for presentation.
它持续了 11 次。有没有比一遍又一遍地复制相同的东西并更改数字更方便的方法?
顺便说一句,这是我构建发票生成器的持续努力。这就是为什么我不得不使用一些 hack 来应对 Word 在处理小数字符时的奇怪之处。
谢谢!
如果对您正在处理的数据没有更好的了解,我无法对此进行全面测试,但是如何将您的变量设置为数组并进行迭代呢?它看起来像这样:
Call SetLocalSetting(LOCALE_SDECIMAL, ",")
Dim qty(0 To 10) As Variant
Dim price(0 To 10) As Variant
Dim price_cro(0 To 10) As Variant
Dim price_eng(0 To 10) As Variant
For inum = 0 To 10
qty(inum) = Val(ActiveDocument.Tables(5).Cell(inum + 1, 2).Range.Text)
price(inum) = ActiveDocument.Tables(5).Cell(inum + 1, 3).Range.Text
price(inum) = Replace(price(inum), ",", ".")
price(inum) = Format(Val(price(inum)), "##,##0.00")
price_cro(inum) = price(inum)
price_eng(inum) = price(inum)
price_eng(inum) = Replace(price_eng(inum), ",", "decSym")
price_eng(inum) = Replace(price_eng(inum), ".", "thoSym")
price_eng(inum) = Replace(price_eng(inum), "decSym", ".")
price_eng(inum) = Replace(price_eng(inum), "thoSym", ",")
Next