VBA Code To Transfer Numerical Value To Another Group Of Cells On Another Sheet

data808

Active Member
Joined
Dec 3, 2010
Messages
353
Office Version
  1. 2019
Platform
  1. Windows
This one is a bit complicated but I hope someone knows how to do the VBA to get this done.

Example
Sheet1 - I want the user to type a numeric value into cell A1. Lets say its 12345678 for example.

Sheet2 - I then want this numeric value (12345678) to be broken down by each number and entered into 8 different cells. One cell for each number. So for example, the first number 1 will be transferred to cell A5. Number 2 will go into B5. Number 3 will go into C5 and so forth...

Let me know if this is possible. Any help is appreciated. Thank you.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
No VBA is necessary. This can be done with a simple formula.

Put this formula in cell A5 on Sheet 2, and then drag across the row to all the other columns:
Excel Formula:
=MID(Sheet1!$A$1,COLUMN(),1)
 
Upvote 0
VBA option:

VBA Code:
Sub BreakDownNumber()
    Dim sourceCell As Range
    Dim targetCell As Range
    Dim i As Integer
    Dim lastRowSource As Long
    Dim lastRowTarget As Long

    lastRowSource = Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "A").End(xlUp).Row
    Set sourceCell = Sheets("Sheet1").Cells(lastRowSource, "A")
    lastRowTarget = Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "A").End(xlUp).Row
    Set targetCell = Sheets("Sheet2").Cells(lastRowTarget + 1, "A")
    
    For i = 1 To Len(sourceCell.Value)
        targetCell.Offset(0, i - 1).Value = Mid(sourceCell.Value, i, 1)
    Next i
End Sub
 
Upvote 0
This code is in sheet1's module, trigger any change in cell A1
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i&
If Target.Address(0, 0) = "A1" Then
    For i = 1 To Len(Target)
        Sheets("sheet2").Cells(5, i).Value = Mid(Target, i, 1)
    Next
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,378
Members
449,097
Latest member
Jabe

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