move the first part to last part for the same cell into column across sheets

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
hello
I need macro to move the first part to last part for each cell into column B across sheets
Microsoft Excel (1).xlsx
ABCD
1ITEMBRSALERET
21VEG TOMATO SS12-
32VEG TOMATO AA1212
43FR BANANA TT20010
54FR APPLE LL10020
65FR PEAR NN10-
76FR BANANA QQ20-
FRUI


Microsoft Excel (1).xlsx
ABCD
1ITEMBRSALERET
21VEG TOMATO SS20-
32VEG TOMATO AA2212
43FR BANANA TT1210
54FR APPLE LL122
65FR BANANA QQ12-
VEGI


Microsoft Excel (1).xlsx
ABCDEFG
1ITEMBRTYORSALERETBALANCE
21FR BANANAT001TT25225227
32FR APPLET002LL16242120
43FR PEART003NN15312
54FR BANANAT004QQ4242
65FR TOMATOT005SS2020
76FR TOMATOT006AA421230
87FR ONIONT007AA10
98FR ONIONT008AA20
109FR ONIONT009AA30
SFD
Cell Formulas
RangeFormula
G2:G10G2=E2-F2

the result should be

Microsoft Excel (1).xlsx
ABCD
1ITEMBRSALERET
21 TOMATO SS VEG12-
32 TOMATO AA VEG1212
43 BANANA TT FR20010
54 APPLE LL FR10020
65 PEAR NN FR10-
76 BANANA QQ FR20-
FRUI


Microsoft Excel (1).xlsx
ABCD
1ITEMBRSALERET
21 TOMATO SS VEG20-
32 TOMATO AA VEG2212
43 BANANA TT FR1210
54 APPLE LL FR122
65 BANANA QQ FR12-
VEGI



Microsoft Excel (1).xlsx
ABCDEFG
1ITEMBRTYORSALERETBALANCE
21 BANANA FRT001TT25225227
32 APPLE FRT002LL16242120
43 PEAR FRT003NN15312
54 BANANA FRT004QQ4242
65 TOMATO FRT005SS2020
76 TOMATO FRT006AA421230
87 ONION FRT007AA10
98 ONION FRT008AA20
109 ONION FRT009AA30
SFD
Cell Formulas
RangeFormula
G2:G10G2=E2-F2

when move to last part should create one space before it and every time i will create more sheets with the same structure and even change or add new data .
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
and every time i will create more sheets with the same structure and even change or add new data .

You agree if the macro, after making the change in each cell, puts a mark on each record, for example in column "Z", that way if you run again, the macro will only work with the unmarked (new records)?
 
Upvote 0
that way if you run again, the macro will only work with the unmarked (new records)?
can you more explain,please?
I find difficulty understand this part of your comment
 
Upvote 0
The first time (before running the macro)
You have this:
VEG TOMATO SS

After the macro, you get this:
TOMATO SS VEG

After you could add new data, you will have something like this:
TOMATO SS VEG
VEG TOMATO AA

If you run the macro again, the result will be this:
SS VEG TOMATO
TOMATO AA VEG

The macro should only consider new records, the result should be:
TOMATO SS VEG (old)
TOMATO AA VEG (new)

In order for the macro to identify which are the new records, we must (somehow) mark the old records.
Putting the mark in column Z of each record is an option of mine. Do you have any other option?
 
Upvote 0
I think now understood your view and the code couldn't identfy where is the old record or new without any mark .
Putting the mark in column Z of each record is an option of mine
so I accept your suggestion .
 
Upvote 0
I need macro to move the first part to last part for each cell into column B across sheets
Try this for all sheets:

VBA Code:
Sub move_first_part_to_last_part()
  Dim sh As Worksheet
  Dim c As Range
  Dim ini As String
  
  For Each sh In Sheets
    For Each c In sh.Range("B2", sh.Range("B" & Rows.Count).End(3))
      If sh.Range("Z" & c.Row).Value = "" Then
        ini = Split(c.Value, " ")(0)
        c.Value = Mid(c.Value, Len(ini) + 2) & " " & ini
        sh.Range("Z" & c.Row).Value = "x"
      End If
    Next
  Next
End Sub
 
Upvote 0
impressive work !
may you make the code more dynamically without spcify how many items should move after it?
as I understand your code in this line
VBA Code:
        c.Value = Mid(c.Value, Len(ini) + 2) & " " & ini
it moves the first part after two items because all of my data in all sheets just three items and this is my bad!!!!
somtimes could be five or six items then should move the first part after four or five items as in this picture, see the row2,7.
Microsoft Excel (1).xlsx
ABCD
1ITEMBRSALERET
21VEG TOMATO SS TG TCM12-
32 VEG TOMATO AA 1212
43FR BANANA TT 20010
54FR APPLE LL 10020
65 FR PEAR NN 10-
76FR BANANA QQ 20-
87 FR BANANA QQ MM N/L KK21-
FRUI

do me favor if there is way to make more dynamically ,please?
 
Upvote 0
it moves the first part after two items
Len(ini) + 2, This doesn't mean 2 elements, it means 2 spaces.

I'll try to explain what these 2 lines of the macro do:
VBA Code:
        ini = Split(c.Value, " ")(0)
        c.Value = Mid(c.Value, Len(ini) + 2) & " " & ini

Taking this as an example:
FR BANANA QQ MM N/L KK

c.Value = "FR BANANA QQ MM N/L KK"
Split(c.Value, " ")
Separate each word, so we have:
Item(0) FR
Item(1) BANANA
Item(2) QQ
Item(3) MM
Item(4) N/L
Item(5) KK

ini = Split(c.Value, " ")(0)
Then
ini = "FR"

Len(ini)
Length of the content of the ini variable
len("FR")
length = 2

Len(ini) + 2
2 + 2 = 4 (In this operation we have a 4, it does not mean 2 items. Let's pause right now...)

Remember this:
c.value = "FR BANANA QQ MM N/L KK"
If we separate each letter of the text, it would be something like this:
Position 1: "F"
Position 2: "R"
Position 3: " "
Position 4: "B"
Position 5: "A"
Position 6: "N"
etc...

c.Value = Mid(c.Value, Len(ini) + 2) & " " & ini
Do you remember 4?

c.Value = Mid(c.Value, 4) & " " & ini
That means that we are going to extract a part of the text and it is going to start the extraction at position 4.

Then
c.value = Mid("FR BANANA QQ MM N/L KK", 4)
c.value = "BANANA QQ MM N/L KK" & " " & ini
c.value = "BANANA QQ MM N/L KK" & " " & "FR"
Result:
c.value = "BANANA QQ MM N/L KK FR"
----
You have a problem with your data as you have whitespace before AND after the text:
1646739462358.png


----
Perhaps the following code tweak will help with any spaces you have at the beginning or end of the text, but you should clean up your data.

VBA Code:
Sub move_first_part_to_last_part()
  Dim sh As Worksheet
  Dim c As Range
  Dim ini As String, cad As String
  
  For Each sh In Sheets
    For Each c In sh.Range("B2", sh.Range("B" & Rows.Count).End(3))
      If sh.Range("Z" & c.Row).Value = "" Then
        cad = WorksheetFunction.Trim(c.Value)
        ini = Split(cad, " ")(0)
        c.Value = Mid(cad, Len(ini) + 2) & " " & ini
        sh.Range("Z" & c.Row).Value = "x"
      End If
    Next
  Next
End Sub
 
Upvote 0
Solution
you should clean up your data.
I did it , but it doesn't work . may you check my data,please?


Microsoft Excel (1).xlsx
ABCD
1ITEMBRSALERET
21VEG TOMATO SS TG TCM12-
32VEG TOMATO AA1212
43FR BANANA TT20010
54FR APPLE LL10020
65FR PEAR NN10-
76FR BANANA QQ20-
87 FR BANANA QQ MM N/L KK21-
FRUI
 
Upvote 0
1646791395157.png


Your data has spaces at the beginning of the text, you must correct them yourself. It is very simple, just edit the cell, go to the beginning of the text and delete what you have as white spaces.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,680
Members
449,116
Latest member
HypnoFant

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