Dynamic column reference from heading identified in another Excel sheet - excel

Firstly I have to apologise as this is my first attempt at any VBA coding!
I am trying to find a way of automating a process whereby I extract data from a single data sheet with multiple columns in it into several separate worksheets.
My source data sheet has multiple column headings that are identified by their heading name, column number and column reference.
I am looking for some code that will (for the 2 specified column headings in each of my output sheets:
perform an auto filter selection (=Blanks for column 1 in the operation, <> Blanks for column 2 in the operation)
copy and paste the returned data into a specified output sheet
reset the auto filters in data sheet
move onto the next output sheet, select the next two relevant columns identified in the next output sheet
perform an auto filter selection (=Blanks for column 1 in the operation, <> Blanks for column 2 in the operation)
copy and paste the returned data into a specified output sheet
reset the auto filters in data sheet etc.
I can do this for 1 sheet and repeat but fall over due to the data sheet columns not necessarily remaining in the same order (as new activity milestones are added between the activity I am counting are added).
My crude code is below:
Sub FcastSFR()
'
' FcastSFR Macro
'
'
Application.ScreenUpdating = False
Range("A23:B345").Select
Selection.ClearContents
Sheets("MOAT").Select
ActiveSheet.ShowAllData
ActiveSheet.Range("$A$1:$DT$5000").AutoFilter Field:=17, Criteria1:="="
ActiveSheet.Range("$A$1:$DT$5000").AutoFilter Field:=15, Criteria1:="<>"
Range("O1:P631").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("SFR Submit").Select
Range("A23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
I am looking for help to make the Field=17/Field=15 and the Range ("O1:P631") be selected from the relevant output sheets.
Help!!

Related

Google Sheets/Excel: Is there a way to pull information from different columns depending on the value of another column?

I'm trying to create a summary of data (responses) in a separate sheet that pulls a value from a calculated column based on the value in another column.
For example, my data output has Item Type in column C and columns D:L are item descriptions based on type. Every row (response) has an item type in column C (e.g., office supplies), then depending on that item type there is a description (e.g., pens) in the appropriate column (e.g., Office supplies item description). All other description cells in that row are blank.
What I want is to summarize the data from D:L into one column (e.g., Item description). In other words, I want to summarize all of the non-blank cells in D:L in one column.
I must add: I'm using Google Sheets with the data output from a Google Form. Every time a new response is added to the sheet, a new row is inserted below the previous row, so using reference cells (e.g., C2) doesn't seem to work.
My first time posting here so please let me know if I need to provide more information.
Example:
Response data
End goal/summary
You can use the below macro to achieve this result :
Sub Macro1()
'
' Macro1 Macro
'
Sheets("Response Data").Select
Range("C1").Select
Selection.End(xlDown).Select
LR = ActiveCell.Row
Sheets("Summary").Select
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Response Data'!R1C4:R" & LR & "C12,ROW(Summary!RC[-1]),MATCH(Summary!RC[-1] & "" item description"",'Response Data'!R1C4:R1C12,0))"
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range("B2", "B" & ActiveCell.Row).Select
Selection.FillDown
End Sub
What I was able to do is create a FILTER of the Google Forms output in a separate sheet (which created a copy of the responses that would NOT insert a new row for each entry), then use TEXTJOIN (referencing columns D:L in the new sheet) to get the info in one column on the summary sheet.
Thank you to BigBen for reminding me in the comments of the TEXTJOIN function :)

Copy/Paste Columns Data to existing worksheet beginning on next empty row (Problem)

I have a spreadsheet with various columns that are individually copied and pasted by individual macros per column to a different sheet beginning on the next empty row. This is working perfectly and as expected but there is an unprecedented problem:
Column C is an Account Name. Column D is a Transaction Description.
Column C never contains empty spaces. Column D however sometimes does not have a Transaction description on the very last cell of the column. The next time you apply the macro, it will paste the beginning data to the next empty cell in column D so it is not lining up properly and then the range on that column going south is offset by 1 or more cells.
My question, in plain english is:
Begin the pasting job on the next empty cell of Column D, but not if Column C has data next to it.
Edit:
Here is the code that I am currently using:
`Sub SAMPLE_COPYOVER_BOA_G()
Sheets("BANK OF AMERICA").Select
Rows("6:6").Select
Selection.AutoFilter
ActiveSheet.Range("$A$6:$T$1000").AutoFilter Field:=2, Criteria1:="<>"
Range("G7:G1000").Select
Selection.Copy
Sheets("COMBINED").Select
Sheets("COMBINED").Cells(Rows.Count, "C").End(xlUp).Offset(1). _
PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Sheets("COMBINED").Select
End Sub`
This code works fine, but the problem is that when I go to Column D's macro it will paste beginning with the next blank row disregarding whether Column C is longer or shorter.

Create Macro to Copy, Paste for multiple Data sheets

Below is just a macro I recorded myself which may or may not be helpful. Basically, I am trying to copy the number, name, and date from one sheet (Worksheet) then paste into a different spreasheet (Data Entry Form).
FYI, the number, name, and date goes from left to right on the ‘Worksheet’, and Each row of data (number, name , and date) needs to go into a separate ‘Data Entry Form’.
So I am looking for a macro that will use the data from “Worksheet” and place it into the ‘Data Entry Form’ while generating additional ‘Data Entry Forms’ (more worksheets) for each separate row of data.
The number of rows in the Worksheet can vary from 10-100 so having a macro would save me ample time of copying and pasting into new forms; even if the macro can only do one row of data at a time.
Sub Popsecform()
'
' Popsecform Macro
'
' Keyboard Shortcut: Ctrl+m
'
Selection.Copy
Windows("Data Entry form.xlsx").Activate
Range("F8").Select
ActiveSheet.Paste
Windows("Worksheet.xlsx").Activate
Range("C2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Data Entry form.xlsx").Activate
Range("F30").Select
ActiveSheet.Paste
Windows("Worksheet.xlsx").Activate
Range("D2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Data Entry form.xlsx").Activate
Range("F24").Select
ActiveSheet.Paste
End Sub
Edit
I am just trying to create a workbook that will be able to generate my Data Entry Form as an additional sheet to the workbook every time I add another row of Name, Number, and Date to the first Worksheet.
Row A goes to Data Entry Form 1 (Distinct cells)
Row B goes to Data Entry Form 2 (same distinct cells)
and so on

Excel VBA to copy

My task is to create a searchable database within Excel with an entry form. I need a macro to take the data from the entry form move to the database sheet offset the active cell down 1 row and copy the values only(not the formatting)
Every time I try to run the macro I get a run-time error in the code. I have no experience with VB or VBA; please tell me what is wrong with this.
Sheets("Database").Select 'Navigates to Database worksheet
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If 'Clears filters
Sheets("Entry Form").Select 'Navigates back to Entry Form worksheet
Range("E10:L10").Select ' select date, period, and data
Selection.Copy
Sheets("datatable").Select ' navigate to datatable tab
Range("A1").Select
Selection.End(xlDown).Select ' ctrl-down to last occupied row,
ActiveCell.Offset(1, 0).Select ' then one more to first blank row
Selection.PasteSpecial Paste:=xlPasteValues
'Pastes data as values only into the Database worksheet
Sheets("Entry Form").Select 'Navigates to Entry Form worksheet
Application.CutCopyMode = False 'clears copy data from clipboard
Range("E10, L10").Select
Selection.ClearContents 'Clears data from drop down selections
Range("E10").Select 'Returns selection back to Date entry box
It goes the very bottom of the next page and gives a 1004 error.
You need more than just a column label in A1 if you are going to use xlDown. There has to be at least one more value in column A or you will traverse to the bottom of the worksheet. It is usually better to look from the bottom of the worksheet upwards and then offset one row down.
With Sheets("Database") 'Primarily use Database worksheet
If .FilterMode Then .ShowAllData
With .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) 'look from bottom up then down 1 row
'direct value transfer is faster than Copy, Paste Special, Values
.Cells.Resize(1, 8) = Sheets("Entry Form").Range("E10:L10").Value
End With
End With
With Sheets("Entry Form") 'Primarily use Entry Form worksheet
.Range("E10:L10").ClearContents 'Clears data from drop down selections
.Range("E10").Select 'Returns selection back to Date entry box
End With
This makes use of the With ... End With statement to control which worksheet is receiving attention. See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Copying and Pasting Row and Column Depending on cell value

Hello i need some help with a abit of code, basically i have a workbook which has a lot of data for product Accessories listed in rows in the columns next to these there are the products with blank fields in all of the cells, basically the point of this is when the user types an "x" in any of these cells the table is able to be filtered.
I have made a summary sheet where i want it to look at these specific columns for example columns E-N and if it contains an "x" it will copy the corresponding row which are A,B,C,D and paste it on this summary sheet one after another as well as copying the product from the row above all the x's
e.g
Column E has an x in E4,5,10,15,53
i want it to copy
ABCD4
ABCD5
ABCD10
ABCD15
ABCD53
as well as the product name e.g "melons" which is located just above the first blank box of each column in this case lets say E4 is the first blank so E3 would be the product name.
and paste it in a sheet called "Summary Sheet" one row after another.
I know this is really confusing but i hope you can help :)
code i have:
I now have this code and im almost at where i want to be i need help on the line
Range("A5").Select
It gives me an error but unless i select this cell it pastes randomly on the sheet
Private Sub CommandButton9_Click()
Range("A7:D7").Select
Range(Selection, Selection.End(xlDown)).Select ' Go to last line
' Add a filter behavior
Selection.AutoFilter Field:=5, Criteria1:="<>"
Range("A7:D7").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste data where you want
Sheets ("Summary Sheet")
Range("A5").Select
ActiveSheet.Paste
End Sub
The idea could be
Select your set of data
Apply filter to isolate rows with x as value for the filtering
column
Select data and do a copy / paste action
Proposed sample of code (should be adapted to your spreadsheets)
' Select your range of data - maybe
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Select ' Go to last line
' Add a filter behavior
Selection.AutoFilter Field:=5, Criteria1:="<>"
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
' Paste data where you want
Sheets("Feuil2").Select
Range("A1").PasteSpecial Paste:=xlPasteValues

Resources