Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
¿Hay alguna manera de anteponer texto a la celda con un formato específico usando Excel VBA?

Estoy trabajando en una macro que exporta una hoja de cálculo de Excel a csv, sin embargo, la hoja tiene celdas con formato que me gustaría identificar agregando texto a la celda a la que se aplican. Para las celdas que tienen un borde en la parte superior e izquierda, me gustaría agregar un | al principio del texto de la celda. Pude hacer que Cells.Replace funcione solo con celdas en blanco con un borde en la parte superior, pero nunca se reconoce ningún otro formato, y no funciona con ninguna celda que tenga contenido, incluso cuando intento reemplazar el contenido por completo.

Aquí hay una versión simplificada de lo que tengo hasta ahora, ¿qué estoy haciendo mal?

 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Application.ScreenUpdating = False Application.EnableEvents = False Application.DisplayAlerts = False Application.FindFormat.Clear With Application.FindFormat.Borders(xlEdgeLeft) .LineStyle = xlContinuous End With With Application.FindFormat.Borders(xlEdgeTop) .LineStyle = xlContinuous End With 'Cells.Find(What:="", SearchFormat:=True).Select Cells.Replace What:="*", Replacement:="||||", SearchFormat:=True Application.ScreenUpdating = True Application.EnableEvents = True Application.DisplayAlerts = True End Sub
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Por lo general, usa Find() en un bucle con FindNext() , pero parece que esto no funcionará cuando use SearchFormat:=True (consulte http://www.tushar-mehta.com/publish_train/xl_vba_cases/1001%20range% 20find.htm#_Using_the_SearchFormat:~:text=Desafortunadamente%2C%20FindNext%20does%20not%20respect%20the%20SearchFormat%20specification )

También es posible que desee verificar si una celda ya tiene un "|" inicial. antes de agregar uno.

Enfoque de ejemplo para solucionar el problema de SearchFormat/FindNext:

 Sub SearchFormatExample() Dim f As Range, addr, rng As Range With Application.FindFormat .Clear .Borders(xlEdgeLeft).LineStyle = xlContinuous .Borders(xlEdgeTop).LineStyle = xlContinuous End With Set rng = ActiveSheet.UsedRange Set f = rng.Find("*", lookat:=xlPart, SearchOrder:=xlByRows, _ SearchDirection:=xlNext, LookIn:=xlFormulas, _ searchformat:=True) If Not f Is Nothing Then addr = f.Address() 'note the first cell found Do While Not f Is Nothing Debug.Print f.Address 'don't add `|` if already present If Not f.Value Like "|*" Then f.Value = "|" & f.Value 'using Find not findNext Set f = rng.Find("*", after:=f, lookat:=xlPart, _ SearchOrder:=xlByRows, SearchDirection:=xlNext, _ LookIn:=xlFormulas, searchformat:=True) If f.Address = addr Then Exit Do 'exit when Find has looped back around Loop End Sub
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!