首页 > 解决方案 > 如何设置带有前缀和递增十进制值的 ID?

问题描述

我正在尝试调整我开发的电子表格应用程序。

当前应用程序使用用户表单在多个工作表中输入用户信息。但是,我没有将所有数据导入 SQL 数据库,而是想知道是否可以一一添加单个“行”。

其中一个工作表上的两列具有使用前缀设置的 ID(类似于 080219001),另一列具有增量值,其顺序为 1.1、1.2、2.1、2.2、3.1 等。

这是我为其中一张纸所拥有的代码

'Open the destination file in case it was closed
Set carb = grids.Sheets("Carbonation") 'Worksheet the data will be exported to
Set etiqc = grids.Sheets("Carb Labels") 'Worksheet used to print Carbonation labels

'Last Filled Row and First Empty Row
FR = carb.Range("A" & Rows.Count).End(xlUp).Row
ER = FR + 1

'Data exported to GridsDB.xslx
carb.Range("B" & ER).Value = Requip.Text 'Equipment reference
carb.Range("C" & ER).Value = Lmembranes.Text 'Membranes lot
carb.Range("E" & ER).Value = Val(Density.Value) 'Density of the carbonation
carb.Range("F" & ER).Value = Val(txt_temperature.Value) 'Carbonation temperature
carb.Range("G" & ER).Value = Val(txt_humidity.Value) 'Carbonation humidity
carb.Range("H" & ER).Value = puidc.Text 'PUID
carb.Range("I" & ER).Value = Format(Now, "dd/MM/yyyy") 'Date of carbonation
carb.Range("J" & ER).Value = Format(Now, "dd/MM/yyyy") 'Date for comparison purposes
carb.Range("K" & ER).Value = Format(Now, "HH:mm:ss") 'Time of carbonation
carb.Range("L" & ER).Value = txt_comments.Text 'Carbonation comments
carb.Range("M" & ER).Value = Val(mnok.Value) 'Qty NOK membranes
carb.Range("N" & ER).Value = motive.Text

'Carbonation ID
If carb.Range("J" & ER).Value = carb.Range("J" & FR).Value Then
    carb.Range("A" & ER).Value = carb.Range("A" & FR).Value + 1
Else
    carb.Range("A" & ER).Value = Replace(Date, "/", "") & "001"
End If

'Carbonation Number
If carb.Range("J" & ER).Value = carb.Range("J" & FR).Value Then
    carb.Range("D" & ER).Value = carb.Range("D" & FR).Value + 1
Else
    carb.Range("D" & ER).Value = 1
End If
End Sub

我正在使用此代码将用户数据输入到“碳化”表中。

我做了一些测试并设法将数据直接导入到 SQL 中的测试表中。我唯一的问题是“Carbonation ID”和“Carbonation Number”。

我很感激有关此的建议。

标签: sqlsql-serverexcelvba

解决方案


推荐阅读