VBA 生成二维码的代码,但是代码从哪里开始呢?
VBA code for generating QR codes, but where does the code begins?
为了工作,我必须生成一些包含信息的二维码。
因此,我在网上查了一下,发现了这个 "already made" QR code generator :
https://github.com/JonasHeidelberg/barcode-vba-macro-only
非常好,效果很好。
我正在尝试将其集成到我的 VBA sheet 中,以便在使用代码创建最终二维码之前进行数据处理。 (没什么复杂的)
这是它的样子:
4 个单元格 "B4 to B6" 获取编码或未编码的条目数据,具体取决于复选框,然后将结果写入 D 列。
每个cell的内容都堆在一个变量里,这个变量是给二维码生成器吃的:
Public Function GenerateQRCode()
Dim CurrentWS As String
UserDataRange = "B6:B9" 'The cells in which the data to be encoded are stored
InputDataRange = "D6:D9" 'the cells with the encoded (or not) values
InputCell = "A4" 'the cell where the text to be encoded in the QR code has to be put
'encode the text depending on hte value of the cell behind the chek boxes
For Each cell In Range(UserDataRange)
If cell.Offset(0, 1) = True Then
EncodedText = EncodeDecode.Base64EncodeString(cell.Value)
cell.Offset(0, 2).Value = EncodedText
ElseIf cell.Offset(0, 1) = False Then
cell.Offset(0, 2).Value = cell.Value
End If
Next
Range(InputCell).ClearContents
DataToEncode = ""
'puts the text in the input line with dashes between each value
For Each cell In Range(InputDataRange)
pouet = Range(InputDataRange).Address
If DataToEncode = "" Then
DataToEncode = cell.Value & Chr(10)
Else
If cell.Address = Mid(Range(InputDataRange).Address, InStr(1, Range(InputDataRange).Address, ":") + 1, _
Len(Range(InputDataRange).Address) - (InStr(1, Range(InputDataRange).Address, ":") - 1)) Then
DataToEncode = DataToEncode & cell.Value
Else
DataToEncode = DataToEncode & cell.Value & Chr(10)
End If
End If
Next
Range(InputCell).Value = DataToEncode
End Function
我担心的是 "whatever cell I modify in the whole workbook, it lunches the QR code generator."
我想像 If cell A4 is modified, lunch the code
一样在代码的开头给出一个条件,但我什至没有理解是什么让代码开始以及如何收集数据...
我最好的猜测是这是代码的开头:
Public Function EncodeBarcode(ShIx As Integer, xAddr As String, _
code As String, pbctype%, Optional pgraficky%, _
Optional pparams%, Optional pzones%) As String
Dim s$, bctype%, graficky%, params%, zones%
Dim oo As Object
Call Init
If IsMissing(pzones) Then zones = 2 Else zones = pzones
If IsMissing(pparams) Then params = 0 Else params = pparams
If IsMissing(pgraficky) Then graficky = 1 Else graficky = pgraficky
If IsMissing(pbctype) Then bctype = 0 Else bctype = pbctype
但是它是如何开始的呢? O.o
我认为像 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
这样的行后面跟着对 "target" 的值的控制是强制性的......
在这里,它对我来说就像魔法:(
xAddr
如何获取我单击的单元格的地址?再次变魔术...
我希望只有在单击我创建的按钮时才会执行代码。 (它会产生一些无限循环并且 excel 关闭 :/ )
或者,如果不可能,我希望它仅在单元格 A4 中的数据被修改时执行。
感谢您的帮助:)
这是因为 CELL function 函数是 volatile 它会在 every 计算时重新计算(不仅在单元格中计算它取决于)。这意味着 EncodeBarcode()
也计算 每个 计算(因为它在其参数中使用 CELL()
函数)。
A Volatile Function is one that causes recalculation of the formula in the cell where it resides every time Excel recalculates.
This occurs regardless of whether the precedent data and formulas on which the formula depends have changed, or whether the formula also contains non-volatile functions.
如果删除 CELL()
函数并将其替换为硬值
=EncodeBarcode(1;"B4";A4;51;1;0;2)
它不会在每次单元格更改时重新计算条形码。但它只会在它依赖的单元格发生变化时才重新计算条形码(本例中的单元格 A4)。
为了工作,我必须生成一些包含信息的二维码。 因此,我在网上查了一下,发现了这个 "already made" QR code generator :
https://github.com/JonasHeidelberg/barcode-vba-macro-only
非常好,效果很好。
我正在尝试将其集成到我的 VBA sheet 中,以便在使用代码创建最终二维码之前进行数据处理。 (没什么复杂的)
这是它的样子:
Public Function GenerateQRCode()
Dim CurrentWS As String
UserDataRange = "B6:B9" 'The cells in which the data to be encoded are stored
InputDataRange = "D6:D9" 'the cells with the encoded (or not) values
InputCell = "A4" 'the cell where the text to be encoded in the QR code has to be put
'encode the text depending on hte value of the cell behind the chek boxes
For Each cell In Range(UserDataRange)
If cell.Offset(0, 1) = True Then
EncodedText = EncodeDecode.Base64EncodeString(cell.Value)
cell.Offset(0, 2).Value = EncodedText
ElseIf cell.Offset(0, 1) = False Then
cell.Offset(0, 2).Value = cell.Value
End If
Next
Range(InputCell).ClearContents
DataToEncode = ""
'puts the text in the input line with dashes between each value
For Each cell In Range(InputDataRange)
pouet = Range(InputDataRange).Address
If DataToEncode = "" Then
DataToEncode = cell.Value & Chr(10)
Else
If cell.Address = Mid(Range(InputDataRange).Address, InStr(1, Range(InputDataRange).Address, ":") + 1, _
Len(Range(InputDataRange).Address) - (InStr(1, Range(InputDataRange).Address, ":") - 1)) Then
DataToEncode = DataToEncode & cell.Value
Else
DataToEncode = DataToEncode & cell.Value & Chr(10)
End If
End If
Next
Range(InputCell).Value = DataToEncode
End Function
我担心的是 "whatever cell I modify in the whole workbook, it lunches the QR code generator."
我想像 If cell A4 is modified, lunch the code
一样在代码的开头给出一个条件,但我什至没有理解是什么让代码开始以及如何收集数据...
我最好的猜测是这是代码的开头:
Public Function EncodeBarcode(ShIx As Integer, xAddr As String, _
code As String, pbctype%, Optional pgraficky%, _
Optional pparams%, Optional pzones%) As String
Dim s$, bctype%, graficky%, params%, zones%
Dim oo As Object
Call Init
If IsMissing(pzones) Then zones = 2 Else zones = pzones
If IsMissing(pparams) Then params = 0 Else params = pparams
If IsMissing(pgraficky) Then graficky = 1 Else graficky = pgraficky
If IsMissing(pbctype) Then bctype = 0 Else bctype = pbctype
但是它是如何开始的呢? O.o
我认为像 Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, cancel As Boolean)
这样的行后面跟着对 "target" 的值的控制是强制性的......
在这里,它对我来说就像魔法:(
xAddr
如何获取我单击的单元格的地址?再次变魔术...
我希望只有在单击我创建的按钮时才会执行代码。 (它会产生一些无限循环并且 excel 关闭 :/ ) 或者,如果不可能,我希望它仅在单元格 A4 中的数据被修改时执行。
感谢您的帮助:)
这是因为 CELL function 函数是 volatile 它会在 every 计算时重新计算(不仅在单元格中计算它取决于)。这意味着 EncodeBarcode()
也计算 每个 计算(因为它在其参数中使用 CELL()
函数)。
A Volatile Function is one that causes recalculation of the formula in the cell where it resides every time Excel recalculates. This occurs regardless of whether the precedent data and formulas on which the formula depends have changed, or whether the formula also contains non-volatile functions.
如果删除 CELL()
函数并将其替换为硬值
=EncodeBarcode(1;"B4";A4;51;1;0;2)
它不会在每次单元格更改时重新计算条形码。但它只会在它依赖的单元格发生变化时才重新计算条形码(本例中的单元格 A4)。