Private Sub CommandButton1Click() Dim ws As Worksheet Dim cell As Range Dim lastRow As…
- User
Private Sub CommandButton1_Click()
Dim ws As Worksheet Dim cell As Range Dim lastRow As Long Dim RegEx As Object Dim matches As Object Dim match As Variant
' Initializing the Regular Expression object Set RegEx = CreateObject("VBScript.RegExp") With RegEx .Global = False ' Find only the first match .IgnoreCase = True ' Case-insensitive matching .Pattern = "(\d+)(FOI|TAIERI)" ' Pattern to find number directly before FOI or TAIERI End With ' Set reference to the required worksheet Set ws = ThisWorkbook.Sheets("Sheet1") lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Loop through each cell in Column B For Each cell In ws.Range("B1:B" & lastRow) If RegEx.Test(cell.Value) Then Set matches = RegEx.Execute(cell.Value) For Each match In matches ' Write the numeric part to Column A cell.Offset(0, -1).Value = match.SubMatches(0) Exit For ' Exit after the first match to ensure only one number is written per cell Next match Else cell.Offset(0, -1).Value = "" ' Mark cells where no pattern was matched End If Next cell MsgBox "Process completed. Check Column A."End Sub
Private Sub CommandButton2_Click()
Dim wsTarget As Worksheet Dim wsLog As Worksheet Dim wbSource As Workbook Dim wsSource As Worksheet Dim strFile As String Dim fileDialog As fileDialog Dim lastRow As Long Dim sourceRange As Range Dim targetRange As Range Dim logRow As Long Application.ScreenUpdating = False ' Seteaz? foaia ?int? unde vrei s? copiezi datele Set wsTarget = ThisWorkbook.Sheets(1) 'Presupunând c? este prima foaie ' Seteaz? foaia pentru log Set wsLog = ThisWorkbook.Sheets("Sheet2") ' Ini?ializeaz? dialogul pentru selectarea fi?ierului Set fileDialog = Application.fileDialog(msoFileDialogFilePicker) With fileDialog .AllowMultiSelect = False .Title = "Selecteaza un fisier .xlsx" .Filters.Add "Fisiere Excel", "*.xlsx" If .Show = -1 Then ' Dac? utilizatorul selecteaz? un fi?ier strFile = .SelectedItems(1) ' Ob?ine calea complet? a fi?ierului selectat ' Deschide fi?ierul .xlsx selectat Set wbSource = Workbooks.Open(Filename:=strFile) Set wsSource = wbSource.Sheets(1) ' Determin? ultimul rând utilizat pe foaia ?int? pentru a începe copierea datelor dincolo de acest punct lastRow = wsTarget.Cells(wsTarget.Rows.Count, 1).End(xlUp).Row ' Seteaz? gama surs? de copiat (doar valorile, nu ?i formatarea) Set sourceRange = wsSource.UsedRange ' Stabile?te gama ?int? unde vor fi copiate datele Set targetRange = wsTarget.Cells(lastRow + 1, 1) ' Copiaz? valorile din surs? în ?int? f?r? a aduce formatarea sau formulele sourceRange.Copy targetRange.PasteSpecial Paste:=xlPasteValues Application.CutCopyMode = False ' Anuleaz? modul de copiere ' Închide fi?ierul .xlsx wbSource.Close SaveChanges:=False ' Logheaz? calea fi?ierului surs? în Sheet2 logRow = wsLog.Cells(wsLog.Rows.Count, 2).End(xlUp).Row + 1 ' Asigur? c? începi de la B1 dac? este gol If logRow = 1048577 Then logRow = 1 wsLog.Cells(logRow, 2).Value = strFile MsgBox "Datele au fost copiate cu succes din " & strFile Else MsgBox "Niciun fisier nu a fost selectat." End If End With Application.ScreenUpdating = True - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.