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
 
sure
VBA Code:
Sub test1()
    Dim a As Variant
    Dim i, k
    a = Range("A1").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
    Range("B2").Resize(UBound(b)) = Application.Transpose(b)
End Sub

Sub test()
    Dim a As Variant
    Dim i
    a = Range("A1").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
    Range("B2").Resize(UBound(b)) = Application.Transpose(b)
End Sub
 
Upvote 0
Solution

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
sure
VBA Code:
Sub test1()
    Dim a As Variant
    Dim i, k
    a = Range("A1").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
    Range("B2").Resize(UBound(b)) = Application.Transpose(b)
End Sub

Sub test()
    Dim a As Variant
    Dim i
    a = Range("A1").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
    Range("B2").Resize(UBound(b)) = Application.Transpose(b)
End Sub
sure
VBA Code:
Sub test1()
    Dim a As Variant
    Dim i, k
    a = Range("A1").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
    Range("B2").Resize(UBound(b)) = Application.Transpose(b)
End Sub

Sub test()
    Dim a As Variant
    Dim i
    a = Range("A1").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
    Range("B2").Resize(UBound(b)) = Application.Transpose(b)
End Sub
soooo, is it too much to ask if you can evaluate this code and explain it to me?

for example: What does resize and UBound do? And why the offset?
 
Upvote 0
Hi
About what you are asking:
-The line
VBA Code:
a = Range("A1").CurrentRegion.Offset(1)
To allocate the data to an array (a): starting from Range("A1") to the Current region where the data available the shift down one row to escape the Headers,
so now we have an array with the data it has 2 dimensions, we can read them as Ubounds And Lbound OK
-About Resize issue
VBA Code:
Range("B2").Resize(UBound(b))
Means from range("B2") and go down number of cells equal to the (b) array size
Hope this help
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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