VBA Paste Data After Other VBA Copied Data - vba

I'm trying to have a "Called Sub" paste data after the last row used in the one that is calling the code.
However, I can only manage to have the first sub to paste the first data selected and when "ESTDEUDA" is called it pastes the other data on information first used.
Sub ActualizarFondos()
'Deuda
J = 12
For i = 15 To 26
Sheets("Reporte").Activate
If Cells(i, "C").Value > 0 Then
Range(Cells(i, "C"), Cells(i, "B")).Copy
ActiveSheet.Range(Cells(J, "Z"), Cells(J, "AA")).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Call ESTDEUDA
J = J + 1
End If
Next
End Sub
Sub ESTDEUDA()
J = 12
For i = 3 To 6
Sheets("FondosEstrategia").Activate
If Cells(i, "F").Value > 0 Then
Range(Cells(i, "E"), Cells(i, "F")).Select
Range(Cells(i, "E"), Cells(i, "F")).Copy
Sheets("Reporte").Activate
Range(Cells(J, "Z"), Cells(J, "AA")).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
J = J + 1
End If
End Sub
I'd like to know what can be done in order to have the information from sheet "FondosEstrategia" to be pasted after the infomation pasted from sheet "Reporte".
Is there any way that a kind of J = J +1 is applied to "ESTDEUDA" in order to paste after J = J +1 from "ActualizarFondos".
Thanks!

You do not need to use J. Just offset your i value in your first loop to produce desired J value.
On your first loop:
i = 15
J = 12 which is the same is i - 3.
Therefore, you can swap out every instance of J with i - 3.
Next, you can pass i as a parameter (input) into ESTDEUDA using the below method.
Sub ActualizarFondos()
Dim i As Integer
For i = 15 To 26
With Sheets("Reporte")
If .Cells(i, "C").Value > 0 Then
.Range(.Cells(i, "C"), .Cells(i, "B")).Copy
ThisWorkbook.Sheets("WHATSHEET").Range("Z" & i - 3).PasteSpecial Paste:=xlPasteValues
Call ESTDEUDA(i)
End If
End With
Next i
End Sub
Sub ESTDEUDA(i As Integer)
Dim x As Long
For x = 3 To 6
With Sheets("FondosEstrategia")
If .Cells(x, "F").Value > 0 Then
.Range(.Cells(x, "E"), .Cells(x, "F")).Copy
Sheets("Reporte").Range("Z" & i - 3).PasteSpecial Paste:=xlPasteValues
End If
End With
Next x
End Sub
Also, you need to qualify your instances of Range and Cells with a direct sheet. You should avoid relying to Active or Selected sheet.

Related

Adjusting the range per iteration in a Macro VBA

I have a huge dataset consisting of multiple choice questions, which have to be sorted. Each question consists of a group of 10 rows, which has to be transformed into 10 columns. The sheet is now 1100 rows and I will have to do this with 16 other sheets of the same format.
I have created a macro in Excel by recording the necessary actions which result in this line of code:
Selection.End(xlDown).Select
Range("C21:C26").Select
Selection.Copy
Range("C19").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Rows("21:31").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("B27").Select
End Sub
Now I need the variables in the macro to change +1 each iteration, so the next iteration it will look like this.
Selection.End(xlDown).Select
Range("C22:C27").Select
Selection.Copy
Range("C20").Select
Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _
, Transpose:=True
Rows("22:32").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Range("B28").Select
End Sub
I'm completely stuck there. Help is greatly appreciated!
Thanks so much in advance.
So I am a little unclear on your row counts.
You might be able to use arrays as follows (be sure to back up sheet as this clears data from the sheet)
Option Explicit
Public Sub Test()
Dim startRow As Long, endRow As Long, rng As Range, arr(), outputArr(), i As Long
startRow = 21
endRow = 1100
With ThisWorkbook.Worksheets("SheetA") '<== Change as required
Set rng = .Range("C" & startRow & ":C" & endRow)
arr = rng.Value
arr = Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Index(arr, 0, 1))
ReDim outputArr(1 To 5, 1 To Application.WorksheetFunction.RoundUp(UBound(arr, 1) / 5, 0))
outputArr = Application.WorksheetFunction.Transpose(outputArr)
Dim counter As Long, counter2 As Long
counter2 = 1
For i = LBound(arr) To UBound(arr) Step 12
For counter = 0 To 4
outputArr(counter2, counter + 1) = arr(i + counter)
Next
counter2 = counter2 + 1
Next
rng.ClearContents
.Range("C19").Resize(UBound(outputArr, 1), UBound(outputArr, 2)) = outputArr
End With
End Sub

Vba script for deleting all rows containing certain text string giving mismatch error as well as transpose script

I use the following two macros to cleanse data i receive in one column, the first is to cleanse the data for rows containing certain words that aren't required. The second transposes this column into rows identified by a variable (in this case X). Both theses scripts are now coming up with mismatch error 13's when I run them on a new desktop. If anyone can help with a fix would be greatly appreciated!
Thanks
Sub deletewordsandblanks()
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "A").Value) = "Surname" Then
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub
and the transpose is done by:
Sub transpose()
Dim i As Long, lRow As Long, n As Long, j As Long
lRow = Range("A" & Rows.Count).End(xlUp).Row
n = 1
j = 1
For i = n To lRow
If Cells(i, 1).Value = "X" Then
Range(Cells(i, 1), Cells(n, 1)).Copy
Range(Cells(j, 2), Cells(j, i)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, transpose:=True
Application.CutCopyMode = False
n = i + 1
j = j + 1
End If
Next i
Columns(1).Delete
End Sub
The issue was not with the code but the data was not correctly formatted and generating the mismatch. It was due to the conversion from the scanned documents into the column.
Apologies for the mistake and Thanks for your help guys!

vba Excel Paste Values: automatic: Error 1004 manual: OK

strange problem:
I read some Values from a sheet with a loop and paste them to another sheet.
I have a Control Module which calls one module after the other one.
My Problem: If I do the Call via control Module I run into the runtime error 1004.
When I start the macro manually it is no problem and everythin works fine..
This is my code:
[...]
rngname = 3
temp = 1
Do Until Cells(lngRow, 1).Value = "test"
lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row
Set slcFind = Range(Cells(lngRow, 1), Cells(lngLastRowOfSection, 1))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(1, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngRow >= Rows.Count Then Exit Do
Loop
lngRow = 1
rngname = 3
i = 2
Do Until Cells(lngRow, 1).Value = "test"
lngLastRowOfSection = Cells(lngRow, 1).End(xlDown).Row
Set slcFind = Range(Cells(lngRow, 2), Cells(lngLastRowOfSection, 2))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(i, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngRow = Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngRow >= Rows.Count Then Exit Do
i = i + 1
Loop
[...]
Has anyone an idea?
This part of the code is marked:
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Best Regards,
Kalain
Scott Holtzman is correct, define your worksheet when setting ranges. I was able to recreate the bug by having 1 sheet with valid data, the sheet to paste and another entirely blank sheet. When I ran the macro from the valid sheet, it was fine, when I ran it with the blank sheet activated it tries to transpose a blank column to a row. Excel sheets have more columns then rows, so it crashes because it can't fit.
Try using this, change "First" to whatever your source sheet is called.
Sub test()
Dim Other As New Worksheet
Set Other = Worksheets("First")
lngrow = 1
rngname = 3
temp = 1
Do Until other.Cells(lngrow, 1).Value = "test"
lngLastRowOfSection = Other.Cells(lngrow, 1).End(xlDown).Row
Set slcFind = Range(Other.Cells(lngrow, 1), Other.Cells(lngLastRowOfSection, 1))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(1, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngrow = Other.Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngrow >= Rows.Count Then Exit Do
Loop
lngrow = 1
rngname = 3
i = 2
Do Until Other.Cells(lngrow, 1).Value = "test"
lngLastRowOfSection = Other.Cells(lngrow, 1).End(xlDown).Row
Set slcFind = Range(Other.Cells(lngrow, 2), Other.Cells(lngLastRowOfSection, 2))
slcFind.Copy
Set targetRange = Worksheets("Node Canister VPD").Cells(i, 1)
targetRange.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
lngrow = Other.Cells(lngLastRowOfSection, 1).End(xlDown).Row
If lngrow >= Rows.Count Then Exit Do
i = i + 1
Loop
End Sub

Repeatively restructure a table excel vba

I have been trying to create a vba code to rebuild a table with the information fed into it. What the code should do is the following:
Look in column G for the first none empty and non 0 cell and copy that value to the first #N/A in column C. Finally recalculate row for the first cell found in column C.This is the code I have.
i = 3
For i = 3 To 79
If Cells(i, 7).Value > 0 And Cells(i, 7).Value <> "" Then
MovingValue = Cells(i, 7).Value
Cells(i, 7).Copy
j = 3
For j = 3 To 4
If Cells(j, 3).Text = "#N/A" Then Exit For
Cells(j, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Cells(i, 3).FormulaLocal = "=VLOOKUP(B" & (i) & ";'Cleaning step 2'!B:C;2;0)-" & MovingValue
Next j
End If
Next i
What I can't get it to do is, jump out of the code after it has completed this cylce an redo it for a new run. Recheck column G for first non empty and non 0. etc.
Can anyone help me?

vba make row cells vertical and repeat value in column one

I have an Excel dataset that has animals in column a, and numbers in columns b, c, and d.
I would like to find a vba code that will take this dataset and do two things: transpose the numbers into a column, and then put the name of the associated animal into the adjacent cell. If you follow the link, sheet one shows the dataset that I have, and sheet 2 shows the dataset I would like to have.
you can see the dataset here: https://drive.google.com/file/d/0B8ss18LQyoQrdDVIQ2JMZmdPNVU/view?usp=sharing
This code will get me partway, but it doesn't do quite what I want it do to:
Sub moveandinsert()
Dim start_cell As Range
For i = 1 To 3
Set start_cell = Sheets("sheet1").Cells(i, 2)
Range(start_cell, start_cell.End(xlToRight)).Copy
Sheets("Sheet2").Select
lastRowA = Range("A" & Rows.Count).End(xlUp).Row + 1
Range("A" & lastRowA).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
For j = 1 To 12
If Cells(j, 1).Value > 0 Then
Sheets("Sheet1").Cells(i, 1).Copy
Sheets("Sheet2").Cells(j, 2).Select
Selection.PasteSpecial xlPasteAll
j = j + 1
End If
Next j
Next i
End Sub`
Any help will be appreciated
Try the following:
Sub moveandinsert()
Dim start_cell As Range
For i = 1 To 3
Set start_cell = Sheets("sheet1").Cells(i, 2)
Range(start_cell, start_cell.End(xlToRight)).Copy
Sheets("Sheet2").Select
lastRowA = Range("A" & Rows.Count).End(xlUp).Row + 1
Range("A" & lastRowA).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
For j = lastRowA To lastRowA + 4
If Cells(j, 1).Value > 0 Then
Sheets("Sheet1").Cells(i, 1).Copy
Sheets("Sheet2").Cells(j, 2).Select
Selection.PasteSpecial xlPasteAll
'j = j + 1
End If
Next j
Next i
End Sub
1) j = j + 1 is not required cuz j will increment itself in a for loop
2) you can use lastrowA as the starting point of your paste instead of hardcoded for j = 1 to 12