How to select some selected Pivot table area? - vba

I am trying to select and copy some selected area of a pivot table. I am able to determine the amount of area I want and am able to display the range in a message box which is not my objective. I want to copy that selected range. My code looks like this.
I want to copy the Values in the range (toprow1,leftcoloumn:lastrow,rightcoloumn).
FYI the message box code is something I don't need Its just their to tell you the Range no.
Sub PivotTableRangeAreas()
With ActiveSheet.PivotTables(1)
Dim TopRow1 As Long, TopRow2 As Long, LastRow As Long
Dim LeftColumn As Long, RightColumn As Long
TopRow2 = .TableRange2.Row
With .TableRange1
TopRow1 = .Row
LastRow = .Rows.Count + .Row - 1
LeftColumn = .Column
RightColumn = .Columns.Count + .Column - 1
End With
MsgBox "The pivot table named " & .Name & vbCrLf & _
"occupies these range elements:" & vbCrLf & vbCrLf & _
"With the Report (Page) field: " & vbCrLf & _
.TableRange2.Address(0, 0) & vbCrLf & _
"Without the Report (Page) field: " & vbCrLf & _
.TableRange1.Address(0, 0) & vbCrLf & vbCrLf & _
"First row, with the Report (Page) field: " & TopRow2 & vbCrLf & _
"First row, without the Report (Page) field: " & TopRow1 & vbCrLf & _
"Last row: " & LastRow & vbCrLf & _
"Left column: " & LeftColumn & vbCrLf & _
"Right column: " & RightColumn, , "Pivot table location."
End With
End Sub

I'm guessing it's just the values that you want to copy? If so, try starting with something like this - it'll put the values into Sheet2 starting at range A1. I'm not sure which range from the pivot table you want to copy to where - you'll have to change some of this to suit:
Sub CopyRange()
Dim vArray() As Variant
'Copies the values between (Toprow1, LeftColumn) and (LastRow, RightColumn) into an array
vArray = ActiveSheet.Range(Cells(TopRow1, LeftColumn), Cells(LastRow, RightColumn)).Value
'Pastes the values from the array into Sheet2, starting at A1
Sheet2.Range("A1").Resize(UBound(vArray, 1), UBound(vArray, 2)).Value = vArray
End Sub

Related

How do i use the IF condition depending on the input contained in a column (not in a cell)?

I have an excel-workbook containing two worksheets, and I have written code to transfer data from sheet No.1 to sheet No.2.
What I need is to include a condition that checks if the column G does not contain a certain value. In that case I would like a MsgBox to display "Check..".
The interested range in the Sheet 1 is (A3:J50), so the condition would interest cells G3 to G50.
My current code is:
Sub kk()
Dim lastrow As Integer
lastrow = [b50].End(xlUp).Row
Range("b3:J" & lastrow).Copy Sheets("Daily Rec.").Range("b" & Sheets("Daily Rec.").[b1000].End(xlUp).Row + 1)
Range("b3:j" & lastrow).ClearContents
MsgBox ("Date Posted")
Sheets("Daily Rec.").Activate
MsgBox ("Check..")
End Sub
please advice
This should help get you started.
But like others have mentioned, we need more info to help.
Sub Okay()
Dim source As Range
Dim target As Range
Dim found As Range
Dim cell As Range
Set source = ThisWorkbook.Worksheets("Sheet 1").Range("A3:J50")
Set target = ThisWorkbook.Worksheets("Sheet 2").Range("G3:G50")
For Each cell In source.Cells
Set found = target.Find(cell.Value)
If found Is Nothing Then
MsgBox "Check.." & vbNewLine _
& "Cell [" & cell.Address(0, 0) & "] on sheet [" & cell.Parent.Name & "]" _
& vbNewLine _
& "was not found within " & vbNewLine _
& "cell range of [" & target.Address(0, 0) & "] on sheet [" & target.Parent.Name & "]"
End If
Next cell
End Sub

Excel Application Crash due to Macro

During launching my macro the Excel application is crashed. If I test the macro with an integer the program runs properly (partnumber = 123). If I check with a string the application is crashed. Thus, no error code is visible for me. I assume that there is a type mismatch (but I set Variant for partnumber)
Sub SbIsInCOPexport()
Dim lastRow As Long
Dim i As Long
Dim found As Boolean
Dim partnumber As Variant
i = 1
found = False
partnumber = ActiveCell.Value
Windows("COPexport.xlsx").Activate
lastRow = Sheets(1).Cells.SpecialCells(xlLastCell).Row
Do While i < lastRow + 1
If Cells(i, 6).Value = partnumber Then
found = True
Exit Do
End If
i = i + 1
Loop
If found = True Then
Cells(i, 6).Select
MsgBox ("Searched part number: " & Str(partnumber) & vbNewLine & "Found part number: " _
& ActiveCell.Value & vbNewLine & "Address: " & Cells(i, 6).Address & vbNewLine & vbNewLine & "Test Order: " & _
Cells(i, 2).Value)
Windows("COPexport.xlsx").Activate
Else
MsgBox "Part number is not found in the COP samples!"
Windows("COPexport.xlsx").Activate
End If
End Sub
What can be the root cause?
I don't see any obvious issues, but consider using the .Find method of range object, like so:
Sub SbIsInCOPexport()
Dim partnumber as Variant
Dim rng as Range
Windows("COPexport.xlsx").Activate
partnumber = ActiveCell.Value
Set rng = Columns(6).Find(partnumber) '## Search in column 6 for partnumber
If rng Is Nothing Then
MsgBox "Part number is not found in the COP samples!"
Windows("COPexport.xlsx").Activate
Else
With rng
MsgBox ("Searched part number: " & Str(partnumber) & vbNewLine & _
"Found part number: " & .Value & vbNewLine & _
"Address: " & .Address & vbNewLine & vbNewLine & _
"Test Order: " & .Offset(0,-4).Value) '## Get the value from column 2
End With
End If
End Sub

Excel VBA find column number of user selected range (by mouse)

I have a script that allows a user to select a column with their mouse. Then, the user selects whether or not that column has a Header. How can I define the column number of the range that is selected so that I can perform functions on it like:
usc = rng.columns
For i = 2 to lastrow
cells(i,usc + 1).Value = left(Cells(i,usc),2)
next i
The script I have is below. Any help would be greatly appreciated!
Set rng = Application.InputBox( _
Prompt:="Please select your target column. " & vbNewLine & _
" (e.g. Column A or Column B)", _
Title:="Select Column", Type:=8)
On Error GoTo 0
hdr = MsgBox("Does your selection contain a header?", vbYesNo + vbQuestion, "Header Option")
try this
Sub test()
Set Rng = Application.InputBox("Please select your target column. " & Chr(10) & _
" (e.g. Column A or Column B)", "Select Column", , , , , , 8)
MsgBox "Selected column is: " & Rng.Column & Chr(10) & "Selected row is: " & Rng.Row
End Sub
result

How to set a loop into a macro that has no integer values set?

I have written the code below so that it will retreive the file locations and put them into Path Column(B) which corresponds to the .csv column(C) where a "YES" is found.
Dim csv_ap As Range
Dim path_report2 As String
Sheets("Mail Report").Activate
Set csv_ap = Range("C65000").End(xlUp)
If csv_ap.Value = "YES" Then
path_report2 = MAIN_PATH & "1. Invoices+BUFs - " & Sheets("Sheet1").Range("D65000").End(xlUp).Value _
& "\" & Sheets("Sheet1").Range("C65000").End(xlUp).Value & " - " & Sheets("Sheet1").Range("AK65000").End(xlUp).Value _
& "\" & "LOGGED" & "\" & Sheets("Sheet1").Range("E65000").End(xlUp).Value
csv_ap.Offset(0, -1) = path_report2
End If
As you can see only the bottom row for column B has been filled. I'm not 100% sure why this is but could be down to not having a loop involved? I have tirelessly looked into adding a loop but cannot do so without affecting the current code. Any ideas?
I have edited the code above and got a loop working. But now it is duplicating the bottom row.
Dim cell As Range
Dim path_report2 As String
Sheets("Mail Report").Activate
For Each cell In Sheets("Mail Report").Range("C2:C10").Cells
If cell = "YES" Then
path_report2 = MAIN_PATH & "1. Invoices+BUFs - " & Sheets("Sheet1").Range("D65000").End(xlUp).Value & "\" & Sheets("Sheet1").Range("C65000").End(xlUp).Value & " - " & Sheets("Sheet1").Range("AK65000").End(xlUp).Value & "\" & "LOGGED" & "\" & Sheets("Sheet1").Range("E65000").End(xlUp).Value
cell.Offset(0, -1) = path_report2
End If
Next
This is the result of the macro:
Your range is defined as a single cell by Set csv_ap = Range("C65000").End(xlUp).
If you want to process all the rows from 1 to the last occupied range, then you would need:
Set csv_ap = Range("C1:C" & Range("C65000").End(xlUp).Row)

Print dynamic list in pop up

I have a list of dynamic length from columns A to F. (starts at row 1) I need to make a code to have this list printed on a pop up. I don not want it printed on another sheet, the sheet this list is on is very hidden. I need to minimize copying these numbers thus why i don't want it on another sheet.
The proble is as i said this list is of dynamis length. So I'd have something like:
msgbox(upf.cells(1,1) & " " & upf.cells(1,2) & " " & upf.cells(1,3) & " " & upf.cells(1,4) _
upf.cells(2,1) & " " & upf.cells(2,2) & " " & upf.cells(2,3) & " " & upf.cells(2,4) _
... up to row lr)
How can I write this in some sort of a for i= 1 to lr loop?
Thank you!
As a basic example...
Sub tgr()
Dim upf As Range
Dim cIndex As Long
Dim rIndex As Long
Dim sMsg As String
Set upf = Range("A1", Cells(Rows.Count, "F").End(xlUp))
For rIndex = 1 To upf.Rows.Count
For cIndex = 1 To upf.Columns.Count
sMsg = sMsg & " " & upf.Cells(rIndex, cIndex)
Next cIndex
Next rIndex
MsgBox Mid(sMsg, 2)
End Sub