VBA to copy month wise data from Pivots from multiple workbooks - excel

I have multiple pivot tables (month wise data in columns) in one worksheet and want to copy all tables in another workbook. I have defined the range to copy paste data but the problem is few Pivot table don't have all months (Jan-Dec), in few months are missing. How do I use match/index/hlookup function in vba to copy data based on month and paste in another workbook.
Book1.Sheets("Analysis").Range("A13:M71").Copy
Temp.Sheets("Data (2021)").Range("B4").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks _
:=True, Transpose:=False
Book2.Sheets("Analysis").Range("S17:W17").Copy
Temp.Sheets("Data (2021)").Range("Z21").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks _
:=True, Transpose:=False
Book3.Sheets("Analysis").Range("D12:H12").Copy
Temp.Sheets("Data (2021)").Range("Z36").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks _
:=True, Transpose:=False
Book4.Sheets("Analysis").Range("B5:J5").Copy
Temp.Sheets("Data (2021)").Range("X16").PasteSpecial Paste:=xlPasteValues, operation:=xlNone, skipblanks _
:=True, Transpose:=True
Thanks

Related

Is there a way to copy grouping from another workbook when copying in VBA

I am trying to copy sheet from one workbook into another. I have a separate function to cycle through files (sources) within the parent folder and copy Summary tabs from the files into the master file in its designated tabs. Is there a way to preserve horizontal (rows) grouping the same as in the source files when copied to the master workbook? The code I've tried is below, but grouping doesn't go through.
Private Function CopyAndPaste(wsCopy As Worksheet, wsDest As Worksheet)
wsCopy.Range("A1:Z500").EntireRow.Copy
wsDest.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wsCopy.Range("A1:Z500").EntireRow.Copy
wsDest.Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Function
Thank you for your help.
To copy grouping it should be the following following:
wsCopy.Range("1:500").EntireRow.Copy
wsDest.Range("1:500").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

VBA Fixed workbook to various workbooks

I think I'd like to do my everyday things easier. I do not have any wide knowleadge of VBA so i use it with "record VBA".
I have 2 workbooks:
(1) "DATABASE.xlsx" - fixed which always collect data
(2) many workbooks i.e. "xxx.xlsx" , "yyyy.xlxs", "zzzz.xlsx" etc. - variable workbook, changing every time depends on what i copy from the workbook (1) - I will open (2) manually by myself
My issue is how to write my VBA to copy data from (1) to various workbooks (2). I do not know how to define the (2) workbooks which alwyas change. I can choose (2) workbooks manually. It does not have to be something really professional. It's only about coping from (1) to (2)....
I have done as below:
Sub Makro10() ' ' Makro10 Makro ' ' Klawisz skrótu: Ctrl+Shift+X '
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 2).Range("A1:H1").Select
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 8).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Offset(0, 9).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 3).Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Offset(0, 14).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False End Sub
It's working for only specific (2) workbook "xxx.xlsx", but i would like to use it for various (2).
I'm asking for support, thanks in advance.
The easiest way to achieve this is by replacing all the instances of Windows("xxx.xlsx").Activate with Workbooks(2).Activate in your code.
But you have to make sure that no other workbooks are open as the code can accidentally reference another workbook based on how you opened excel files. So technically, you need to open workbook (1) first which is DATABASE.xlsx. Then open whichever workbook (2) you need and run the code.
Sub Makro10() ' ' Makro10 Makro ' ' Klawisz skrótu: Ctrl+Shift+X '
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 2).Range("A1:H1").Select
Selection.Copy
Workbooks(2).Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 8).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(2).Activate
ActiveCell.Offset(0, 9).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(2).Activate
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 3).Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(2).Activate
ActiveCell.Offset(0, 14).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False End Sub
What is happening here is that VBA references the 2nd workbook that was opened (which is workbooks(2)) and pastes the data there as per the code. in your earlier code, because you recorded it, it was always using xxx.xlsx file because that is how you recorded it.
I would suggest that if possible save the code in DATABASE.xlsx and save the file as a macro file (DATABASE.xlsm). That way, you can open workbook (1) and workbook (2) (whichever one you want) and run the code from workbook (1).
Hope this helps! Happy coding!
Sub Makro2()
'
' Makro2 Makro
'
' Klawisz skrótu: Ctrl+Shift+X
'
Dim OthWB As String
Windows.Application.ActiveSheet.Select
OthWB = ActiveWorkbook.Name
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 2).Range("A1:H1").Select
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 8).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Offset(0, 9).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 3).Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Offset(0, 14).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
#Bharath Raja
Thanks for your answer. I will try your solution also
I was thinking also about issue & found the resolve. We must active (2) workbook & then it is defined as OthWB. Maybe somebody uses it also.

How to use the same macro for a new row

How do I change the following code to use the same macro for the next row in my spreadsheet: i.e. range("C1360") then "C1361" etc.
ActiveWindow.ActivateNext
Range("C1359").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Adding If/End If to macro

I'm trying to add a condition to a copy and paste macro where it copies a row from table1 and pastes it onto table2 if the row in table1 is red.
I've tried:
Sub ColdLake1()
If Range("B55").Interior.ColorIndex = 3 Then
Range("B55:H55").Select
Selection.Copy
Range("C140").Select
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End Sub
It returns nothing. Any ideas?
Thanks (I'm quite new to all this).
This worked for me:
Sub ColdLake1()
With ActiveSheet
Debug.Print "ColorIndex", .Range("B55").Interior.ColorIndex
If .Range("B55").Interior.ColorIndex = 3 Then
.Range("B55:H55").Copy
With .Range("C140")
.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
End If
End With
End Sub
If your code isn't doing anything it may be because the color isn't what you think it is, or for some reason your code is not entering the If block. Put some breakpoints and see what is going on. https://www.excel-easy.com/vba/examples/debugging.html

Paste figures avoiding formulas on target

I have a macro to copy and paste values into different columns in my spreadsheet. On the target columns I have some formulas that I need to avoid to override when the macro paste the values as I actually want the formula to calculate the new values. I have a code created already, but I would really appreciate you help adding the condition to avoid pasting where there are formulas on the target.
Sub RawDataNew()
'
' RawDataNew Macro
' To move validated to previous week on raw data tab
'
Range("$B$17:$AQ$2572").AutoFilter Field:=6
Range("Z18:AB2572").Select
Selection.Copy
Range("AU18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("H18:H2572").Select
Selection.Copy
Range("AR18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("O18:O2572").Select
Application.CutCopyMode = False
Selection.Copy
Range("AS18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("W18:W2572").Select
Selection.Copy
Range("AX18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("X18:X2572").Select
Application.CutCopyMode = False
Selection.Copy
Range("AY18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G4").FormulaR1C1 = "=TODAY()"
Range("G4").Value = Date
Range("G5").Value = Environ("username")
'
End Sub
The paste method you're using Paste:=xlPasteValues is only pasting the values; if you want formulas as well as values, you'll need Paste:=xlPasteAll.
If you ONLY want formulas, you'll need Paste:=xlPasteFormulas
Please refer to link for other optional parameters
Another suggestion is using the = method ie.
Range("A1").value = Range("B1").value
As the copy/paste method is very slow, especially when you have as much data as you're using here now. Also, you'll get all the extra formatting from the copied cells, which can be quite annoying to deal with

Resources