Fill a table with input from other table via VBA - vba

I am trying to fill a table in one sheet with input from another table (different sheet "wsQuant") based on this function:
=CountIfs(Tabelle14[Question1];[#[Q1]];Tabelle14[Name];[#[Name]];Tabelle14[Participation];[#[Participation]])
This works, but since the table is very big, I am trying to realize it via VBA since the file always crashes (I assume it's too much calculation at once). My solution so far is:
Sub DataBase()
Dim wsQuant As Worksheet
Set wsQuant = Worksheets("quantitativ")
Range("I5").Value = WorksheetFunction.CountIfs("wsQuant[$Z$4:$Z$1000]", [#[Q1]], "wsQuant[Name]", [#[Name]], "wsQuant[Participation]", [#[Participation]])
End Sub
This doesn't work and I can't figure out why.
I searched the entire day now and didn't find any answers.
Any help would be gratly appreciated.
EDIT:
Sample:
Data from the questionaire
This is how the data is given. Answers to the questions on a scale from 1 to 5.
List to be filled
This list should be filled through counting the answers that match the first 4 cloumns.

Related

VBA reading cell values into a list then using it

I have a long list of data, one line for one movement. I need to check the volumes of the movements by location. The locations can be grouped, as some of them has the same criteria for filtering. For doing that I would like to read the cell values into a list and then looping through the original list of data by checking into which group does the location belongs. I don't know how to read the group into a list in a way that then I can check if a cell value matches with any of the items in the original - to be filtered - list.
Can anyone help with it? Also, in case you have any other suggestion for the solution, I appreciate it too.
Thanks!!
Please provide us your own solution or at least a attempts to present your effort. Without any data or code to analyze I can suggest sth like this:
Sub Make_a_List()
Dim TableWithData() as Variant
TableWithData = Range(Cells(1,1),Cells(100,100)) 'Values as example
End Sub
This gives you an table with elements of the specified range. For one row you will get one dimension table.
Then for instance you can make a for loop:
For i=1 to Ubound(TableWithData)
if TableWithData(i) = sth then
do whatever you need
end if
Next i

Search xlsx for a value, if found replace with different value

So I'm either being too specific, or not searching well enough, because I can't find anything that answers my question. So I came here for help. Here is the situation:
I have an excel sheet, let's call it "CustomerCodeReference", that has a column (A) of Customer Codes (I.E. A2001, A2002, B3900, Q2838, etc, these are NOT necessarily in order) About 3000 of them, and in the next column over (B), I have the group that code represents (I.E. Accounts Primary, Accounts Secondary, Admin Group, User Group, just different names and etc.)
Now, from our company server I can export a spreadsheet of reports from customers, but the problem is, they are labelled by customer code, + a report serial number. The sheet exports as several columns, but one of the columns (G) contains the Customer code and serial number, and each row is a report, sometimes hundreds depending on the date range set. So keeping with the example, let's say it's a report from "Accounts Primary" It's labelled A2001234567 (where everything after the customer code of 'A2001' is the report serial number) sometimes, the report may be from several customers, so that column may have more than one code+SN in it per row.
Given that I have thousands of these codes and groups, is there some macro I can create that every time I export the spreadsheet of reports, I can maybe copy over the "CustomerCodeReference" sheet, and have it automatically search the column of customer codes and SNs, then either replace the code with the actual name, or place the actual name in another (empty) row further back. So I can basically easily reference whose report it is without having to look up the code each time?
I realize I will need to do this in VBA, as there is no formula I can think of that will work.
I have some pro's I think going for me:
-I already have the Master code list, so even though there are thousands of codes, they are all listed in Column A, and the actual name of group they reference is in column B.
-The codes are consistent, a letter, followed by 4 numbers, so always 5 characters long.
-When pulling the report, it always names the worksheet "Customer Reports" so it's easy to reference
These are constants. So I need the actual customer name to either replace the code (while leaving the serial number intact) or if easier, add the actual name to the next empty column on the same row. I also might need to share this with coworkers, so basically just send them the "CustomerCodeReference" sheet and when they add it to all their pulled spreadsheets, it does the same thing. (Macros will be enabled, so no worries there)
Is this too complicated an idea? or can I pull it off? Thanks in advance for the help!
EDIT: I apologize, I complete forgot to attach any sort of code. Here is what I have come up with in VBA, but not sure if I am on the right track as it does not complete the replacement, and I can't quite get it to add values in next available empty cell.
Sub replaceStringInCell()
'declaring my sheet I want to change change customer codes in
Dim CustomerCodes As Range
'declaring strings I will be replacing and with what I will be replacing them
Dim ReportNumbers As Range
Dim CustomerNames As Range
'identifying column I am working to replace, also trying to shoot for next empty column
Set CustomerCodes = PulledReports.Worksheets("Customer Reports").Range("G:G")
'specifying my strings
ReportNumbers = PulledReports.Worksheets("Customer Reports").Range("G:G")
myReplacementString = PulledReports.Worksheets("Customer Code Reference").Range("A:A")
'replace string in cell
CustomerCodes.Value = Replace(Expression:=CustomerCodes.Value, Find:=ReportNumbers, Replace:=CustomerNames)
End Sub
This should do the trick:
Sub stack_overflow()
Dim cust As Worksheet
Dim ref As Worksheet
Set cust = ActiveWorkbook.Worksheets("Customer Reports")
Set ref = ActiveWorkbook.Worksheets("Customer Code Reference")
'Finding next empty column
Dim column As Integer
column = cust.UsedRange.Columns.column + 1
'Filling this columns
For Each cell In cust.Range("G2:G" & cust.Cells(Rows.Count, "G").End(xlUp).Row)
cust.Cells(cell.Row, column).Value = Application.WorksheetFunction.VLookup(Left(cell.Value, 5), _
ref.Range("A2:B" & ref.Cells(Rows.Count, "B").End(xlUp).Row), 2, False)
Next cell
End Sub

Tabulating a final score for a questionnaire

Thanking in advance for your help with this one. I have searched everywhere but haven't been able to find an answer that addresses my issue.
Here is the background:
I have a questionnaire with 15 questions for which the possible answers are “Y”, “N” or “N/A”. For each question, I have assigned to the “Y” a value and to the “N” and “N/A” a zero.
I need a macro that will, for the active row in question, identify the heading of each question (1-15), look up each heading on a different worksheet in the same file, locate the number assigned to it if the answer is either “Y” or “N” (ignore the “N/A”s) and add all those numbers for the base score.
Then, as a second step, the macro will add only the “Y” answers and tabulate them against the base score for a final score.
For illustration purposes let’s say that I have the following questions:
Table Chair Lamp Plant
Y Y N N/A
In a different tab, the table looks like this:
Table Chair Lamp Plant
5 10 8 15
I need a macro that will look at the column headings to match them, and then at the values assigned to table, chair and lamp (ignore plant since N/A), add them (for a total of 23). Then add only the Ys (for a total of 15) and then produce a final score of 65 (15 out of 23 or 65%).
Really stuck with this one… I have no idea how to even start… any help is truly appreciated.
Thank you so much!!!!
Marta
When you have a big task in front of you, you need to divide it up incrementally. Stack Overflow isn't here to write your macro for you.
The first part you want to do is match the column headers in the Active Row with the column headers on another worksheet, right? What I've posted below should get you the column headers on the question sheet and read them off to you. Try it out and take it further (do some googling if you need to). When you run into a problem with the code you make, that's where a question should be asked.
(Place this code into a fresh module in your workbook, and connect it to a button or something on the questionnaire worksheet)
Public Sub TallyQuestions()
Dim questionSht As Worksheet
Set questionSht = Sheets("Worksheet that has the questions you were talking about") 'replace name with name of questionSht
Dim questionShtColHeaders As Range, cell As Range
Set questionShtColHeaders = questionSht.Range("A1:A15")
For Each cell In questionShtColHeaders
MsgBox cell.Value
Next cell
End Sub

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

External Data Pull: Pull more specific data or change macro based on data

This question is somewhat difficult to explain, so bear with me.
I am pulling data from a large table for my company and am trying to create a macro to make this data easier to read/understand. The data that is on the site changes every day based on what caused certain failures in our plant, which causes my macro to analyze data that isn't there or wrong cells (due to rows getting shifted/moved/added/removed). Because I don't think that was really clear, here is an example:
The macro says to select cells J5, J13, and J25. These were, when I was creating the macro, the values I wanted to be put in a list. However, when I pulled the data and ran the macro today, these values were in different spots on my sheet (the value for cell J13 is now in J12). This completely messes up all of the analysis and renders my macro / data pull useless.
Is there a way to have the macro select the data more intelligently? Perhaps have it check for the group name, then select the value from the cell next to it? I wish I could word this better... Thanks if you've gotten this far!
Simply put... yes. Here's a code exert for looking for a groupname and getting the adjacent cell:
Dim Group1Range As Range
'Look in ThisWorkbook
With ThisWorkbook
'Look in Sheet1
With .Sheets(1)
'Look in Column I
With .Columns("I:I")
'Find the text Group1
Set Group1Range = .Find(What:="Group1").Offset(0, 1)
End With
End With
End With
'Indicate the address of the found range
Debug.Print Group1Range.Address
End Sub
Now here are ways that you can improve your question:
Explain how you know that cell J13 is no longer valid, and that J12 is now.
Give us some sample data.
Give us your code.
Tell us what your end result would be, possibly with an example.