Pivotitems(Cell reference) - excel

I recently started using Macros in my workbooks, and I ran into a problem with my record.
I recorded a macro that performs the following actions:
go to my Summary sheet (called "PIVOT.EXM"), expend the first Cell in the second column on my pivot table.
copy the data from there
paste my copied data to my cus.1 sheet
return to my PIVOT.EXM sheet, close the expansion of my first cell and expend the second cell and then repeat all the actions from above.
My problem is that I want to set pivotitems dynamically, in my code its recognized as value and I want it will be as my cell location.
example for my code (2 first cells):
Range("A5").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("1") _
.ShowDetail = True
Range("D5:E7").Select
Selection.Copy
Sheets("CUS.1").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("PIVOT.EXM").Select
Range("A5").Select
Application.CutCopyMode = False
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("1") _
.ShowDetail = False
Range("A6").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("2") _
.ShowDetail = True
Range("D6:E8").Select
Selection.Copy
Sheets("CUS.2").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("PIVOT.EXM").Select
Range("A6").Select
Application.CutCopyMode = False
ActiveSheet.PivotTables("PivotTable1").PivotFields("cus_number").PivotItems("2") _
.ShowDetail = False
I want that the cell location will be written in pivotitems("") instead of the value 1
Thank you very much!
I tried to write the Cell location inside the brackets with quotation marks and without.

Related

How do I exclude cells where the formula returns zero?

My file has four sheets.
From all of them, I want to copy and paste column A (from A:10) (which contains a concat formula) when some other rows are populated and then save into a csv.
All rows from A10 onwards have the concat formula which is then filled in depending on the other columns (the same applies for the other sheets).
I have it currently creating sheet1, and pasting there, then saving as a csv.
However, from the first sheet it looks at, it takes only the first line (but the second line - J11 (and so A11) are populated.
In the other sheets, it is copy and pasting the 2 rows that are populated, but also all the other rows as there are formulas there that return zero.
As I have the .End(xlDown) and technically all the other rows are populated.
I tried an IF statement for the last sheet only as a test, and currently it only copies the first populated line, and not the second (but at least it also doesn't copy all the other cells with zero).
Essentially, for each sheet I'd like to loop through with for example E10 is populated, copy and paste A10 into Sheet1, etc., if E10 is not zero.
Sub Output_test1()
'
' Output_test1 Macro
'
'
Sheets("Create").Select
Range("A10", Range("J10").End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets.Add.Name = "Sheet1"
Sheets("Sheet1").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Assign").Select
Range("A10", Range("E10").End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A1").End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Date & Time").Select
Range("A10", Range("E10").End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A1").End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Event Type").Select
Dim rg As Range
For Each rg In Range("E10").End(xlDown)
If rg.Value > 0 Then
End If
Range("A10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("A1").End(xlDown).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Application.CutCopyMode = False
Next
Sheets("Sheet1").Move
myTime = Format(Now, ("dd.mm.yy"))
ChDir "C:\Users\"
ActiveWorkbook.SaveAs Filename:= _
"Recruit_" & myTime & ".csv", FileFormat:=xlCSVUTF8, _
CreateBackup:=False
End Sub
There is no loop in your code not are you checking any values. I assumed you need to check column J in the source sheet and copy column A to the destination sheet.
This is a possible starting point:
k = 1
For i = 10 to 20
If Sheets("Source").Range("J" & i).Value = 0 then
Sheets("Destination").Range("A" & k).Value = Sheets("Source").Range("A" & i).Value
k = k + 1
End if
Next i
This only copies the value, not the formula. Not sure how much to explain, comment on the answer if any questions

Find column and then paste vba excel

I need to save some data by just clicking a button.
It should be easy but I dont know the syntax for vba.
Range("P2:P47").Copy
Sheets("All Data").Select
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I have 2 problem with this code,
1 there is "a lot of" selections that I do not know how to get rid of(I know you should try not to use selections).
2 My real problem is this:
cell J2 in the code should not be a determined cell. I would like to have a "lookup value" and search for that in an array, and then using the mathing cell to paste my values.
So in my workbook cell A1 = "Aug 19" and in row 2 all the months are listed. So cell A2 = "Jan 19", B2 = "Feb 19" etc.
I would like to select cell H2 and then pasting by matching my lookup value "Aug 19" to H2 contains "Aug 19"
So in excel formulas I would basically just write:
=HLOOKUP(A1;'All Data'!$B$2:$AL$2;1;FALSE)
Solved problem 1
Sub savedata()
Range("P2:P47").Copy
Worksheets("All Data").Range("J2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
EDIT on problem 2:
Sub savedata()
Range("P2:P47").Copy
ActiveSheet.Range("C3:K3").Find(What:="Aug 19").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
This work as I need it to! Still som questions tho, I couldn't figure out how to reference cell A1 instead of "Aug 19".
I solved it. I ended up with more selections that I would like but I tried to minimize it but I got a lot of error codes that way.
see code below:
Sub savedata()
G = Range("A1").Value
Range("P2:P47").Copy
Worksheets("All Data").Select
Range("C2:AL2").Find(What:=G).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Add Data").Select
Application.CutCopyMode = False
Range("C4:K34").ClearContents
End Sub
I would like to do like this bu this does not work:
Sub savedata()
G = Range("A1").Value
Range("P2:P47").Copy
Worksheets("All Data").Range("C2:AL2").Find(What:=G).PasteSpecial
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Worksheets("Add Data").Select
Application.CutCopyMode = False
Range("C4:K34").ClearContents
End Sub

Recognize cell contents in VBA

im trying to create a code that can help me to paste my formulas in a range.
the range is always changing as i have to include information on a daily basis at the en of my list
the existing code almost gets the job done but6 instead of recognizing the value on cell a1 (a count of the amount of records to calculate) it pastes the information on range e1:k1
Sub Run_Formulas()
'
' Run_Formulas Macro
'
Dim As Variant
x1 As Variant
x1 = Worksheets("Raw Data").Range("A1")
'
Range("E4:K4").Select
Selection.Copy
Range("E8" & ":" & "K" & "a1").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
End Sub
how can i get the macro to recognize the contents of cell a1 and to use it for my final range/

How to save and transpose a range in Excel VBA

I have a macro where I open a textfile to copy and transpose several columns into my worksheet. As it is now this actually works. However, as I am right now opening the textfile, copying a column, going back to my original worksheet pasting and transposing the data, then switching back and forward between the two sheets until all relevant columns have been copied I figured it could probably be optimized if I understood VBA a little better.
So my question is if I can save the column data as ranges instead and then copying it all at once and pasting and transposing it all at once as well?
Or will this not have any impact on the speed of my macro?
Also, as I am actually opening many textfiles (open one, close it, open the next, close it, etc) is it possible to overwrite a range when a new textfile has been opened?
I have shown an example of my code below hope you can make sense of it:
'First Selection to be copied
ActiveSheet.Cells(RowTemp + 1, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Go back to original sheet
Windows(Left(f, Len(f))).Activate
'Paste and Transpose Data
If IsEmpty(ActiveSheet.Cells(4, 2).Value) = True Then
ActiveSheet.Cells(4, 1).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
ActiveSheet.Cells(4 + 5 + Range("A2").Value, 1).Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If
'Copy 2nd set of Data
Windows(Left(z, Len(z))).Activate
Range("B1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows(Left(f, Len(f))).Activate
'Paste and Transpose 2. set of data
If IsEmpty(ActiveSheet.Cells(5, 2).Value) = True Then
ActiveSheet.Cells(5, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
ActiveSheet.Cells(5 + 5 + Range("A2").Value, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues,
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If

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