combining cell values and adding a custom text to a cell value via VBA

Xalova

Board Regular
Joined
Feb 11, 2021
Messages
80
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
So i have a table created in Excel. I want to have a macro which allows me to fill a certain cell (lets say "B1") with the cell values from cell "A1" and "A2". Also i want that at the end of the cell there always is a "mm", if that makes any sense.

Basicly what i want is the excel formula
Excel Formula:
=SUBSTITUTE(A1;"_";" ")&" x "&A2&"mm"
as a macro.

What i mean is:
InputsOutputs
ISO_4014ISO 4014 x 50mm
50
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this with active A1 cell:
VBA Code:
Sub x()
Dim c As Range, t$
    Set c = ActiveCell: t = ActiveCell.Value

    If IsEmpty(c(1, 2)) Then
        c(1, 2).Value = Replace(t, "_", " ") & " x " & c(2, 1).Value & "mm"
    Else
        MsgBox "Output cell is not empty."
    End If
End Sub
 
Upvote 0
Try this with active A1 cell:
VBA Code:
Sub x()
Dim c As Range, t$
    Set c = ActiveCell: t = ActiveCell.Value

    If IsEmpty(c(1, 2)) Then
        c(1, 2).Value = Replace(t, "_", " ") & " x " & c(2, 1).Value & "mm"
    Else
        MsgBox "Output cell is not empty."
    End If
End Sub
is there a possibility without an active cell?
 
Upvote 0
Hi
Pick one
VBA Code:
Sub test1()
    Dim a As Variant
    Dim i, k
    a = Cells(2, 1).CurrentRegion.Offset(1)
    ReDim b(1 To UBound(a) / 2)
    For i = 1 To UBound(a) - 1 Step 2
        b(k + 1) = WorksheetFunction.Substitute(a(i, 1), "_", " ") & " x " & a(i + 1, 1) & "mm"
        k = k + 1
    Next
    Cells(2, 2).Resize(UBound(b)) = Application.Transpose(b)
End Sub

Sub test2()
    Dim a As Variant
    Dim i
    a = Cells(2, 1).CurrentRegion.Offset(1)
    ReDim b(1 To UBound(a))
    For i = 1 To UBound(a) - 1 Step 2
        b(i) = WorksheetFunction.Substitute(a(i, 1), "_", " ") & " x " & a(i + 1, 1) & "mm"
    Next
    Cells(2, 2).Resize(UBound(b)) = Application.Transpose(b)
End Sub
 
Upvote 0
Hi
Pick one
VBA Code:
Sub test1()
    Dim a As Variant
    Dim i, k
    a = Cells(2, 1).CurrentRegion.Offset(1)
    ReDim b(1 To UBound(a) / 2)
    For i = 1 To UBound(a) - 1 Step 2
        b(k + 1) = WorksheetFunction.Substitute(a(i, 1), "_", " ") & " x " & a(i + 1, 1) & "mm"
        k = k + 1
    Next
    Cells(2, 2).Resize(UBound(b)) = Application.Transpose(b)
End Sub

Sub test2()
    Dim a As Variant
    Dim i
    a = Cells(2, 1).CurrentRegion.Offset(1)
    ReDim b(1 To UBound(a))
    For i = 1 To UBound(a) - 1 Step 2
        b(i) = WorksheetFunction.Substitute(a(i, 1), "_", " ") & " x " & a(i + 1, 1) & "mm"
    Next
    Cells(2, 2).Resize(UBound(b)) = Application.Transpose(b)
End Sub
Ok, so... Im a newbie to VBA alltogether. Can you tell me where the cells are defined exactly? Or is it relative to my current active cell?
 
Upvote 0
OK
Your data in range("A2") dowm
The result in B2 down since you have headers
 
Upvote 0
Book1
AB
1Inputsout
2ISO_4014ISO 4014 x 50mm
350
4ISO_4015ISO 4015 x 51mm
551
6ISO_4016ISO 4016 x 52mm
752
8ISO_4017ISO 4017 x 53mm
953
10ISO_4018ISO 4018 x 54mm
1154
12ISO_4019ISO 4019 x 55mm
1355
14ISO_4020ISO 4020 x 56mm
1556
16ISO_4021ISO 4021 x 57mm
1757
18ISO_4022ISO 4022 x 58mm
1958
Periode


Or
Book1
AB
1Inputsout
2ISO_4014ISO 4014 x 50mm
350ISO 4015 x 51mm
4ISO_4015ISO 4016 x 52mm
551ISO 4017 x 53mm
6ISO_4016ISO 4018 x 54mm
752ISO 4019 x 55mm
8ISO_4017ISO 4020 x 56mm
953ISO 4021 x 57mm
10ISO_4018ISO 4022 x 58mm
1154
12ISO_4019
1355
14ISO_4020
1556
16ISO_4021
1757
18ISO_4022
1958
Periode
 
Upvote 0
OK
Your data in range("A2") dowm
The result in B2 down since you have headers
But where is the range "A2" defined in your VBA Code? Is it "Cells(2,1)? Is it in the RC format?
 
Upvote 0
VBA Code:
Cells(2,1) same Range("A2")
BTW

VBA Code:
a = Cells(1, 1).CurrentRegion.Offset(1)
VBA Code:
a = Cells(2, 1).CurrentRegion.Offset(1)
Both gives same result
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top