格式化单元格而不直接覆盖内容

时间:2019-01-10 09:56:54

标签: excel vba excel-formula formatting

我有一个VBA宏,它基本上根据数据库中存在的日期生成数据和行重复,我无法做的一件事就是格式化用户的唯一ID,理想情况下,我必须添加前导零和删除-字符,最长的id具有10位数字,最短的8位。

当前数据与应显示的内容:
Current data vs What It should show

问题在于何时粘贴数据之前,我运行以下excel公式来覆盖单元格中的数据。将单元格保留为0,并保存公式。我知道公式只是粘贴到单元格中并删除了旧内容。

=CONCAT(IF(LEN(SUBSTITUTE(G18,"-","") )<9,"00","0"),SUBSTITUTE(G18,"-","") )

这是宏的代码段

Private Sub duplicadorLicMed()

Set Application = CreateObject("Excel.Application")

Dim planillaDestino As Worksheet
Set planillaDestino = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
planillaDestino.Name = "hojaDest"

Dim planillaFuente As Worksheet
Set planillaFuente = Application.Workbooks.Open("tstfl.xlsm")
Set planillaFuente = ThisWorkbook.Worksheets(1)
planillaFuente.Name = "hojaFuente"

Dim filaFuenteUltima As Long
filaFuenteUltima = planillaFuente.Cells(planillaFuente.Rows.Count, "B").End(xlUp).Row

Dim filaIndiceFuente As Long

Dim filaIndiceDestino As Long
filaIndiceDestino = 1 ' salto de lineas y encabezado

Dim fechaInicio As Variant
Dim fechaFin As Variant
Dim fechaIndice As Date

    For filaIndiceFuente = 2 To filaFuenteUltima
    fechaInicio = planillaFuente.Cells(filaIndiceFuente, "L").Value
    fechaFin = planillaFuente.Cells(filaIndiceFuente, "M").Value

    ' VALIDATOR OF DATA

    For fechaIndice = fechaInicio To fechaFin
        filaIndiceDestino = filaIndiceDestino + 1

    ' ROWS GET REPEATED HERE

    Next fechaIndice
Next filaIndiceFuente

'HERE I RUN THE FORMAT FORMULAS

planillaDestino.Range("B2:B" & filaIndiceDestino).Formula = "=CONCAT(YEAR(L2),IF(INT(MONTH(L2))<10,0,""""),MONTH(L2))" ' per pro
planillaDestino.Range("B2:B" & filaIndiceDestino).Formula = "=CONCAT(IF(LEN(SUBSTITUTE(C2,"-","") )<9,"00","0"),SUBSTITUTE(C2,"-","") )" ' digitador rut
planillaDestino.Range("K2:K" & filaIndiceDestino).Formula = "=ABS(DAYS(L2,M2))+1" ' dias totales

MsgBox "PROCESO COMPLETO"

' End If
End Sub

那么我该如何在处理数据的同时“存储”而不丢失内容呢?

2 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么您想使用单元格格式使您在第一列(15489651-2)中提供的单元格值看起来像最后一列(0154896512)。据我所知这是不可能的,因为值“ 15489651-2”将始终被解释为文本字符串,并且无法通过格式化单元格来进行复杂的字符串操作(如“查找”和“替换”)。

由于使用单元格格式“ ########-将纯数字(154896512)或带前导零的字符串(0154896512)转换为带连字符(15489651-2)的格式非常容易” #”或函数

=TEXT(A1,"########-#")

我建议检查是否可以采用反向方法。您可以将数据更改为纯数字格式,以符合您的期望。每当需要包含连字符(用于外观或其他宏)时,只需使用TEXT公式将其更改(返回)为该格式即可。

关于单元格格式,这里是常规资源: https://www.ablebits.com/office-addins-blog/2016/07/07/custom-excel-number-format/#Repeat-characters

答案 1 :(得分:0)

首先删除“-”,输入数字,然后将数字设置为10位并保存数据。使用格式功能。然后,您可以将单元格设置为文本格式,然后将数据移动到单元格中。这可以通过vba完成。

'HERE I RUN THE FORMAT FORMULAS
Dim rngDB As Range
Dim vDB As Variant, i As Long, r As Long

planillaDestino.Range("B2:B" & filaIndiceDestino).Formula = "=CONCAT(YEAR(L2),IF(INT(MONTH(L2))<10,0,""""),MONTH(L2))" ' per pro
Set rngDB = planillaDestino.Range("B2:B" & filaIndiceDestino)
vDB = rngDB
r = UBound(vDB, 1)
For i = 1 To r
    vDB(i, 1) = Replace(vDB(i, 1), "-", "")
    cnt = 10 - Len(vDB(i, 1))
    s = WorksheetFunction.Rept("0", cnt) 'if letters inside
    'vDB(i, 1) = Format(vDB(i, 1), "0000000000") ' if number
Next i
rngDB.NumberFormatLocal = "@" '<~~ text format
rngDB = vDB

整个代码

Private Sub duplicadorLicMed()

    Set Application = CreateObject("Excel.Application")

    Dim planillaDestino As Worksheet
    Set planillaDestino = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    planillaDestino.Name = "hojaDest"

    Dim planillaFuente As Worksheet
    Set planillaFuente = Application.Workbooks.Open("tstfl.xlsm")
    Set planillaFuente = ThisWorkbook.Worksheets(1)
    planillaFuente.Name = "hojaFuente"

    Dim filaFuenteUltima As Long
    filaFuenteUltima = planillaFuente.Cells(planillaFuente.Rows.Count, "B").End(xlUp).Row

    Dim filaIndiceFuente As Long

    Dim filaIndiceDestino As Long
    filaIndiceDestino = 1 ' salto de lineas y encabezado

    Dim fechaInicio As Variant
    Dim fechaFin As Variant
    Dim fechaIndice As Date

        For filaIndiceFuente = 2 To filaFuenteUltima
        fechaInicio = planillaFuente.Cells(filaIndiceFuente, "L").Value
        fechaFin = planillaFuente.Cells(filaIndiceFuente, "M").Value

        ' VALIDATOR OF DATA

        For fechaIndice = fechaInicio To fechaFin
            filaIndiceDestino = filaIndiceDestino + 1

        ' ROWS GET REPEATED HERE

        Next fechaIndice
    Next filaIndiceFuente

    'HERE I RUN THE FORMAT FORMULAS
    Dim rngDB As Range
    Dim vDB As Variant, i As Long, r As Long
    Dim cnt As Integer

    planillaDestino.Range("B2:B" & filaIndiceDestino).Formula = "=CONCAT(YEAR(L2),IF(INT(MONTH(L2))<10,0,""""),MONTH(L2))" ' per pro

    Set rngDB = planillaDestino.Range("B2:B" & filaIndiceDestino)
    vDB = rngDB
    r = UBound(vDB, 1)
    For i = 1 To r
        vDB(i, 1) = Replace(vDB(i, 1), "-", "")
        cnt = 10 - Len(vDB(i, 1))
        s = WorksheetFunction.Rept("0", cnt) 'if number & string
        'vDB(i, 1) = Format(vDB(i, 1), "0000000000") ' if number
    Next i
    rngDB.NumberFormatLocal = "@" '<~~ text format
    rngDB = vDB
    'planillaDestino.Range("B2:B" & filaIndiceDestino).Formula = "=CONCAT(IF(LEN(SUBSTITUTE(C2,"-","") )<9,"00","0"),SUBSTITUTE(C2,"-","") )" ' digitador rut
    planillaDestino.Range("K2:K" & filaIndiceDestino).Formula = "=ABS(DAYS(L2,M2))+1" ' dias totales

MsgBox "PROCESO COMPLETO"

' End If
End Sub