Get info from on sheet to other sheet, then use it and do other one the same

hendrikbez

Board Regular
Joined
Dec 13, 2013
Messages
95
Office Version
  1. 2021
Platform
  1. Windows
I have 5 sheets. on my lat sheet I want to copy all info form sheet1 - 4 to sheet 5, did do a macro, but need help on how to do the following

on sheet 1 I can have info for about 2 to 12 rows
on sheet 2 I can have info for about 2 to 15 rows
on sheet 3 I can have info for about 2 to 120 rows
on sheet 4 I can have info for about 2 to 5 rows

I want to when I press the 2,3 or 4 macro it must start just after the last one of the sheet before it.
I am doing the first one, then drag down till I have lal 4 or 5 of first sheet in. Then I want to start the second one on the next row

Code:
Sub get1all()
'
' get1all Macro
'

'
    ActiveCell.FormulaR1C1 = "=UnixO!R[5]C[-1]"
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "=UnixO!R[5]C[-2]"
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "=UnixO!R[5]C[-2]"
    Range("F4").Select
    ActiveCell.FormulaR1C1 = "=UnixO!R[5]C[-1]"
    Range("F4").Select
End Sub
Code:
Sub get2all()
'
' get2all Macro
'

'
    Range("B8").Select
    ActiveCell.FormulaR1C1 = "=UnixN!R[1]C[-1]"
    Range("D8").Select
    ActiveCell.FormulaR1C1 = "=UnixN!R[1]C[-2]"
    Range("E8").Select
    ActiveCell.FormulaR1C1 = "=UnixN!R[1]C[-2]"
    Range("F8").Select
    ActiveCell.FormulaR1C1 = "=UnixN!R[1]C[-1]"
    Range("F9").Select
End Sub
 
Last edited:
It is working, I have go down in the allinfo page and the info is there, but there is a lot of blank spaces between them.

Could be because, when I import a text file, it has many rows, I delete most of them because they are not needed.
Then I run your code, and it runs, but with a lot of empty rows between them (the Sheets info)
 
Last edited:
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
yes what I gave you does not account for blank spaces, and is absolutely the reason you are getting blank spaces; you didn't mention that to begin with. I do have a similar code that would counter act it, however given the mapping you have between your columns gets extremely complicated - since the columns to not map in sequential order (or at least extremely complicated to where its above my pay grade knowledge; with Excel there's thousands of ways of writing code, just like SQL so I am not saying my way is the only way).
an example, you are mapping from columns 1,2,3, (A, B, C respectively) then jump to 5. Its almost as if you would need to use STEP just for that column in an IF statement

My suggestion would be to remapped you columns before you run this macro if you want to eliminate the blank spaces
OR
on your Import code, eliminate the spaces
 
Upvote 0
There is no spaces between the rows in each sheet,
I import the text file, then I delete all that I don't need, I always keep the top rows in each sheet.
 
Upvote 0
On each sheet, after deleting rows that I do not need, then I run your code. It then shows the first sheet, and then it shows a lot of empty spaces or rows, then the rows of the next sheet and so on
 
Upvote 0
if its a consistent amount of rows between sheet1 and sheet2, there might be something with how you are deleting the rows. The code below (which should be the same as what I had posted before) is generating the results. I'd have to see the sheet otherwise.


Code:
Sub RC_Index()
    Dim x As Integer
    Dim SCIndex As Long
        SCIndex = 1
    Dim SRIndex As Long
        SRIndex = 9
    Dim TCIndex As Long
        TCIndex = 2
    Dim TRIndex As Long
        TRIndex = 4


        For x = 1 To 4
                For SRIndex = 9 To ThisWorkbook.Worksheets(x).UsedRange.Rows.Count
                    If ThisWorkbook.Worksheets(x).Cells(SRIndex, 1) <> "" Or 1 = 1 Then
                        Dim SValue As String
                            SValue = ThisWorkbook.Worksheets(x).Cells(SRIndex, SCIndex).Value
                            With ThisWorkbook.Worksheets("Allinfo")
                                .Cells(TRIndex, 2).Value = Worksheets(x).Cells(SRIndex, 1).Value
                                .Cells(TRIndex, 4).Value = Worksheets(x).Cells(SRIndex, 2).Value
                                .Cells(TRIndex, 5).Value = Worksheets(x).Cells(SRIndex, 3).Value
                                .Cells(TRIndex, 6).Value = Worksheets(x).Cells(SRIndex, 5).Value
                            End With
                            TRIndex = TRIndex + 1
                    End If
                Next
        Next x
End Sub

Excel 2013 32 bit
A
B
C
D
E
F
1
Allinfo_ColumnA_Row1Allinfo_ColumnB_Row1Allinfo_ColumnC_Row1Allinfo_ColumnD_Row1Allinfo_ColumnE_Row1Allinfo_ColumnF_Row1
2
3
4
Sheet1_ColumnA_Row_9Sheet1_ColumnB_Row_9Sheet1_ColumnC_Row_9Sheet1_ColumnE_Row_9
5
Sheet1_ColumnA_Row_10Sheet1_ColumnB_Row_10Sheet1_ColumnC_Row_10Sheet1_ColumnE_Row_10
6
Sheet1_ColumnA_Row_11Sheet1_ColumnB_Row_11Sheet1_ColumnC_Row_11Sheet1_ColumnE_Row_11
7
Sheet1_ColumnA_Row_12Sheet1_ColumnB_Row_12Sheet1_ColumnC_Row_12Sheet1_ColumnE_Row_12
8
Sheet1_ColumnA_Row_13Sheet1_ColumnB_Row_13Sheet1_ColumnC_Row_13Sheet1_ColumnE_Row_13
9
Sheet1_ColumnA_Row_14Sheet1_ColumnB_Row_14Sheet1_ColumnC_Row_14Sheet1_ColumnE_Row_14
10
Sheet1_ColumnA_Row_15Sheet1_ColumnB_Row_15Sheet1_ColumnC_Row_15Sheet1_ColumnE_Row_15
11
Sheet1_ColumnA_Row_16Sheet1_ColumnB_Row_16Sheet1_ColumnC_Row_16Sheet1_ColumnE_Row_16
12
Sheet1_ColumnA_Row_17Sheet1_ColumnB_Row_17Sheet1_ColumnC_Row_17Sheet1_ColumnE_Row_17
13
Sheet1_ColumnA_Row_18Sheet1_ColumnB_Row_18Sheet1_ColumnC_Row_18Sheet1_ColumnE_Row_18
14
Sheet1_ColumnA_Row_19Sheet1_ColumnB_Row_19Sheet1_ColumnC_Row_19Sheet1_ColumnE_Row_19
15
Sheet1_ColumnA_Row_20Sheet1_ColumnB_Row_20Sheet1_ColumnC_Row_20Sheet1_ColumnE_Row_20
16
Sheet1_ColumnA_Row_21Sheet1_ColumnB_Row_21Sheet1_ColumnC_Row_21Sheet1_ColumnE_Row_21
17
Sheet1_ColumnA_Row_22Sheet1_ColumnB_Row_22Sheet1_ColumnC_Row_22Sheet1_ColumnE_Row_22
18
Sheet1_ColumnA_Row_23Sheet1_ColumnB_Row_23Sheet1_ColumnC_Row_23Sheet1_ColumnE_Row_23
19
Sheet1_ColumnA_Row_24Sheet1_ColumnB_Row_24Sheet1_ColumnC_Row_24Sheet1_ColumnE_Row_24
20
Sheet1_ColumnA_Row_25Sheet1_ColumnB_Row_25Sheet1_ColumnC_Row_25Sheet1_ColumnE_Row_25
21
Sheet1_ColumnA_Row_26Sheet1_ColumnB_Row_26Sheet1_ColumnC_Row_26Sheet1_ColumnE_Row_26
22
Sheet1_ColumnA_Row_27Sheet1_ColumnB_Row_27Sheet1_ColumnC_Row_27Sheet1_ColumnE_Row_27
23
Sheet1_ColumnA_Row_28Sheet1_ColumnB_Row_28Sheet1_ColumnC_Row_28Sheet1_ColumnE_Row_28
24
Sheet1_ColumnA_Row_29Sheet1_ColumnB_Row_29Sheet1_ColumnC_Row_29Sheet1_ColumnE_Row_29
25
Sheet1_ColumnA_Row_30Sheet1_ColumnB_Row_30Sheet1_ColumnC_Row_30Sheet1_ColumnE_Row_30
26
Sheet2_ColumnA_Row_9Sheet2_ColumnB_Row_9Sheet2_ColumnC_Row_9Sheet2_ColumnE_Row_9
27
Sheet2_ColumnA_Row_10Sheet2_ColumnB_Row_10Sheet2_ColumnC_Row_10Sheet2_ColumnE_Row_10
28
Sheet2_ColumnA_Row_11Sheet2_ColumnB_Row_11Sheet2_ColumnC_Row_11Sheet2_ColumnE_Row_11
29
Sheet2_ColumnA_Row_12Sheet2_ColumnB_Row_12Sheet2_ColumnC_Row_12Sheet2_ColumnE_Row_12
30
Sheet2_ColumnA_Row_13Sheet2_ColumnB_Row_13Sheet2_ColumnC_Row_13Sheet2_ColumnE_Row_13
31
Sheet2_ColumnA_Row_14Sheet2_ColumnB_Row_14Sheet2_ColumnC_Row_14Sheet2_ColumnE_Row_14
32
Sheet2_ColumnA_Row_15Sheet2_ColumnB_Row_15Sheet2_ColumnC_Row_15Sheet2_ColumnE_Row_15
33
Sheet2_ColumnA_Row_16Sheet2_ColumnB_Row_16Sheet2_ColumnC_Row_16Sheet2_ColumnE_Row_16
34
Sheet2_ColumnA_Row_17Sheet2_ColumnB_Row_17Sheet2_ColumnC_Row_17Sheet2_ColumnE_Row_17
35
Sheet2_ColumnA_Row_18Sheet2_ColumnB_Row_18Sheet2_ColumnC_Row_18Sheet2_ColumnE_Row_18
36
Sheet2_ColumnA_Row_19Sheet2_ColumnB_Row_19Sheet2_ColumnC_Row_19Sheet2_ColumnE_Row_19
37
Sheet2_ColumnA_Row_20Sheet2_ColumnB_Row_20Sheet2_ColumnC_Row_20Sheet2_ColumnE_Row_20
38
Sheet2_ColumnA_Row_21Sheet2_ColumnB_Row_21Sheet2_ColumnC_Row_21Sheet2_ColumnE_Row_21
39
Sheet2_ColumnA_Row_22Sheet2_ColumnB_Row_22Sheet2_ColumnC_Row_22Sheet2_ColumnE_Row_22
40
Sheet2_ColumnA_Row_23Sheet2_ColumnB_Row_23Sheet2_ColumnC_Row_23Sheet2_ColumnE_Row_23
41
Sheet2_ColumnA_Row_24Sheet2_ColumnB_Row_24Sheet2_ColumnC_Row_24Sheet2_ColumnE_Row_24
42
Sheet2_ColumnA_Row_25Sheet2_ColumnB_Row_25Sheet2_ColumnC_Row_25Sheet2_ColumnE_Row_25
43
Sheet2_ColumnA_Row_26Sheet2_ColumnB_Row_26Sheet2_ColumnC_Row_26Sheet2_ColumnE_Row_26
44
Sheet2_ColumnA_Row_27Sheet2_ColumnB_Row_27Sheet2_ColumnC_Row_27Sheet2_ColumnE_Row_27
45
Sheet2_ColumnA_Row_28Sheet2_ColumnB_Row_28Sheet2_ColumnC_Row_28Sheet2_ColumnE_Row_28
46
Sheet2_ColumnA_Row_29Sheet2_ColumnB_Row_29Sheet2_ColumnC_Row_29Sheet2_ColumnE_Row_29
47
Sheet2_ColumnA_Row_30Sheet2_ColumnB_Row_30Sheet2_ColumnC_Row_30Sheet2_ColumnE_Row_30
48
Sheet3_ColumnA_Row_9Sheet3_ColumnB_Row_9Sheet3_ColumnC_Row_9Sheet3_ColumnE_Row_9
49
Sheet3_ColumnA_Row_10Sheet3_ColumnB_Row_10Sheet3_ColumnC_Row_10Sheet3_ColumnE_Row_10
50
Sheet3_ColumnA_Row_11Sheet3_ColumnB_Row_11Sheet3_ColumnC_Row_11Sheet3_ColumnE_Row_11
51
Sheet3_ColumnA_Row_12Sheet3_ColumnB_Row_12Sheet3_ColumnC_Row_12Sheet3_ColumnE_Row_12
52
Sheet3_ColumnA_Row_13Sheet3_ColumnB_Row_13Sheet3_ColumnC_Row_13Sheet3_ColumnE_Row_13
53
Sheet3_ColumnA_Row_14Sheet3_ColumnB_Row_14Sheet3_ColumnC_Row_14Sheet3_ColumnE_Row_14
54
Sheet3_ColumnA_Row_15Sheet3_ColumnB_Row_15Sheet3_ColumnC_Row_15Sheet3_ColumnE_Row_15
55
Sheet3_ColumnA_Row_16Sheet3_ColumnB_Row_16Sheet3_ColumnC_Row_16Sheet3_ColumnE_Row_16
Sheet: AllInfo
 
Last edited:
Upvote 0
When running your code, it makes about 400 empty rows between sheet one and sheet 2 data, It is a pity that I cannot add a test file for you to see.
 
Last edited:
Upvote 0
bsquad, we are nearly winning. just have to also find a way to delete rows (hundreds of them)

I have delete by selecting the rows now the rows that I do not need, and it nearly working like it should, it is now showing the next sheet after the first one and so on, but now it take the last row of each sheet and replace it with the first row of the next sheet.

eg

it should look like this

AAA from sheet 1, then BBA form sheet2, then CCA from sheet4 and DDA from sheet 4, on sheet 5 it should look like this ( this just example with one column)

AAA
AAB
AAC
AAD
BBA
BBB
BBC
BBD
BBE
CCA
CCB
CCC
DDA
DDB


But is shows it like this, each last row of each sheet is not showing

AAA
AAB
AAC
BBA
BBB
BBC
BBD
CCA
CCB
DDA
 
Upvote 0
I am thinking its how you are importing and deleting - to my knowledge. When I run it after deleting rows it seems to work fine, however I know 'UsedRange' can somewhat be finicky at times.

Try replacing
Code:
For SRIndex = 9 To ThisWorkbook.Worksheets(x).UsedRange.Rows.Count

with this
Code:
For SRIndex = 9 To ThisWorkbook.Worksheets(x).Cells(Rows.Count, SCIndex).End(xlUp).Row

to see if it makes any difference
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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